aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/iterator_ut.cpp
blob: 5dc085c82149dc40b5606cdb66ebc9fa7fae9f54 (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
#include "iterator.h"

#include <library/cpp/testing/unittest/registar.h>

Y_UNIT_TEST_SUITE(TIterator) {
    Y_UNIT_TEST(ToForwardIteratorTest) {
        TVector<int> x = {1, 2};
        UNIT_ASSERT_VALUES_EQUAL(*std::prev(x.end()), *ToForwardIterator(x.rbegin()));
        UNIT_ASSERT_VALUES_EQUAL(*ToForwardIterator(std::prev(x.rend())), *x.begin());
    }
}
 
Y_UNIT_TEST_SUITE(TInputRangeAdaptor) { 
    class TSquaresGenerator: public TInputRangeAdaptor<TSquaresGenerator> {
    public: 
        const i64* Next() { 
            Current_ = State_ * State_; 
            ++State_; 
            // Never return nullptr => we have infinite range! 
            return &Current_; 
        } 
 
    private: 
        i64 State_ = 0.0; 
        i64 Current_ = 0.0; 
    }; 
 
    Y_UNIT_TEST(TSquaresGenerator) { 
        i64 cur = 0; 
        for (i64 sqr : TSquaresGenerator{}) { 
            UNIT_ASSERT_VALUES_EQUAL(cur * cur, sqr); 
 
            if (++cur > 10) { 
                break; 
            } 
        } 
    } 
 
    class TUrlPart: public TInputRangeAdaptor<TUrlPart> {
    public: 
        TUrlPart(const TStringBuf& url)
            : Url_(url)
        {
        } 
 
        NStlIterator::TProxy<TStringBuf> Next() { 
            return Url_.NextTok('/'); 
        } 
 
    private: 
        TStringBuf Url_; 
    }; 
 
    Y_UNIT_TEST(TUrlPart) { 
        const TVector<TStringBuf> expected = {TStringBuf("yandex.ru"), TStringBuf("search?")};
        auto expected_part = expected.begin(); 
        for (const TStringBuf& part : TUrlPart(TStringBuf("yandex.ru/search?"))) {
            UNIT_ASSERT_VALUES_EQUAL(part, *expected_part);
            ++expected_part;
        } 
        UNIT_ASSERT(expected_part == expected.end());
    } 
}