aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/iterator/ut/iterate_values_ut.cpp
blob: ed099e560d0c5aae2cf6613b51b7ed3143ba3925 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <library/cpp/iterator/iterate_values.h>

#include <library/cpp/testing/gtest/gtest.h>

#include <util/generic/algorithm.h>

#include <map>
#include <unordered_map>

using namespace testing;

TEST(IterateValues, ConstMappingIteration) {
    const std::map<int, int> squares{
        {1, 1},
        {2, 4},
        {3, 9},
    };
    EXPECT_THAT(
        IterateValues(squares),
        ElementsAre(1, 4, 9)
    );

    const std::unordered_map<int, int> roots{
        {49, 7},
        {36, 6},
        {25, 5},
    };
    EXPECT_THAT(
        IterateValues(roots),
        UnorderedElementsAre(5, 6, 7)
    );

    const std::map<int, std::string> translations{
        {1, "one"},
        {2, "two"},
        {3, "three"},
    };
    EXPECT_EQ(
        Accumulate(IterateValues(translations), std::string{}),
        "onetwothree"
    );
}

TEST(IterateValues, NonConstMappingIteration) {
    std::map<int, int> squares{
        {1, 1},
        {2, 4},
        {3, 9},
    };
    for (auto& value: IterateValues(squares)) {
        value *= value;
    }
    EXPECT_THAT(
        IterateValues(squares),
        ElementsAre(1, 16, 81)
    );
}

TEST(IterateValues, ConstMultiMappingIteration) {
    const std::multimap<int, int> primesBelow{
        {2, 2},
        {5, 3},
        {5, 5},
        {11, 7},
        {11, 11},
        {23, 13},
        {23, 17},
        {23, 23},
    };

    EXPECT_THAT(
        IterateValues(primesBelow),
        ElementsAre(2, 3, 5, 7, 11, 13, 17, 23)
    );
    auto [begin, end] = primesBelow.equal_range(11);
    EXPECT_EQ(std::distance(begin, end), 2);
    EXPECT_THAT(
        IterateValues(std::vector(begin, end)),
        ElementsAre(7, 11)
    );
}

TEST(IterateValues, ConstUnorderedMultiMappingIteration) {
    const std::unordered_multimap<int, int> primesBelow{
        {2, 2},
        {5, 3},
        {5, 5},
        {11, 7},
        {11, 11},
        {23, 13},
        {23, 17},
        {23, 23},
    };

    EXPECT_THAT(
        IterateValues(primesBelow),
        UnorderedElementsAre(2, 3, 5, 7, 11, 13, 17, 23)
    );

    auto [begin, end] = primesBelow.equal_range(11);
    EXPECT_EQ(std::distance(begin, end), 2);
    EXPECT_THAT(
        IterateValues(std::vector(begin, end)),
        UnorderedElementsAre(7, 11)
    );
}