aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/xrange_ut.cpp
blob: 45750307927c42a1ae42799b60e7ad21c3e40d0a (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "xrange.h"

#include "algorithm.h"
#include "maybe.h"
#include "vector.h"
#include <library/cpp/testing/unittest/registar.h>
#include <util/string/builder.h>

Y_UNIT_TEST_SUITE(XRange) {
    void TestXRangeImpl(size_t begin, size_t end) {
        size_t count = 0;
        size_t sum = 0;
        size_t first = 42;
        bool firstInited = false;
        size_t last = 0;

        for (auto i : xrange(begin, end)) {
            ++count;
            sum += i;
            last = i;
            if (!firstInited) {
                first = i;
                firstInited = true;
            }
        }

        UNIT_ASSERT_VALUES_EQUAL(count, end - begin);
        UNIT_ASSERT_VALUES_EQUAL(first, begin);
        UNIT_ASSERT_VALUES_EQUAL(last, end - 1);
        UNIT_ASSERT_VALUES_EQUAL(sum, count * (first + last) / 2);
    }

    void TestSteppedXRangeImpl(int begin, int end, int step, const TVector<int>& expected) {
        size_t expInd = 0;
        for (auto i : xrange(begin, end, step)) {
            UNIT_ASSERT(expInd < expected.size());
            UNIT_ASSERT_VALUES_EQUAL(i, expected[expInd]);
            ++expInd;
        }
        UNIT_ASSERT_VALUES_EQUAL(expInd, expected.size());
    }

    Y_UNIT_TEST(IncrementWorks) {
        TestXRangeImpl(0, 10);
        TestXRangeImpl(10, 20);
    }

    Y_UNIT_TEST(DecrementWorks) {
        TestSteppedXRangeImpl(10, 0, -1, {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}); 
        TestSteppedXRangeImpl(10, -1, -1, {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}); 
        TestSteppedXRangeImpl(20, 9, -1, {20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10}); 
    }

    Y_UNIT_TEST(StepWorks) {
        TestSteppedXRangeImpl(0, 0, 1, {}); 
        TestSteppedXRangeImpl(0, 9, 3, {0, 3, 6}); 
        TestSteppedXRangeImpl(0, 10, 3, {0, 3, 6, 9}); 
        TestSteppedXRangeImpl(0, 11, 3, {0, 3, 6, 9}); 
        TestSteppedXRangeImpl(0, 12, 3, {0, 3, 6, 9}); 
        TestSteppedXRangeImpl(0, 13, 3, {0, 3, 6, 9, 12}); 
        TestSteppedXRangeImpl(0, 10, 2, {0, 2, 4, 6, 8}); 
        TestSteppedXRangeImpl(15, 0, -4, {15, 11, 7, 3}); 
        TestSteppedXRangeImpl(15, -1, -4, {15, 11, 7, 3}); 
        TestSteppedXRangeImpl(15, -2, -4, {15, 11, 7, 3, -1}); 
    }

    Y_UNIT_TEST(PointersWorks) {
        TVector<size_t> data = {3, 1, 4, 1, 5, 9, 2, 6};
        const size_t digSumExpected = Accumulate(data.begin(), data.end(), static_cast<size_t>(0));
        size_t digSumByIt = 0;
        for (auto ptr : xrange(data.begin(), data.end())) {
            digSumByIt += *ptr;
        }
        UNIT_ASSERT_VALUES_EQUAL(digSumByIt, digSumExpected);
        size_t digSumByPtr = 0;
        for (auto ptr : xrange(&data[0], &data[0] + data.size())) {
            digSumByPtr += *ptr;
        }
        UNIT_ASSERT_VALUES_EQUAL(digSumByPtr, digSumExpected);
    }

    Y_UNIT_TEST(SizeMethodCheck) {
        UNIT_ASSERT_VALUES_EQUAL(xrange(5).size(), 5);
        UNIT_ASSERT_VALUES_EQUAL(xrange(0, 5, 2).size(), 3);
        UNIT_ASSERT_VALUES_EQUAL(xrange(0, 6, 2).size(), 3);
    }

    class TVectorChild: public TVector<size_t> {
    public:
        template <typename TIterator>
        TVectorChild(TIterator a, TIterator b)
            : TVector<size_t>(a, b)
        {
        }
    };

    Y_UNIT_TEST(ConvertionWorks) {
        TVector<size_t> data = {0, 1, 2, 3, 4, 5, 6, 7, 8};

        TVector<size_t> convertionResults[] = {xrange<size_t>(9),
                                               xrange<ui32>(0, 9),
                                               xrange(0, 9, 1)};

        for (const auto& arr : convertionResults) {
            UNIT_ASSERT(arr == data);
        }

        TVectorChild sons[] = {xrange(0, 9),
                               xrange(0, 9, 1)};

        for (const auto& arr : sons) {
            UNIT_ASSERT(arr == data);
        }
    }

    template <class XRangeContainer>
    void TestEmptyRanges(const XRangeContainer& c) {
        for (const auto& emptyRange : c) {
            UNIT_ASSERT_VALUES_EQUAL(emptyRange.size(), 0);

            for (auto i : emptyRange) {
                Y_UNUSED(i);
                UNIT_ASSERT(false);
            }

            using TValueType = decltype(*emptyRange.begin());
            const TVector<TValueType> asVector = emptyRange;
            UNIT_ASSERT(asVector.empty());
        }
    }

    Y_UNIT_TEST(EmptySimpleRange) {
        using TSimpleRange = decltype(xrange(1));

        const TSimpleRange emptySimpleRanges[] = {
            xrange(-1),
            xrange(-10),
            xrange(0, -5),
            xrange(10, 10),
            xrange(10, 9),
        };

        TestEmptyRanges(emptySimpleRanges);
    }

    Y_UNIT_TEST(EmptySteppedRange) {
        using TSteppedRange = decltype(xrange(1, 10, 1));

        const TSteppedRange emptySteppedRanges[] = {
            xrange(5, 5, 1),
            xrange(5, 0, 5),
            xrange(0, -1, 5),
            xrange(0, 1, -1),
            xrange(0, -10, 10),
        };

        TestEmptyRanges(emptySteppedRanges);
    }

    template <class TRange>
    static void TestIteratorDifferenceImpl(TRange range, int a, int b, TMaybe<int> step) {
        auto fmtCase = [&]() -> TString { return TStringBuilder() << "xrange(" << a << ", " << b << (step ? ", " + ToString(*step) : TString{}) << ")"; };
        auto begin = std::begin(range);
        auto end = std::end(range);
        auto distance = end - begin;
        UNIT_ASSERT_VALUES_EQUAL_C(range.size(), distance, fmtCase());
        UNIT_ASSERT_EQUAL_C(end, begin + distance, fmtCase());
    }

    Y_UNIT_TEST(IteratorDifference) {
        for (int a = -20; a <= 20; ++a) {
            for (int b = -20; b <= 20; ++b) {
                for (int step = -25; step <= 25; ++step) {
                    if (step != 0) {
                        TestIteratorDifferenceImpl(xrange(a, b, step), a, b, step);
                    }
                }
                TestIteratorDifferenceImpl(xrange(a, b), a, b, Nothing());
            }
        }
    }

    Y_UNIT_TEST(Advance) {
        {
            auto range = xrange(30, 160, 7);
            auto it = std::begin(range);
            it += 5;
            UNIT_ASSERT_VALUES_EQUAL(*(std::begin(range) + 5), *it);
            UNIT_ASSERT_VALUES_EQUAL(65, *it);
            it -= 2;
            UNIT_ASSERT_VALUES_EQUAL(*(std::begin(range) + 3), *it);
            UNIT_ASSERT_VALUES_EQUAL(51, *it);
            std::advance(it, 10);
            UNIT_ASSERT_VALUES_EQUAL(*(std::begin(range) + 13), *it);
            UNIT_ASSERT_VALUES_EQUAL(121, *it);
            std::advance(it, -5);
            UNIT_ASSERT_VALUES_EQUAL(*(std::begin(range) + 8), *it);
            UNIT_ASSERT_VALUES_EQUAL(86, *it);
        }
        {
            auto range = xrange(-20, 100);
            auto it = std::begin(range);
            it += 5;
            UNIT_ASSERT_VALUES_EQUAL(*(std::begin(range) + 5), *it);
            UNIT_ASSERT_VALUES_EQUAL(-15, *it);
            it -= 2;
            UNIT_ASSERT_VALUES_EQUAL(*(std::begin(range) + 3), *it);
            UNIT_ASSERT_VALUES_EQUAL(-17, *it);
            std::advance(it, 30);
            UNIT_ASSERT_VALUES_EQUAL(*(std::begin(range) + 33), *it);
            UNIT_ASSERT_VALUES_EQUAL(13, *it);
            std::advance(it, -8);
            UNIT_ASSERT_VALUES_EQUAL(*(std::begin(range) + 25), *it);
            UNIT_ASSERT_VALUES_EQUAL(5, *it);
        }
    }
}