recursive_iterator_mixed_container_test.cxx 4.3 KB

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