recursive_iterator_mixed_container_test.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "iterator/recursive_iterator.hpp"
  2. #include <map>
  3. #include <vector>
  4. #include <gmock/gmock.h>
  5. TEST(RecursiveIteratorMapVectorTest, IterDistanceIsSumOfInnerContainerSizes) {
  6. std::map<int, std::vector<int>> const obj{
  7. {1, {1, 2}},
  8. {2, {3, 4, 5}}
  9. };
  10. auto rit = make_recursive_iterator(obj);
  11. decltype(rit) end{ };
  12. // TODO: Actually perform the summation?
  13. EXPECT_THAT(std::distance(rit, end), 5);
  14. }
  15. TEST(RecursiveIteratorMapVectorTest, ElementsAreUnwrappedAsATuple) {
  16. std::map<int, std::vector<int>> const obj{
  17. {1, {1, 2}},
  18. {2, {3, 4, 5}}
  19. };
  20. std::vector<std::tuple<int, int>> const expected{
  21. {1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}};
  22. // TODO: Collapse into a container instead
  23. auto rit = make_recursive_iterator(obj);
  24. for (auto it = expected.begin(), end = expected.end(); it != end; ++it, ++rit) {
  25. EXPECT_THAT(*it, *rit);
  26. }
  27. }
  28. TEST(RecursiveIteratorMapVectorTest, CanMutatePointedToData) {
  29. std::map<int, std::vector<int>> obj{
  30. {1, {1, 2}},
  31. {2, {3, 4, 5}}
  32. };
  33. auto rit = make_recursive_iterator(obj);
  34. std::get<1>(*rit) = 6;
  35. EXPECT_THAT(obj[1][0], 6);
  36. }
  37. TEST(RecursiveIteratorVectorMapTest, IterDistanceIsSumOfInnerContainerSizes) {
  38. std::vector<std::map<int, int>> const obj{
  39. {{1, 1}, {2, 2}},
  40. {{3, 3}, {4, 4}, {5, 5}}
  41. };
  42. auto rit = make_recursive_iterator(obj);
  43. decltype(rit) end{ };
  44. // TODO: Actually perform the summation?
  45. EXPECT_THAT(std::distance(rit, end), 5);
  46. }
  47. TEST(RecursiveIteratorVectorMapTest, ElementsAreUnwrappedAsATuple) {
  48. std::vector<std::map<int, int>> const obj{
  49. {{1, 1}, {2, 2}},
  50. {{3, 3}, {4, 4}, {5, 5}}
  51. };
  52. std::vector<std::pair<int const, int>> const expected{
  53. {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
  54. auto rit = make_recursive_iterator(obj);
  55. // TODO: Collapse into a container instead
  56. for (auto it = expected.begin(), end = expected.end(); it != end; ++it, ++rit) {
  57. EXPECT_THAT(*it, *rit);
  58. }
  59. }
  60. TEST(RecursiveIteratorVectorMapTest, CanMutatePointedToData) {
  61. std::vector<std::map<int, int>> obj{
  62. {{1, 1}, {2, 2}},
  63. {{3, 3}, {4, 4}, {5, 5}}
  64. };
  65. auto rit = make_recursive_iterator(obj);
  66. std::get<1>(*rit) = 6;
  67. EXPECT_THAT(obj[0][1], 6);
  68. }
  69. TEST(RecursiveIteratorMapVecMapTest, IterDistanceIsSumOfInnerContainerSizes) {
  70. std::map<int, std::vector<std::map<int, int>>> const obj{
  71. {1, {
  72. {{1, 1}, {2, 2}}
  73. }}
  74. };
  75. std::vector<std::tuple<int, int, int>> const expected{{1, 1, 1}, {1, 2, 2}};
  76. auto rit = make_recursive_iterator(obj);
  77. decltype(rit) end{ };
  78. EXPECT_THAT(std::distance(rit, end), expected.size());
  79. }
  80. TEST(RecursiveIteratorMapVecMapTest, ElementsAreUnwrappedAsATuple) {
  81. std::map<int, std::vector<std::map<int, int>>> const obj{
  82. {1, {
  83. {{1, 1}, {2, 2}}
  84. }}
  85. };
  86. std::vector<std::tuple<int, int, int>> const expected{{1, 1, 1}, {1, 2, 2}};
  87. auto rit = make_recursive_iterator(obj);
  88. // TODO: Collapse into a container instead
  89. for (auto it = expected.begin(), end = expected.end(); it != end; ++it, ++rit) {
  90. EXPECT_THAT(*it, *rit);
  91. }
  92. }
  93. TEST(RecursiveIteratorMapVecMapTest, CanMutatePointedToData) {
  94. std::map<int, std::vector<std::map<int, int>>> obj{{1, {{{1, 1}, {2, 2}}}}};
  95. auto rit = make_recursive_iterator(obj);
  96. std::get<2>(*rit) = 4;
  97. EXPECT_THAT(obj[1][0][1], 4);
  98. }
  99. TEST(RecursiveIteratorVecMapVecTest, IterDistanceIsSumOfInnerContainerSizes) {
  100. std::vector<std::map<int, std::vector<int>>> const obj{
  101. {
  102. {1, {1, 2}},
  103. {2, {3, 4, 5}}
  104. },
  105. {
  106. {1, {3, 4}}
  107. }
  108. };
  109. auto rit = make_recursive_iterator(obj);
  110. decltype(rit) end{ };
  111. // TODO: Actually perform the summation?
  112. EXPECT_THAT(std::distance(rit, end), 7);
  113. }
  114. TEST(RecursiveIteratorVecMapVecTest, ElementsAreUnwrappedAsATuple) {
  115. std::vector<std::map<int, std::vector<int>>> const obj{
  116. {
  117. {1, {1, 2}},
  118. {2, {3, 4, 5}}
  119. },
  120. {
  121. {1, {3, 4}}
  122. }
  123. };
  124. std::vector<std::tuple<int, int>> const expected{
  125. {1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {1, 3}, {1, 4}};
  126. auto rit = make_recursive_iterator(obj);
  127. // TODO: Collapse into a container instead
  128. for (auto it = expected.begin(), end = expected.end(); it != end; ++it, ++rit) {
  129. EXPECT_THAT(*it, *rit);
  130. }
  131. }
  132. TEST(RecursiveIteratorVecMapVecTest, CanMutatePointedToData) {
  133. std::vector<std::map<int, std::vector<int>>> obj{
  134. {
  135. {1, {1, 2}},
  136. {2, {3, 4, 5}}
  137. },
  138. {
  139. {1, {3, 4}}
  140. }
  141. };
  142. auto rit = make_recursive_iterator(obj);
  143. std::get<1>(*rit) = 6;
  144. EXPECT_THAT(obj[0][1][0], 6);
  145. }