aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/compact_vector/compact_vector_ut.cpp
blob: 4dba7b2f49e9fb0e7e563030ec4989b04b6a71a8 (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
#include <library/cpp/testing/unittest/registar.h>

#include "compact_vector.h"

Y_UNIT_TEST_SUITE(TCompactVectorTest) {
    Y_UNIT_TEST(TestSimple1) {
    }

    Y_UNIT_TEST(TestSimple) {
        TCompactVector<ui32> vector;
        for (ui32 i = 0; i < 10000; ++i) {
            vector.PushBack(i + 20);
            UNIT_ASSERT_VALUES_EQUAL(i + 1, vector.Size());
        }
        for (ui32 i = 0; i < 10000; ++i) {
            UNIT_ASSERT_VALUES_EQUAL(i + 20, vector[i]);
        }
    }

    Y_UNIT_TEST(TestInsert) {
        TCompactVector<ui32> vector;

        for (ui32 i = 0; i < 10; ++i) {
            vector.PushBack(i + 2);
        }

        vector.Insert(vector.Begin(), 99);

        UNIT_ASSERT_VALUES_EQUAL(11u, vector.Size());
        UNIT_ASSERT_VALUES_EQUAL(99u, vector[0]);
        for (ui32 i = 0; i < 10; ++i) {
            UNIT_ASSERT_VALUES_EQUAL(i + 2, vector[i + 1]);
        }

        vector.Insert(vector.Begin() + 3, 77);

        UNIT_ASSERT_VALUES_EQUAL(12u, vector.Size());
        UNIT_ASSERT_VALUES_EQUAL(99u, vector[0]);
        UNIT_ASSERT_VALUES_EQUAL(2u, vector[1]);
        UNIT_ASSERT_VALUES_EQUAL(3u, vector[2]);
        UNIT_ASSERT_VALUES_EQUAL(77u, vector[3]);
        UNIT_ASSERT_VALUES_EQUAL(4u, vector[4]);
        UNIT_ASSERT_VALUES_EQUAL(5u, vector[5]);
        UNIT_ASSERT_VALUES_EQUAL(11u, vector[11]);
    }

    Y_UNIT_TEST(TestInitializerListConstructor) {
        TCompactVector<ui32> vector = { 4, 8, 10, 3, 5};
        UNIT_ASSERT_VALUES_EQUAL(5u, vector.Size());

        UNIT_ASSERT_VALUES_EQUAL(4u, vector[0]);
        UNIT_ASSERT_VALUES_EQUAL(8u, vector[1]);
        UNIT_ASSERT_VALUES_EQUAL(10u, vector[2]);
        UNIT_ASSERT_VALUES_EQUAL(3u, vector[3]);
        UNIT_ASSERT_VALUES_EQUAL(5u, vector[4]);
    }

    Y_UNIT_TEST(TestIteratorConstructor) {
        TVector<ui32> origVector = { 4, 8, 10, 3, 5};
        TCompactVector<ui32> vector(origVector.begin(), origVector.end());
        UNIT_ASSERT_VALUES_EQUAL(5u, vector.Size());

        UNIT_ASSERT_VALUES_EQUAL(4u, vector[0]);
        UNIT_ASSERT_VALUES_EQUAL(8u, vector[1]);
        UNIT_ASSERT_VALUES_EQUAL(10u, vector[2]);
        UNIT_ASSERT_VALUES_EQUAL(3u, vector[3]);
        UNIT_ASSERT_VALUES_EQUAL(5u, vector[4]);
    }

    Y_UNIT_TEST(TestInitializerListCopyOperator) {
        TCompactVector<double> vector = { 4, 8, 10, 3, 5};
        UNIT_ASSERT_VALUES_EQUAL(5u, vector.Size());

        vector = { 11, 17, 23 };
        UNIT_ASSERT_VALUES_EQUAL(3u, vector.Size());

        UNIT_ASSERT_VALUES_EQUAL(11.0, vector[0]);
        UNIT_ASSERT_VALUES_EQUAL(17.0, vector[1]);
        UNIT_ASSERT_VALUES_EQUAL(23.0, vector[2]);
    }

    Y_UNIT_TEST(TestMoveConstructor) {
        TCompactVector<ui32> vector = { 4, 8, 10, 3, 5};
        auto it = vector.Begin();

        TCompactVector<ui32> vector2(std::move(vector));
        UNIT_ASSERT_VALUES_EQUAL(it, vector2.begin());

        UNIT_ASSERT_VALUES_EQUAL(5u, vector2.Size());

        UNIT_ASSERT_VALUES_EQUAL(4u, vector2[0]);
        UNIT_ASSERT_VALUES_EQUAL(8u, vector2[1]);
        UNIT_ASSERT_VALUES_EQUAL(10u, vector2[2]);
        UNIT_ASSERT_VALUES_EQUAL(3u, vector2[3]);
        UNIT_ASSERT_VALUES_EQUAL(5u, vector2[4]);
    }

    Y_UNIT_TEST(TestReverseIterators) {
        TCompactVector<std::string> vector = {
                "мама",
                "мыла",
                "раму"
        };

        TCompactVector<std::string> reverseVector(vector.rbegin(), vector.rend());
        UNIT_ASSERT_VALUES_EQUAL(3u, reverseVector.Size());

        UNIT_ASSERT_VALUES_EQUAL("раму", reverseVector[0]);
        UNIT_ASSERT_VALUES_EQUAL("мыла", reverseVector[1]);
        UNIT_ASSERT_VALUES_EQUAL("мама", reverseVector[2]);
    }

    Y_UNIT_TEST(TestErase) {
        TCompactVector<std::string> vector = {
                "мама",
                "утром",
                "мыла",
                "раму"
        };

        vector.erase(vector.begin() + 1);
        UNIT_ASSERT_VALUES_EQUAL(3u, vector.Size());

        UNIT_ASSERT_VALUES_EQUAL("мама", vector[0]);
        UNIT_ASSERT_VALUES_EQUAL("мыла", vector[1]);
        UNIT_ASSERT_VALUES_EQUAL("раму", vector[2]);
    }

    Y_UNIT_TEST(TestCopyAssignmentOperator) {
        TCompactVector<std::string> vector;
        const TCompactVector<std::string> vector2 = {
                "мама",
                "мыла",
                "раму"
        };

        vector = vector2;

        UNIT_ASSERT_VALUES_EQUAL(3u, vector.Size());

        UNIT_ASSERT_VALUES_EQUAL("мама", vector[0]);
        UNIT_ASSERT_VALUES_EQUAL("мыла", vector[1]);
        UNIT_ASSERT_VALUES_EQUAL("раму", vector[2]);

        UNIT_ASSERT_VALUES_EQUAL(vector[0], vector2[0]);
        UNIT_ASSERT_VALUES_EQUAL(vector[1], vector2[1]);
        UNIT_ASSERT_VALUES_EQUAL(vector[2], vector2[2]);
    }
}