recursive_iterator_mixed_container.t.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // recursive_iterator_mixed_container.t.h
  3. // iterator
  4. //
  5. // Created by Sam Jaffe on 2/18/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "recursive_iterator.hpp"
  10. class recursive_iterator_mixed_container_TestSuite : public CxxTest::TestSuite {
  11. public:
  12. void test_map_vector_iterator_matches_size() {
  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{{1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}};
  15. auto rit = make_recursive_iterator(obj);
  16. decltype(rit) end{ };
  17. TS_ASSERT_EQUALS(std::distance(rit, end), expected.size());
  18. }
  19. void test_map_vector_iterator_matches_data() {
  20. std::map<int, std::vector<int>> const obj{{1, {1, 2}}, {2, {3, 4, 5}}};
  21. std::vector<std::tuple<int, int>> const expected{{1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}};
  22. auto rit = make_recursive_iterator(obj);
  23. for (auto it = expected.begin(), end = expected.end(); it != end; ++it, ++rit) {
  24. TS_ASSERT_EQUALS(*it, *rit);
  25. }
  26. }
  27. void test_map_vector_iterator_can_edit_data() {
  28. std::map<int, std::vector<int>> obj{{1, {1, 2}}, {2, {3, 4, 5}}};
  29. auto rit = make_recursive_iterator(obj);
  30. std::get<1>(*rit) = 6;
  31. TS_ASSERT_EQUALS(obj[1][0], 6);
  32. }
  33. void test_vector_map_iterator_matches_size() {
  34. std::vector<std::map<int, int>> const obj{{{1, 1}, {2, 2}}, {{3, 3}, {4, 4}, {5, 5}}};
  35. std::vector<std::pair<int const, int>> const expected{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
  36. auto rit = make_recursive_iterator(obj);
  37. decltype(rit) end{ };
  38. TS_ASSERT_EQUALS(std::distance(rit, end), expected.size());
  39. }
  40. void test_vector_map_iterator_matches_data() {
  41. std::vector<std::map<int, int>> const obj{{{1, 1}, {2, 2}}, {{3, 3}, {4, 4}, {5, 5}}};
  42. std::vector<std::pair<int const, int>> const expected{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
  43. auto rit = make_recursive_iterator(obj);
  44. for (auto it = expected.begin(), end = expected.end(); it != end; ++it, ++rit) {
  45. TS_ASSERT_EQUALS(*it, *rit);
  46. }
  47. }
  48. void test_vector_map_iterator_can_edit_data() {
  49. std::vector<std::map<int, int>> obj{{{1, 1}, {2, 2}}, {{3, 3}, {4, 4}, {5, 5}}};
  50. auto rit = make_recursive_iterator(obj);
  51. std::get<1>(*rit) = 6;
  52. TS_ASSERT_EQUALS(obj[0][1], 6);
  53. }
  54. void test_map_vector_map_iterator_matches_size() {
  55. std::map<int, std::vector<std::map<int, int>>> const obj{{1, {{{1, 1}, {2, 2}}}}};
  56. std::vector<std::tuple<int, int, int>> const expected{{1, 1, 1}, {1, 2, 2}};
  57. auto rit = make_recursive_iterator(obj);
  58. decltype(rit) end{ };
  59. TS_ASSERT_EQUALS(std::distance(rit, end), expected.size());
  60. }
  61. void test_map_vector_map_iterator_matches_data() {
  62. std::map<int, std::vector<std::map<int, int>>> const obj{{1, {{{1, 1}, {2, 2}}}}};
  63. std::vector<std::tuple<int, int, int>> const expected{{1, 1, 1}, {1, 2, 2}};
  64. auto rit = make_recursive_iterator(obj);
  65. for (auto it = expected.begin(), end = expected.end(); it != end; ++it, ++rit) {
  66. TS_ASSERT_EQUALS(*it, *rit);
  67. }
  68. }
  69. void test_map_vector_map_iterator_can_edit_data() {
  70. std::map<int, std::vector<std::map<int, int>>> obj{{1, {{{1, 1}, {2, 2}}}}};
  71. auto rit = make_recursive_iterator(obj);
  72. std::get<2>(*rit) = 4;
  73. TS_ASSERT_EQUALS(obj[1][0][1], 4);
  74. }
  75. void test_vector_map_vector_iterator_matches_size() {
  76. std::vector<std::map<int, std::vector<int>>> const obj{{{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  77. std::vector<std::tuple<int, int>> const expected{{1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {1, 3}, {1, 4}};
  78. auto rit = make_recursive_iterator(obj);
  79. decltype(rit) end{ };
  80. TS_ASSERT_EQUALS(std::distance(rit, end), expected.size());
  81. }
  82. void test_vector_map_vector_iterator_matches_data() {
  83. std::vector<std::map<int, std::vector<int>>> const obj{{{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  84. std::vector<std::tuple<int, int>> const expected{{1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {1, 3}, {1, 4}};
  85. auto rit = make_recursive_iterator(obj);
  86. for (auto it = expected.begin(), end = expected.end(); it != end; ++it, ++rit) {
  87. TS_ASSERT_EQUALS(*it, *rit);
  88. }
  89. }
  90. void test_vector_map_vector_iterator_can_edit_data() {
  91. std::vector<std::map<int, std::vector<int>>> obj{{{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
  92. auto rit = make_recursive_iterator(obj);
  93. std::get<1>(*rit) = 6;
  94. TS_ASSERT_EQUALS(obj[0][1][0], 6);
  95. }
  96. };