recursive_iterator_mixed_container_test.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "iterator/recursive_iterator.hpp"
  2. #include <map>
  3. #include <vector>
  4. #include "xcode_gtest_helper.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. auto rit = make_recursive_iterator(obj);
  17. decltype(expected) result(rit, decltype(rit)());
  18. EXPECT_THAT(result, 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_THAT(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_THAT(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. decltype(rit) end{};
  38. // TODO: Actually perform the summation?
  39. EXPECT_THAT(std::distance(rit, end), 5);
  40. }
  41. TEST(RecursiveIteratorVectorMapTest, ElementsAreUnwrappedAsATuple) {
  42. std::vector<std::map<int, int>> const obj{{{1, 1}, {2, 2}},
  43. {{3, 3}, {4, 4}, {5, 5}}};
  44. std::vector<std::pair<int const, int>> const expected{
  45. {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
  46. auto rit = make_recursive_iterator(obj);
  47. decltype(expected) result(rit, decltype(rit)());
  48. EXPECT_THAT(result, 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 = make_recursive_iterator(obj);
  54. std::get<1>(*rit) = 6;
  55. EXPECT_THAT(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 = make_recursive_iterator(obj);
  62. decltype(rit) end{};
  63. EXPECT_THAT(std::distance(rit, end), expected.size());
  64. }
  65. TEST(RecursiveIteratorMapVecMapTest, ElementsAreUnwrappedAsATuple) {
  66. std::map<int, std::vector<std::map<int, int>>> const obj{
  67. {1, {{{1, 1}, {2, 2}}}}};
  68. std::vector<std::tuple<int, int, int>> const expected{{1, 1, 1}, {1, 2, 2}};
  69. auto rit = make_recursive_iterator(obj);
  70. decltype(expected) result(rit, decltype(rit)());
  71. EXPECT_THAT(result, 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 = make_recursive_iterator(obj);
  76. std::get<2>(*rit) = 4;
  77. EXPECT_THAT(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 = make_recursive_iterator(obj);
  83. decltype(rit) end{};
  84. // TODO: Actually perform the summation?
  85. EXPECT_THAT(std::distance(rit, end), 7);
  86. }
  87. TEST(RecursiveIteratorVecMapVecTest, ElementsAreUnwrappedAsATuple) {
  88. std::vector<std::map<int, std::vector<int>>> const obj{
  89. {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  90. std::vector<std::tuple<int, int>> const expected{
  91. {1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {1, 3}, {1, 4}};
  92. auto rit = make_recursive_iterator(obj);
  93. decltype(expected) result(rit, decltype(rit)());
  94. EXPECT_THAT(result, expected);
  95. }
  96. TEST(RecursiveIteratorVecMapVecTest, CanMutatePointedToData) {
  97. std::vector<std::map<int, std::vector<int>>> obj{
  98. {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  99. auto rit = make_recursive_iterator(obj);
  100. std::get<1>(*rit) = 6;
  101. EXPECT_THAT(obj[0][1][0], 6);
  102. }