recursive_iterator_mixed_container_test.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "iterator/recursive_iterator.h"
  2. #include <map>
  3. #include <vector>
  4. #include "ranges.h"
  5. #include "xcode_gtest_helper.h"
  6. using iterator::recursive_iterator;
  7. TEST(RecursiveIteratorMapVectorTest, IterDistanceIsSumOfInnerContainerSizes) {
  8. std::map<int, std::vector<int>> const obj{{1, {1, 2}}, {2, {3, 4, 5}}};
  9. auto rit = recursive_iterator(obj);
  10. EXPECT_EQ(ranges::distance(rit, iterator::sentinel), 5);
  11. }
  12. TEST(RecursiveIteratorMapVectorTest, ElementsAreUnwrappedAsATuple) {
  13. std::map<int, std::vector<int>> const obj{{1, {1, 2}}, {2, {3, 4, 5}}};
  14. std::vector<std::tuple<int, int>> const expected{
  15. {1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}};
  16. auto rit = recursive_iterator(obj);
  17. EXPECT_EQ(
  18. (ranges::to<std::vector<std::tuple<int, int>>>(rit, iterator::sentinel)),
  19. expected);
  20. }
  21. TEST(RecursiveIteratorMapVectorTest, CanMutatePointedToData) {
  22. std::map<int, std::vector<int>> obj{{1, {1, 2}}, {2, {3, 4, 5}}};
  23. auto rit = recursive_iterator(obj);
  24. std::get<1>(*rit) = 6;
  25. EXPECT_EQ(obj[1][0], 6);
  26. }
  27. TEST(RecursiveIteratorMapMapVectorTest, CanMutatePointedToData) {
  28. std::map<int, std::map<int, std::vector<int>>> obj{{1, {{1, {1, 2}}}},
  29. {2, {{1, {3, 4, 5}}}}};
  30. auto rit = recursive_iterator(obj);
  31. std::get<2>(*rit) = 6;
  32. EXPECT_EQ(obj[1][1][0], 6);
  33. }
  34. TEST(RecursiveIteratorVectorMapTest, IterDistanceIsSumOfInnerContainerSizes) {
  35. std::vector<std::map<int, int>> const obj{{{1, 1}, {2, 2}},
  36. {{3, 3}, {4, 4}, {5, 5}}};
  37. auto rit = recursive_iterator(obj);
  38. EXPECT_EQ(ranges::distance(rit, iterator::sentinel), 5);
  39. }
  40. TEST(RecursiveIteratorVectorMapTest, ElementsAreUnwrappedAsATuple) {
  41. std::vector<std::map<int, int>> const obj{{{1, 1}, {2, 2}},
  42. {{3, 3}, {4, 4}, {5, 5}}};
  43. std::vector<std::pair<int, int>> const expected{
  44. {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
  45. auto rit = recursive_iterator(obj);
  46. EXPECT_EQ(
  47. (ranges::to<std::vector<std::pair<int, int>>>(rit, iterator::sentinel)),
  48. expected);
  49. }
  50. TEST(RecursiveIteratorVectorMapTest, CanMutatePointedToData) {
  51. std::vector<std::map<int, int>> obj{{{1, 1}, {2, 2}},
  52. {{3, 3}, {4, 4}, {5, 5}}};
  53. auto rit = recursive_iterator(obj);
  54. std::get<1>(*rit) = 6;
  55. EXPECT_EQ(obj[0][1], 6);
  56. }
  57. TEST(RecursiveIteratorMapVecMapTest, IterDistanceIsSumOfInnerContainerSizes) {
  58. std::map<int, std::vector<std::map<int, int>>> const obj{
  59. {1, {{{1, 1}, {2, 2}}}}};
  60. std::vector<std::tuple<int, int, int>> const expected{{1, 1, 1}, {1, 2, 2}};
  61. auto rit = recursive_iterator(obj);
  62. EXPECT_EQ(ranges::distance(rit, iterator::sentinel), expected.size());
  63. }
  64. TEST(RecursiveIteratorMapVecMapTest, ElementsAreUnwrappedAsATuple) {
  65. std::map<int, std::vector<std::map<int, int>>> const obj{
  66. {1, {{{1, 1}, {2, 2}}}}};
  67. std::vector<std::tuple<int, int, int>> const expected{{1, 1, 1}, {1, 2, 2}};
  68. auto rit = recursive_iterator(obj);
  69. EXPECT_EQ((ranges::to<std::vector<std::tuple<int, int, int>>>(
  70. rit, iterator::sentinel)),
  71. expected);
  72. }
  73. TEST(RecursiveIteratorMapVecMapTest, CanMutatePointedToData) {
  74. std::map<int, std::vector<std::map<int, int>>> obj{{1, {{{1, 1}, {2, 2}}}}};
  75. auto rit = recursive_iterator(obj);
  76. std::get<2>(*rit) = 4;
  77. EXPECT_EQ(obj[1][0][1], 4);
  78. }
  79. TEST(RecursiveIteratorVecMapVecTest, IterDistanceIsSumOfInnerContainerSizes) {
  80. std::vector<std::map<int, std::vector<int>>> const obj{
  81. {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  82. auto rit = recursive_iterator(obj);
  83. EXPECT_EQ(ranges::distance(rit, iterator::sentinel), 7);
  84. }
  85. TEST(RecursiveIteratorVecMapVecTest, ElementsAreUnwrappedAsATuple) {
  86. std::vector<std::map<int, std::vector<int>>> const obj{
  87. {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  88. std::vector<std::tuple<int, int>> const expected{
  89. {1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {1, 3}, {1, 4}};
  90. auto rit = recursive_iterator(obj);
  91. EXPECT_EQ(
  92. (ranges::to<std::vector<std::tuple<int, int>>>(rit, iterator::sentinel)),
  93. expected);
  94. }
  95. TEST(RecursiveIteratorVecMapVecTest, CanMutatePointedToData) {
  96. std::vector<std::map<int, std::vector<int>>> obj{
  97. {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  98. auto rit = recursive_iterator(obj);
  99. std::get<1>(*rit) = 6;
  100. EXPECT_EQ(obj[0][1][0], 6);
  101. }