aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/stack_vector/stack_vec_ut.cpp
blob: 89821dd4a8feec116bcf9f6c1a0a56d60df7d767 (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
#include "stack_vec.h"

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

namespace { 
    struct TNotCopyAssignable { 
        const int Value; 
    }; 
 
    static_assert(std::is_copy_constructible_v<TNotCopyAssignable>); 
    static_assert(!std::is_copy_assignable_v<TNotCopyAssignable>); 

    template <class T, size_t JunkPayloadSize>
    struct TThickAlloc: public std::allocator<T> {
        template <class U>
        struct rebind {
            using other = TThickAlloc<U, JunkPayloadSize>;
        };

        char Junk[JunkPayloadSize]{sizeof(T)};
    };

    template <class T>
    struct TStatefulAlloc: public std::allocator<T> {
        using TBase = std::allocator<T>;

        template <class U>
        struct rebind {
            using other = TStatefulAlloc<U>;
        };

        TStatefulAlloc(size_t* allocCount)
            : AllocCount(allocCount)
        {}

        size_t* AllocCount;

        T* allocate(size_t n)
        {
            *AllocCount += 1;
            return TBase::allocate(n);
        }
    };
} 
 
Y_UNIT_TEST_SUITE(TStackBasedVectorTest) {
    Y_UNIT_TEST(TestCreateEmpty) {
        TStackVec<int> ints;
        UNIT_ASSERT_EQUAL(ints.size(), 0);
    }

    Y_UNIT_TEST(TestCreateNonEmpty) {
        TStackVec<int> ints(5);
        UNIT_ASSERT_EQUAL(ints.size(), 5);

        for (size_t i = 0; i < ints.size(); ++i) {
            UNIT_ASSERT_EQUAL(ints[i], 0);
        }
    }

    Y_UNIT_TEST(TestReallyOnStack) {
        const TStackVec<int> vec(5);

        UNIT_ASSERT(
            (const char*)&vec <= (const char*)&vec[0] &&
            (const char*)&vec[0] <= (const char*)&vec + sizeof(vec)
        );
    }

    Y_UNIT_TEST(TestFallback) {
        TSmallVec<int> ints;
        for (int i = 0; i < 14; ++i) {
            ints.push_back(i);
        }

        for (size_t i = 0; i < ints.size(); ++i) {
            UNIT_ASSERT_EQUAL(ints[i], (int)i);
        }

        for (int i = 14; i < 20; ++i) {
            ints.push_back(i);
        }

        for (size_t i = 0; i < ints.size(); ++i) {
            UNIT_ASSERT_EQUAL(ints[i], (int)i);
        }

        TSmallVec<int> ints2 = ints;

        for (size_t i = 0; i < ints2.size(); ++i) {
            UNIT_ASSERT_EQUAL(ints2[i], (int)i);
        }

        TSmallVec<int> ints3;
        ints3 = ints2;

        for (size_t i = 0; i < ints3.size(); ++i) {
            UNIT_ASSERT_EQUAL(ints3[i], (int)i);
        }
    }
 
    Y_UNIT_TEST(TestCappedSize) { 
        TStackVec<int, 8, false> ints; 
        ints.push_back(1); 
        ints.push_back(2); 
 
        auto intsCopy = ints; 
        UNIT_ASSERT_VALUES_EQUAL(intsCopy.capacity(), 8); 
 
        for (int i = 2; i != 8; ++i) { 
            intsCopy.push_back(i); 
        } 
        // Just verify that the program did not crash. 
    } 
 
    Y_UNIT_TEST(TestCappedSizeWithNotCopyAssignable) { 
        TStackVec<TNotCopyAssignable, 8, false> values; 
        values.push_back({1}); 
        values.push_back({2}); 
 
        auto valuesCopy = values; 
        UNIT_ASSERT_VALUES_EQUAL(valuesCopy.capacity(), 8); 
 
        for (int i = 2; i != 8; ++i) { 
            valuesCopy.push_back({i}); 
        } 
        // Just verify that the program did not crash. 
    } 

    Y_UNIT_TEST(TestCustomAllocSize) {
        constexpr size_t n = 16384;
        using TVec = TStackVec<size_t, 1, true, TThickAlloc<size_t, n>>;
        UNIT_ASSERT_LT(sizeof(TVec), 1.5 * n);
    }

    Y_UNIT_TEST(TestStatefulAlloc) {
        size_t count = 0;
        TStackVec<size_t, 1, true, TStatefulAlloc<size_t>> vec{{ &count }};
        for (size_t i = 0; i < 5; ++i) {
            vec.push_back(1);
        }
        UNIT_ASSERT_VALUES_EQUAL(count, 3);
    }
}