| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- #include "iterator/recursive_iterator.h"
- #include <map>
- #include <vector>
- #include "ranges.h"
- #include "xcode_gtest_helper.h"
- TEST(RecursiveIteratorMapVectorTest, IterDistanceIsSumOfInnerContainerSizes) {
- std::map<int, std::vector<int>> const obj{{1, {1, 2}}, {2, {3, 4, 5}}};
- auto rit = make_recursive_iterator(obj);
- EXPECT_EQ(ranges::distance(rit, iterator::sentinel), 5);
- }
- TEST(RecursiveIteratorMapVectorTest, ElementsAreUnwrappedAsATuple) {
- std::map<int, std::vector<int>> const obj{{1, {1, 2}}, {2, {3, 4, 5}}};
- std::vector<std::tuple<int, int>> const expected{
- {1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}};
- auto rit = make_recursive_iterator(obj);
- EXPECT_EQ(
- (ranges::to<std::vector<std::tuple<int, int>>>(rit, iterator::sentinel)),
- expected);
- }
- TEST(RecursiveIteratorMapVectorTest, CanMutatePointedToData) {
- std::map<int, std::vector<int>> obj{{1, {1, 2}}, {2, {3, 4, 5}}};
- auto rit = make_recursive_iterator(obj);
- std::get<1>(*rit) = 6;
- EXPECT_EQ(obj[1][0], 6);
- }
- TEST(RecursiveIteratorMapMapVectorTest, CanMutatePointedToData) {
- std::map<int, std::map<int, std::vector<int>>> obj{{1, {{1, {1, 2}}}},
- {2, {{1, {3, 4, 5}}}}};
- auto rit = make_recursive_iterator(obj);
- std::get<2>(*rit) = 6;
- EXPECT_EQ(obj[1][1][0], 6);
- }
- TEST(RecursiveIteratorVectorMapTest, IterDistanceIsSumOfInnerContainerSizes) {
- std::vector<std::map<int, int>> const obj{{{1, 1}, {2, 2}},
- {{3, 3}, {4, 4}, {5, 5}}};
- auto rit = make_recursive_iterator(obj);
- EXPECT_EQ(ranges::distance(rit, iterator::sentinel), 5);
- }
- TEST(RecursiveIteratorVectorMapTest, ElementsAreUnwrappedAsATuple) {
- std::vector<std::map<int, int>> const obj{{{1, 1}, {2, 2}},
- {{3, 3}, {4, 4}, {5, 5}}};
- std::vector<std::pair<int, int>> const expected{
- {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
- auto rit = make_recursive_iterator(obj);
- EXPECT_EQ(
- (ranges::to<std::vector<std::pair<int, int>>>(rit, iterator::sentinel)),
- expected);
- }
- TEST(RecursiveIteratorVectorMapTest, CanMutatePointedToData) {
- std::vector<std::map<int, int>> obj{{{1, 1}, {2, 2}},
- {{3, 3}, {4, 4}, {5, 5}}};
- auto rit = make_recursive_iterator(obj);
- std::get<1>(*rit) = 6;
- EXPECT_EQ(obj[0][1], 6);
- }
- TEST(RecursiveIteratorMapVecMapTest, IterDistanceIsSumOfInnerContainerSizes) {
- std::map<int, std::vector<std::map<int, int>>> const obj{
- {1, {{{1, 1}, {2, 2}}}}};
- std::vector<std::tuple<int, int, int>> const expected{{1, 1, 1}, {1, 2, 2}};
- auto rit = make_recursive_iterator(obj);
- EXPECT_EQ(ranges::distance(rit, iterator::sentinel), expected.size());
- }
- TEST(RecursiveIteratorMapVecMapTest, ElementsAreUnwrappedAsATuple) {
- std::map<int, std::vector<std::map<int, int>>> const obj{
- {1, {{{1, 1}, {2, 2}}}}};
- std::vector<std::tuple<int, int, int>> const expected{{1, 1, 1}, {1, 2, 2}};
- auto rit = make_recursive_iterator(obj);
- EXPECT_EQ((ranges::to<std::vector<std::tuple<int, int, int>>>(
- rit, iterator::sentinel)),
- expected);
- }
- TEST(RecursiveIteratorMapVecMapTest, CanMutatePointedToData) {
- std::map<int, std::vector<std::map<int, int>>> obj{{1, {{{1, 1}, {2, 2}}}}};
- auto rit = make_recursive_iterator(obj);
- std::get<2>(*rit) = 4;
- EXPECT_EQ(obj[1][0][1], 4);
- }
- TEST(RecursiveIteratorVecMapVecTest, IterDistanceIsSumOfInnerContainerSizes) {
- std::vector<std::map<int, std::vector<int>>> const obj{
- {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
- auto rit = make_recursive_iterator(obj);
- EXPECT_EQ(ranges::distance(rit, iterator::sentinel), 7);
- }
- TEST(RecursiveIteratorVecMapVecTest, ElementsAreUnwrappedAsATuple) {
- std::vector<std::map<int, std::vector<int>>> const obj{
- {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
- std::vector<std::tuple<int, int>> const expected{
- {1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {1, 3}, {1, 4}};
- auto rit = make_recursive_iterator(obj);
- EXPECT_EQ(
- (ranges::to<std::vector<std::tuple<int, int>>>(rit, iterator::sentinel)),
- expected);
- }
- TEST(RecursiveIteratorVecMapVecTest, CanMutatePointedToData) {
- std::vector<std::map<int, std::vector<int>>> obj{
- {{1, {1, 2}}, {2, {3, 4, 5}}}, {{1, {3, 4}}}};
- auto rit = make_recursive_iterator(obj);
- std::get<1>(*rit) = 6;
- EXPECT_EQ(obj[0][1][0], 6);
- }
|