aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/visit_ranges/visit_ranges_test.cpp
blob: 5cdf9a022d6bfcdfae1bfa7f7ec19cc012bf95df (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/overload.h>
#include <vespa/vespalib/util/visit_ranges.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <variant>
#include <string>

using namespace vespalib;

TEST(VisitRangeExample, set_intersection) {
    std::vector<int> first({1,3,7});
    std::vector<int> second({2,3,8});
    std::vector<int> result;
    vespalib::visit_ranges(overload{[](visit_ranges_either, int) {},
                                    [&result](visit_ranges_both, int x, int) { result.push_back(x); }},
                           first.begin(), first.end(), second.begin(), second.end());
    EXPECT_EQ(result, std::vector<int>({3}));
}

TEST(VisitRangeExample, set_subtraction) {
    std::vector<int> first({1,3,7});
    std::vector<int> second({2,3,8});
    std::vector<int> result;
    vespalib::visit_ranges(overload{[&result](visit_ranges_first, int a) { result.push_back(a); },
                                    [](visit_ranges_second, int) {},
                                    [](visit_ranges_both, int, int) {}},
                           first.begin(), first.end(), second.begin(), second.end());
    EXPECT_EQ(result, std::vector<int>({1,7}));
}

TEST(VisitRangesTest, empty_ranges_can_be_visited) {
    std::vector<int> a;
    std::vector<int> b;
    std::vector<int> c;
    auto visitor = overload
                   {
                       [&c](visit_ranges_either, int) {
                           c.push_back(42);
                       },
                       [&c](visit_ranges_both, int, int) {
                           c.push_back(42);
                       }
                   };
    vespalib::visit_ranges(visitor, a.begin(), a.end(), b.begin(), b.end());
    EXPECT_EQ(c, std::vector<int>({}));
}

TEST(VisitRangesTest, simple_merge_can_be_implemented) {
    std::vector<int> a({1,3,7});
    std::vector<int> b({2,3,8});
    std::vector<int> c;
    auto visitor = overload
                   {
                       [&c](visit_ranges_either, int x) {
                           c.push_back(x);
                       },
                       [&c](visit_ranges_both, int x, int y) {
                           c.push_back(x);
                           c.push_back(y);
                       }
                   };
    vespalib::visit_ranges(visitor, a.begin(), a.end(), b.begin(), b.end());
    EXPECT_EQ(c, std::vector<int>({1,2,3,3,7,8}));
}

TEST(VisitRangesTest, simple_union_can_be_implemented) {
    std::vector<int> a({1,3,7});
    std::vector<int> b({2,3,8});
    std::vector<int> c;
    auto visitor = overload
                   {
                       [&c](visit_ranges_either, int x) {
                           c.push_back(x);
                       },
                       [&c](visit_ranges_both, int x, int) {
                           c.push_back(x);
                       }
                   };
    vespalib::visit_ranges(visitor, a.begin(), a.end(), b.begin(), b.end());
    EXPECT_EQ(c, std::vector<int>({1,2,3,7,8}));
}

TEST(VisitRangesTest, asymmetric_merge_can_be_implemented) {
    std::vector<int> a({1,3,7});
    std::vector<int> b({2,3,8});
    std::vector<int> c;
    auto visitor = overload
                   {
                       [&c](visit_ranges_first, int x) {
                           c.push_back(x);
                       },
                       [](visit_ranges_second, int) {},
                       [&c](visit_ranges_both, int x, int y) {
                           c.push_back(x * y);
                       }
                   };
    vespalib::visit_ranges(visitor, a.begin(), a.end(), b.begin(), b.end());
    EXPECT_EQ(c, std::vector<int>({1,9,7}));
}

TEST(VisitRangesTest, comparator_can_be_specified) {
    std::vector<int> a({7,3,1});
    std::vector<int> b({8,3,2});
    std::vector<int> c;
    auto visitor = overload
                   {
                       [&c](visit_ranges_either, int x) {
                           c.push_back(x);
                       },
                       [&c](visit_ranges_both, int x, int y) {
                           c.push_back(x);
                           c.push_back(y);
                       }
                   };
    vespalib::visit_ranges(visitor, a.begin(), a.end(), b.begin(), b.end(), std::greater<>());
    EXPECT_EQ(c, std::vector<int>({8,7,3,3,2,1}));
}

GTEST_MAIN_RUN_ALL_TESTS()