iterate_values_ut.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <library/cpp/iterator/iterate_values.h>
  2. #include <library/cpp/testing/gtest/gtest.h>
  3. #include <util/generic/algorithm.h>
  4. #include <map>
  5. #include <unordered_map>
  6. using namespace testing;
  7. TEST(IterateValues, ConstMappingIteration) {
  8. const std::map<int, int> squares{
  9. {1, 1},
  10. {2, 4},
  11. {3, 9},
  12. };
  13. EXPECT_THAT(
  14. IterateValues(squares),
  15. ElementsAre(1, 4, 9)
  16. );
  17. const std::unordered_map<int, int> roots{
  18. {49, 7},
  19. {36, 6},
  20. {25, 5},
  21. };
  22. EXPECT_THAT(
  23. IterateValues(roots),
  24. UnorderedElementsAre(5, 6, 7)
  25. );
  26. const std::map<int, std::string> translations{
  27. {1, "one"},
  28. {2, "two"},
  29. {3, "three"},
  30. };
  31. EXPECT_EQ(
  32. Accumulate(IterateValues(translations), std::string{}),
  33. "onetwothree"
  34. );
  35. }
  36. TEST(IterateValues, NonConstMappingIteration) {
  37. std::map<int, int> squares{
  38. {1, 1},
  39. {2, 4},
  40. {3, 9},
  41. };
  42. for (auto& value: IterateValues(squares)) {
  43. value *= value;
  44. }
  45. EXPECT_THAT(
  46. IterateValues(squares),
  47. ElementsAre(1, 16, 81)
  48. );
  49. }
  50. TEST(IterateValues, ConstMultiMappingIteration) {
  51. const std::multimap<int, int> primesBelow{
  52. {2, 2},
  53. {5, 3},
  54. {5, 5},
  55. {11, 7},
  56. {11, 11},
  57. {23, 13},
  58. {23, 17},
  59. {23, 23},
  60. };
  61. EXPECT_THAT(
  62. IterateValues(primesBelow),
  63. ElementsAre(2, 3, 5, 7, 11, 13, 17, 23)
  64. );
  65. auto [begin, end] = primesBelow.equal_range(11);
  66. EXPECT_EQ(std::distance(begin, end), 2);
  67. EXPECT_THAT(
  68. IterateValues(std::vector(begin, end)),
  69. ElementsAre(7, 11)
  70. );
  71. }
  72. TEST(IterateValues, ConstUnorderedMultiMappingIteration) {
  73. const std::unordered_multimap<int, int> primesBelow{
  74. {2, 2},
  75. {5, 3},
  76. {5, 5},
  77. {11, 7},
  78. {11, 11},
  79. {23, 13},
  80. {23, 17},
  81. {23, 23},
  82. };
  83. EXPECT_THAT(
  84. IterateValues(primesBelow),
  85. UnorderedElementsAre(2, 3, 5, 7, 11, 13, 17, 23)
  86. );
  87. auto [begin, end] = primesBelow.equal_range(11);
  88. EXPECT_EQ(std::distance(begin, end), 2);
  89. EXPECT_THAT(
  90. IterateValues(std::vector(begin, end)),
  91. UnorderedElementsAre(7, 11)
  92. );
  93. }