aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/adaptor_ut.cpp
blob: 3ccd1b84bce22a99a2c832d3c34f7fd2b75677cc (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
#include "adaptor.h"
#include "yexception.h"

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

struct TOnCopy: yexception {
};

struct TOnMove: yexception {
};

struct TState {
    explicit TState() {
    }

    TState(const TState&) {
        ythrow TOnCopy();
    }

    TState(TState&&) {
        ythrow TOnMove();
    }

    void operator=(const TState&) {
        ythrow TOnCopy();
    }

    void rbegin() const {
    }

    void rend() const {
    }
};

Y_UNIT_TEST_SUITE(TReverseAdaptor) {
    Y_UNIT_TEST(ReadTest) {
        TVector<int> cont = {1, 2, 3};
        TVector<int> etalon = {3, 2, 1};
        size_t idx = 0;
        for (const auto& x : Reversed(cont)) {
            UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
        }
        idx = 0;
        for (const auto& x : Reversed(std::move(cont))) {
            UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
        }
    }

    Y_UNIT_TEST(WriteTest) {
        TVector<int> cont = {1, 2, 3};
        TVector<int> etalon = {3, 6, 9};
        size_t idx = 0;
        for (auto& x : Reversed(cont)) {
            x *= x + idx++;
        }
        idx = 0;
        for (auto& x : cont) {
            UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
        }
    }

    Y_UNIT_TEST(InnerTypeTest) {
        using TStub = TVector<int>;
        TStub stub;
        const TStub cstub;

        using namespace NPrivate;
        UNIT_ASSERT_TYPES_EQUAL(decltype(Reversed(stub)), TReverseRange<TStub&>);
        UNIT_ASSERT_TYPES_EQUAL(decltype(Reversed(cstub)), TReverseRange<const TStub&>);
    }

    Y_UNIT_TEST(CopyMoveTest) {
        TState lvalue;
        const TState clvalue;
        UNIT_ASSERT_NO_EXCEPTION(Reversed(lvalue));
        UNIT_ASSERT_NO_EXCEPTION(Reversed(clvalue));
    }

    Y_UNIT_TEST(ReverseX2Test) {
        TVector<int> cont = {1, 2, 3};
        size_t idx = 0;
        for (const auto& x : Reversed(Reversed(cont))) {
            UNIT_ASSERT_VALUES_EQUAL(cont[idx++], x);
        }
    }

    Y_UNIT_TEST(ReverseX3Test) {
        TVector<int> cont = {1, 2, 3};
        TVector<int> etalon = {3, 2, 1};
        size_t idx = 0;
        for (const auto& x : Reversed(Reversed(Reversed(cont)))) {
            UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
        }
    }

    Y_UNIT_TEST(ReverseTemporaryTest) {
        TVector<int> etalon = {3, 2, 1};
        TVector<int> etalon2 = {1, 2, 3};
        size_t idx = 0;
        for (const auto& x : Reversed(TVector<int>{1, 2, 3})) {
            UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
        }
        idx = 0;
        for (const auto& x : Reversed(Reversed(TVector<int>{1, 2, 3}))) {
            UNIT_ASSERT_VALUES_EQUAL(etalon2[idx++], x);
        }
    }

    Y_UNIT_TEST(ReverseInitializerListTest) {
        // initializer_list has no rbegin and rend
        auto cont = {1, 2, 3};
        TVector<int> etalon = {3, 2, 1};
        TVector<int> etalon2 = {1, 2, 3};

        size_t idx = 0;
        for (const auto& x : Reversed(cont)) {
            UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
        }
        idx = 0;
        for (const auto& x : Reversed(Reversed(cont))) {
            UNIT_ASSERT_VALUES_EQUAL(etalon2[idx++], x);
        }
    }
} // Y_UNIT_TEST_SUITE(TReverseAdaptor)