recursive_iterator_mixed_container_test.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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{{1, {1, 2}}, {2, {3, 4, 5}}};
  7. auto rit = make_recursive_iterator(obj);
  8. decltype(rit) end{};
  9. // TODO: Actually perform the summation?
  10. EXPECT_THAT(std::distance(rit, end), 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. // TODO: Collapse into a container instead
  17. auto rit = make_recursive_iterator(obj);
  18. for (auto it = expected.begin(), end = expected.end(); it != end;
  19. ++it, ++rit) {
  20. EXPECT_THAT(*it, *rit);
  21. }
  22. }
  23. TEST(RecursiveIteratorMapVectorTest, CanMutatePointedToData) {
  24. std::map<int, std::vector<int>> obj{{1, {1, 2}}, {2, {3, 4, 5}}};
  25. auto rit = make_recursive_iterator(obj);
  26. std::get<1>(*rit) = 6;
  27. EXPECT_THAT(obj[1][0], 6);
  28. }
  29. TEST(RecursiveIteratorVectorMapTest, IterDistanceIsSumOfInnerContainerSizes) {
  30. std::vector<std::map<int, int>> const obj{{{1, 1}, {2, 2}},
  31. {{3, 3}, {4, 4}, {5, 5}}};
  32. auto rit = make_recursive_iterator(obj);
  33. decltype(rit) end{};
  34. // TODO: Actually perform the summation?
  35. EXPECT_THAT(std::distance(rit, end), 5);
  36. }
  37. TEST(RecursiveIteratorVectorMapTest, ElementsAreUnwrappedAsATuple) {
  38. std::vector<std::map<int, int>> const obj{{{1, 1}, {2, 2}},
  39. {{3, 3}, {4, 4}, {5, 5}}};
  40. std::vector<std::pair<int const, int>> const expected{
  41. {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
  42. auto rit = make_recursive_iterator(obj);
  43. // TODO: Collapse into a container instead
  44. for (auto it = expected.begin(), end = expected.end(); it != end;
  45. ++it, ++rit) {
  46. EXPECT_THAT(*it, *rit);
  47. }
  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_THAT(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. decltype(rit) end{};
  62. EXPECT_THAT(std::distance(rit, end), 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 = make_recursive_iterator(obj);
  69. // TODO: Collapse into a container instead
  70. for (auto it = expected.begin(), end = expected.end(); it != end;
  71. ++it, ++rit) {
  72. EXPECT_THAT(*it, *rit);
  73. }
  74. }
  75. TEST(RecursiveIteratorMapVecMapTest, CanMutatePointedToData) {
  76. std::map<int, std::vector<std::map<int, int>>> obj{{1, {{{1, 1}, {2, 2}}}}};
  77. auto rit = make_recursive_iterator(obj);
  78. std::get<2>(*rit) = 4;
  79. EXPECT_THAT(obj[1][0][1], 4);
  80. }
  81. TEST(RecursiveIteratorVecMapVecTest, IterDistanceIsSumOfInnerContainerSizes) {
  82. std::vector<std::map<int, std::vector<int>>> const obj{
  83. {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  84. auto rit = make_recursive_iterator(obj);
  85. decltype(rit) end{};
  86. // TODO: Actually perform the summation?
  87. EXPECT_THAT(std::distance(rit, end), 7);
  88. }
  89. TEST(RecursiveIteratorVecMapVecTest, ElementsAreUnwrappedAsATuple) {
  90. std::vector<std::map<int, std::vector<int>>> const obj{
  91. {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  92. std::vector<std::tuple<int, int>> const expected{
  93. {1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {1, 3}, {1, 4}};
  94. auto rit = make_recursive_iterator(obj);
  95. // TODO: Collapse into a container instead
  96. for (auto it = expected.begin(), end = expected.end(); it != end;
  97. ++it, ++rit) {
  98. EXPECT_THAT(*it, *rit);
  99. }
  100. }
  101. TEST(RecursiveIteratorVecMapVecTest, CanMutatePointedToData) {
  102. std::vector<std::map<int, std::vector<int>>> obj{
  103. {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  104. auto rit = make_recursive_iterator(obj);
  105. std::get<1>(*rit) = 6;
  106. EXPECT_THAT(obj[0][1][0], 6);
  107. }