mapped_ut.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <library/cpp/iterator/mapped.h>
  2. #include <library/cpp/testing/gtest/gtest.h>
  3. #include <util/generic/map.h>
  4. #include <util/generic/vector.h>
  5. using namespace testing;
  6. TEST(TIterator, TMappedIteratorTest) {
  7. TVector<int> x = {1, 2, 3, 4, 5};
  8. auto it = MakeMappedIterator(x.begin(), [](int x) { return x + 7; });
  9. EXPECT_EQ(*it, 8);
  10. EXPECT_EQ(it[2], 10);
  11. }
  12. TEST(TIterator, TMappedRangeTest) {
  13. TVector<int> x = {1, 2, 3, 4, 5};
  14. EXPECT_THAT(
  15. MakeMappedRange(
  16. x,
  17. [](int x) { return x + 3; }
  18. ),
  19. ElementsAre(4, 5, 6, 7, 8)
  20. );
  21. }
  22. //TODO: replace with dedicated IterateKeys / IterateValues methods
  23. TEST(TIterator, TMutableMappedRangeTest) {
  24. TMap<int, int> points = {{1, 2}, {3, 4}};
  25. EXPECT_THAT(
  26. MakeMappedRange(
  27. points.begin(), points.end(),
  28. [](TMap<int, int>::value_type& kv) -> int& { return kv.second; }
  29. ),
  30. ElementsAre(2, 4)
  31. );
  32. }
  33. TEST(TIterator, TOwningMappedMethodTest) {
  34. auto range = MakeMappedRange(
  35. TVector<std::pair<int, int>>{std::make_pair(1, 2), std::make_pair(3, 4)},
  36. [](auto& point) -> int& {
  37. return point.first;
  38. }
  39. );
  40. EXPECT_EQ(range[0], 1);
  41. range[0] += 1;
  42. EXPECT_EQ(range[0], 2);
  43. (*range.begin()) += 1;
  44. EXPECT_EQ(range[0], 3);
  45. for (int& y : range) {
  46. y += 7;
  47. }
  48. EXPECT_EQ(*range.begin(), 10);
  49. EXPECT_EQ(*(range.begin() + 1), 10);
  50. }