aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/atomic_ut.cpp
blob: 07211ffba732d0998cc90432e4c030bbf4fdc6a2 (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
218
219
220
221
222
223
224
225
226
227
#include "atomic.h"

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

#include <util/generic/ylimits.h>

template <typename TAtomic>
class TAtomicTest
    : public TTestBase {
    UNIT_TEST_SUITE(TAtomicTest);
    UNIT_TEST(TestAtomicInc1)
    UNIT_TEST(TestAtomicInc2)
    UNIT_TEST(TestAtomicGetAndInc)
    UNIT_TEST(TestAtomicDec)
    UNIT_TEST(TestAtomicGetAndDec)
    UNIT_TEST(TestAtomicAdd)
    UNIT_TEST(TestAtomicGetAndAdd)
    UNIT_TEST(TestAtomicSub)
    UNIT_TEST(TestAtomicGetAndSub)
    UNIT_TEST(TestAtomicSwap)
    UNIT_TEST(TestAtomicOr)
    UNIT_TEST(TestAtomicAnd)
    UNIT_TEST(TestAtomicXor)
    UNIT_TEST(TestCAS)
    UNIT_TEST(TestGetAndCAS)
    UNIT_TEST(TestLockUnlock)
    UNIT_TEST_SUITE_END();

private:
    inline void TestLockUnlock() {
        TAtomic v = 0;

        UNIT_ASSERT(AtomicTryLock(&v));
        UNIT_ASSERT(!AtomicTryLock(&v));
        UNIT_ASSERT_VALUES_EQUAL(v, 1);
        AtomicUnlock(&v);
        UNIT_ASSERT_VALUES_EQUAL(v, 0);
    }

    inline void TestCAS() {
        TAtomic v = 0;

        UNIT_ASSERT(AtomicCas(&v, 1, 0));
        UNIT_ASSERT(!AtomicCas(&v, 1, 0));
        UNIT_ASSERT_VALUES_EQUAL(v, 1);
        UNIT_ASSERT(AtomicCas(&v, 0, 1));
        UNIT_ASSERT_VALUES_EQUAL(v, 0);
        UNIT_ASSERT(AtomicCas(&v, Max<intptr_t>(), 0));
        UNIT_ASSERT_VALUES_EQUAL(v, Max<intptr_t>());
    }

    inline void TestGetAndCAS() {
        TAtomic v = 0;

        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndCas(&v, 1, 0), 0);
        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndCas(&v, 2, 0), 1);
        UNIT_ASSERT_VALUES_EQUAL(v, 1);
        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndCas(&v, 0, 1), 1);
        UNIT_ASSERT_VALUES_EQUAL(v, 0);
        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndCas(&v, Max<intptr_t>(), 0), 0);
        UNIT_ASSERT_VALUES_EQUAL(v, Max<intptr_t>());
    }

    inline void TestAtomicInc1() {
        TAtomic v = 0;

        UNIT_ASSERT(AtomicAdd(v, 1));
        UNIT_ASSERT_VALUES_EQUAL(v, 1);
        UNIT_ASSERT(AtomicAdd(v, 10));
        UNIT_ASSERT_VALUES_EQUAL(v, 11);
    }

    inline void TestAtomicInc2() {
        TAtomic v = 0;

        UNIT_ASSERT(AtomicIncrement(v));
        UNIT_ASSERT_VALUES_EQUAL(v, 1);
        UNIT_ASSERT(AtomicIncrement(v));
        UNIT_ASSERT_VALUES_EQUAL(v, 2);
    }

    inline void TestAtomicGetAndInc() {
        TAtomic v = 0;

        UNIT_ASSERT_EQUAL(AtomicGetAndIncrement(v), 0);
        UNIT_ASSERT_VALUES_EQUAL(v, 1);
        UNIT_ASSERT_EQUAL(AtomicGetAndIncrement(v), 1);
        UNIT_ASSERT_VALUES_EQUAL(v, 2);
    }

    inline void TestAtomicDec() {
        TAtomic v = 2;

        UNIT_ASSERT(AtomicDecrement(v));
        UNIT_ASSERT_VALUES_EQUAL(v, 1);
        UNIT_ASSERT(!AtomicDecrement(v));
        UNIT_ASSERT_VALUES_EQUAL(v, 0);
    }

    inline void TestAtomicGetAndDec() {
        TAtomic v = 2;

        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndDecrement(v), 2);
        UNIT_ASSERT_VALUES_EQUAL(v, 1);
        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndDecrement(v), 1);
        UNIT_ASSERT_VALUES_EQUAL(v, 0);
    }

    inline void TestAtomicAdd() {
        TAtomic v = 0;

        UNIT_ASSERT_VALUES_EQUAL(AtomicAdd(v, 1), 1);
        UNIT_ASSERT_VALUES_EQUAL(AtomicAdd(v, 2), 3);
        UNIT_ASSERT_VALUES_EQUAL(AtomicAdd(v, -4), -1);
        UNIT_ASSERT_VALUES_EQUAL(v, -1);
    }

    inline void TestAtomicGetAndAdd() {
        TAtomic v = 0;

        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndAdd(v, 1), 0);
        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndAdd(v, 2), 1);
        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndAdd(v, -4), 3);
        UNIT_ASSERT_VALUES_EQUAL(v, -1);
    }

    inline void TestAtomicSub() {
        TAtomic v = 4;

        UNIT_ASSERT_VALUES_EQUAL(AtomicSub(v, 1), 3);
        UNIT_ASSERT_VALUES_EQUAL(AtomicSub(v, 2), 1);
        UNIT_ASSERT_VALUES_EQUAL(AtomicSub(v, 3), -2);
        UNIT_ASSERT_VALUES_EQUAL(v, -2);
    }

    inline void TestAtomicGetAndSub() {
        TAtomic v = 4;

        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndSub(v, 1), 4);
        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndSub(v, 2), 3);
        UNIT_ASSERT_VALUES_EQUAL(AtomicGetAndSub(v, 3), 1);
        UNIT_ASSERT_VALUES_EQUAL(v, -2);
    }

    inline void TestAtomicSwap() {
        TAtomic v = 0;

        UNIT_ASSERT_VALUES_EQUAL(AtomicSwap(&v, 3), 0);
        UNIT_ASSERT_VALUES_EQUAL(AtomicSwap(&v, 5), 3);
        UNIT_ASSERT_VALUES_EQUAL(AtomicSwap(&v, -7), 5);
        UNIT_ASSERT_VALUES_EQUAL(AtomicSwap(&v, Max<intptr_t>()), -7);
        UNIT_ASSERT_VALUES_EQUAL(v, Max<intptr_t>());
    }

    inline void TestAtomicOr() {
        TAtomic v = 0xf0;

        UNIT_ASSERT_VALUES_EQUAL(AtomicOr(v, 0x0f), 0xff);
        UNIT_ASSERT_VALUES_EQUAL(v, 0xff);
    }

    inline void TestAtomicAnd() {
        TAtomic v = 0xff;

        UNIT_ASSERT_VALUES_EQUAL(AtomicAnd(v, 0xf0), 0xf0);
        UNIT_ASSERT_VALUES_EQUAL(v, 0xf0);
    }

    inline void TestAtomicXor() {
        TAtomic v = 0x00;

        UNIT_ASSERT_VALUES_EQUAL(AtomicXor(v, 0xff), 0xff);
        UNIT_ASSERT_VALUES_EQUAL(AtomicXor(v, 0xff), 0x00);
    }

    inline void TestAtomicPtr() {
        int* p;
        AtomicSet(p, nullptr);

        UNIT_ASSERT_VALUES_EQUAL(AtomicGet(p), 0);

        int i;
        AtomicSet(p, &i);

        UNIT_ASSERT_VALUES_EQUAL(AtomicGet(p), &i);
        UNIT_ASSERT_VALUES_EQUAL(AtomicSwap(&p, nullptr), &i);
        UNIT_ASSERT(AtomicCas(&p, &i, nullptr));
    }
};

UNIT_TEST_SUITE_REGISTRATION(TAtomicTest<TAtomic>);

#ifndef _MSC_VER
// chooses type *other than* T1
template <typename T1, typename T2, typename T3>
struct TChooser {
    using TdType = T2;
};

template <typename T1, typename T2>
struct TChooser<T1, T1, T2> {
    using TdType = T2;
};

template <typename T1>
struct TChooser<T1, T1, T1> {};

    #if defined(__IOS__) && defined(_32_)
using TAltAtomic = int;
    #else
using TAltAtomic = volatile TChooser<TAtomicBase, long, long long>::TdType;
    #endif

class TTTest: public TAtomicTest<TAltAtomic> {
public:
    TString Name() const noexcept override {
        return "TAtomicTest<TAltAtomic>";
    }

    static TString StaticName() noexcept {
        return "TAtomicTest<TAltAtomic>";
    }
};

UNIT_TEST_SUITE_REGISTRATION(TTTest);

#endif