summaryrefslogtreecommitdiff
path: root/Tests/AK/TestAtomic.cpp
blob: eb7a3843ef236038f4e875324e7f4ae939479e6c (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibTest/TestCase.h>

#include <AK/Atomic.h>

TEST_CASE(construct_empty)
{
    EXPECT(Atomic<bool>().load() == false);
    EXPECT(Atomic<u32>().load() == 0);
    EXPECT(Atomic<u16>().load() == 0);
    EXPECT(Atomic<u8>().load() == 0);

    EXPECT(Atomic<u16*>().load() == nullptr);
}

TEST_CASE(construct_with_value)
{
    EXPECT(Atomic<bool>(false).load() == false);
    EXPECT(Atomic<bool>(true).load() == true);
    EXPECT(Atomic<u32>(2).load() == 2);
    EXPECT(Atomic<u16>(3).load() == 3);
    EXPECT(Atomic<u8>(4).load() == 4);

    u16 v_u16 = 0;
    EXPECT(Atomic<u16*>(&v_u16).load() == &v_u16);
}

TEST_CASE(do_exchange)
{
    Atomic<bool> a_bool(false);
    EXPECT(a_bool.exchange(true) == false);
    EXPECT(a_bool.load() == true && static_cast<bool>(a_bool) == true);

    Atomic<u32> a_u32(2);
    EXPECT(a_u32.exchange(22) == 2);
    EXPECT(a_u32.load() == 22 && static_cast<u8>(a_u32) == 22);

    Atomic<u16> a_u16(3);
    EXPECT(a_u16.exchange(33) == 3);
    EXPECT(a_u16.load() == 33 && static_cast<u8>(a_u16) == 33);

    Atomic<u8> a_u8(4);
    EXPECT(a_u8.exchange(44) == 4);
    EXPECT(a_u8.load() == 44 && static_cast<u8>(a_u8) == 44);

    u16 v_u16[6];
    Atomic<u16*> a_pu16(&v_u16[2]);
    EXPECT(a_pu16.load() == &v_u16[2] && static_cast<u16*>(a_pu16) == &v_u16[2]);
}

TEST_CASE(do_compare_exchange)
{
    Atomic<bool> a_bool(false);
    bool e_bool = true;
    EXPECT(a_bool.compare_exchange_strong(e_bool, true) == false);
    EXPECT(e_bool == false);
    EXPECT(a_bool.load() == false && static_cast<bool>(a_bool) == false);
    e_bool = false;
    EXPECT(a_bool.compare_exchange_strong(e_bool, true) == true);
    EXPECT(a_bool.load() == true && static_cast<bool>(a_bool) == true);

    Atomic<u32> a_u32(2);
    u32 e_u32 = 99;
    EXPECT(a_u32.compare_exchange_strong(e_u32, 22) == false);
    EXPECT(e_u32 == 2);
    EXPECT(a_u32.load() == 2 && static_cast<u32>(a_u32) == 2);
    e_u32 = 2;
    EXPECT(a_u32.compare_exchange_strong(e_u32, 22) == true);
    EXPECT(a_u32.load() == 22 && static_cast<u32>(a_u32) == 22);

    Atomic<u16> a_u16(3);
    u16 e_u16 = 99;
    EXPECT(a_u16.compare_exchange_strong(e_u16, 33) == false);
    EXPECT(e_u16 == 3);
    EXPECT(a_u16.load() == 3 && static_cast<u16>(a_u16) == 3);
    e_u16 = 3;
    EXPECT(a_u16.compare_exchange_strong(e_u16, 33) == true);
    EXPECT(a_u16.load() == 33 && static_cast<u16>(a_u16) == 33);

    Atomic<u8> a_u8(4);
    u8 e_u8 = 99;
    EXPECT(a_u8.compare_exchange_strong(e_u8, 44) == false);
    EXPECT(e_u8 == 4);
    EXPECT(a_u8.load() == 4 && static_cast<u16>(a_u8) == 4);
    e_u8 = 4;
    EXPECT(a_u8.compare_exchange_strong(e_u8, 44) == true);
    EXPECT(a_u8.load() == 44 && static_cast<u16>(a_u8) == 44);
}

TEST_CASE(fetch_add)
{
    Atomic<u32> a_u32(5);
    EXPECT(a_u32.fetch_add(2) == 5);
    EXPECT(a_u32.load() == 7 && static_cast<u32>(a_u32) == 7);

    Atomic<u16> a_u16(5);
    EXPECT(a_u16.fetch_add(2) == 5);
    EXPECT(a_u16.load() == 7 && static_cast<u16>(a_u16) == 7);

    Atomic<u8> a_u8(5);
    EXPECT(a_u8.fetch_add(2) == 5);
    EXPECT(a_u8.load() == 7 && static_cast<u8>(a_u8) == 7);

    u32 v_u32[6];
    Atomic<u32*> a_pu32(&v_u32[2]);
    EXPECT(a_pu32.load() == &v_u32[2] && static_cast<u32*>(a_pu32) == &v_u32[2]);
    EXPECT(a_pu32.fetch_add(2) == &v_u32[2]);
    EXPECT(a_pu32.load() == &v_u32[4] && static_cast<u32*>(a_pu32) == &v_u32[4]);
    EXPECT(a_pu32.fetch_add(-3) == &v_u32[4]);
    EXPECT(a_pu32.load() == &v_u32[1] && static_cast<u32*>(a_pu32) == &v_u32[1]);

    u16 v_u16[6];
    Atomic<u16*> a_pu16(&v_u16[2]);
    EXPECT(a_pu16.load() == &v_u16[2] && static_cast<u16*>(a_pu16) == &v_u16[2]);
    EXPECT(a_pu16.fetch_add(2) == &v_u16[2]);
    EXPECT(a_pu16.load() == &v_u16[4] && static_cast<u16*>(a_pu16) == &v_u16[4]);
    EXPECT(a_pu16.fetch_add(-3) == &v_u16[4]);
    EXPECT(a_pu16.load() == &v_u16[1] && static_cast<u16*>(a_pu16) == &v_u16[1]);

    u8 v_u8[6];
    Atomic<u8*> a_pu8(&v_u8[2]);
    EXPECT(a_pu8.load() == &v_u8[2] && static_cast<u8*>(a_pu8) == &v_u8[2]);
    EXPECT(a_pu8.fetch_add(2) == &v_u8[2]);
    EXPECT(a_pu8.load() == &v_u8[4] && static_cast<u8*>(a_pu8) == &v_u8[4]);
    EXPECT(a_pu8.fetch_add(-3) == &v_u8[4]);
    EXPECT(a_pu8.load() == &v_u8[1] && static_cast<u8*>(a_pu8) == &v_u8[1]);
}

TEST_CASE(fetch_sub)
{
    Atomic<u32> a_u32(5);
    EXPECT(a_u32.fetch_sub(2) == 5);
    EXPECT(a_u32.load() == 3 && static_cast<u32>(a_u32) == 3);

    Atomic<u16> a_u16(5);
    EXPECT(a_u16.fetch_sub(2) == 5);
    EXPECT(a_u16.load() == 3 && static_cast<u16>(a_u16) == 3);

    Atomic<u8> a_u8(5);
    EXPECT(a_u8.fetch_sub(2) == 5);
    EXPECT(a_u8.load() == 3 && static_cast<u8>(a_u8) == 3);

    u32 v_u32[6];
    Atomic<u32*> a_pu32(&v_u32[2]);
    EXPECT(a_pu32.load() == &v_u32[2] && static_cast<u32*>(a_pu32) == &v_u32[2]);
    EXPECT(a_pu32.fetch_sub(2) == &v_u32[2]);
    EXPECT(a_pu32.load() == &v_u32[0] && static_cast<u32*>(a_pu32) == &v_u32[0]);
    EXPECT(a_pu32.fetch_sub(-3) == &v_u32[0]);
    EXPECT(a_pu32.load() == &v_u32[3] && static_cast<u32*>(a_pu32) == &v_u32[3]);

    u16 v_u16[6];
    Atomic<u16*> a_pu16(&v_u16[2]);
    EXPECT(a_pu16.load() == &v_u16[2] && static_cast<u16*>(a_pu16) == &v_u16[2]);
    EXPECT(a_pu16.fetch_sub(2) == &v_u16[2]);
    EXPECT(a_pu16.load() == &v_u16[0] && static_cast<u16*>(a_pu16) == &v_u16[0]);
    EXPECT(a_pu16.fetch_sub(-3) == &v_u16[0]);
    EXPECT(a_pu16.load() == &v_u16[3] && static_cast<u16*>(a_pu16) == &v_u16[3]);

    u8 v_u8[6];
    Atomic<u8*> a_pu8(&v_u8[2]);
    EXPECT(a_pu8.load() == &v_u8[2] && static_cast<u8*>(a_pu8) == &v_u8[2]);
    EXPECT(a_pu8.fetch_sub(2) == &v_u8[2]);
    EXPECT(a_pu8.load() == &v_u8[0] && static_cast<u8*>(a_pu8) == &v_u8[0]);
    EXPECT(a_pu8.fetch_sub(-3) == &v_u8[0]);
    EXPECT(a_pu8.load() == &v_u8[3] && static_cast<u8*>(a_pu8) == &v_u8[3]);
}

TEST_CASE(fetch_inc)
{
    Atomic<u32> a_u32(5);
    EXPECT(a_u32++ == 5);
    EXPECT(a_u32.load() == 6 && a_u32 == 6);
    EXPECT(++a_u32 == 7);
    EXPECT(a_u32.load() == 7 && a_u32 == 7);
    EXPECT((a_u32 += 2) == 9);
    EXPECT(a_u32.load() == 9 && a_u32 == 9);

    Atomic<u16> a_u16(5);
    EXPECT(a_u16++ == 5);
    EXPECT(a_u16.load() == 6 && a_u16 == 6);
    EXPECT(++a_u16 == 7);
    EXPECT(a_u16.load() == 7 && a_u16 == 7);
    EXPECT((a_u16 += 2) == 9);
    EXPECT(a_u16.load() == 9 && a_u16 == 9);

    Atomic<u8> a_u8(5);
    EXPECT(a_u8++ == 5);
    EXPECT(a_u8.load() == 6 && a_u8 == 6);
    EXPECT(++a_u8 == 7);
    EXPECT(a_u8.load() == 7 && a_u8 == 7);
    EXPECT((a_u8 += 2) == 9);
    EXPECT(a_u8.load() == 9 && a_u8 == 9);

    u32 v_u32[8];
    Atomic<u32*> a_pu32(&v_u32[2]);
    EXPECT(a_pu32++ == &v_u32[2]);
    EXPECT(a_pu32.load() == &v_u32[3] && a_pu32 == &v_u32[3]);
    EXPECT(++a_pu32 == &v_u32[4]);
    EXPECT(a_pu32.load() == &v_u32[4] && a_pu32 == &v_u32[4]);
    EXPECT((a_pu32 += 2) == &v_u32[6]);
    EXPECT(a_pu32.load() == &v_u32[6] && a_pu32 == &v_u32[6]);

    u16 v_u16[8];
    Atomic<u16*> a_pu16(&v_u16[2]);
    EXPECT(a_pu16++ == &v_u16[2]);
    EXPECT(a_pu16.load() == &v_u16[3] && a_pu16 == &v_u16[3]);
    EXPECT(++a_pu16 == &v_u16[4]);
    EXPECT(a_pu16.load() == &v_u16[4] && a_pu16 == &v_u16[4]);
    EXPECT((a_pu16 += 2) == &v_u16[6]);
    EXPECT(a_pu16.load() == &v_u16[6] && a_pu16 == &v_u16[6]);

    u8 v_u8[8];
    Atomic<u8*> a_pu8(&v_u8[2]);
    EXPECT(a_pu8++ == &v_u8[2]);
    EXPECT(a_pu8.load() == &v_u8[3] && a_pu8 == &v_u8[3]);
    EXPECT(++a_pu8 == &v_u8[4]);
    EXPECT(a_pu8.load() == &v_u8[4] && a_pu8 == &v_u8[4]);
    EXPECT((a_pu8 += 2) == &v_u8[6]);
    EXPECT(a_pu8.load() == &v_u8[6] && a_pu8 == &v_u8[6]);
}

TEST_CASE(fetch_dec)
{
    Atomic<u32> a_u32(5);
    EXPECT(a_u32-- == 5);
    EXPECT(a_u32.load() == 4 && a_u32 == 4);
    EXPECT(--a_u32 == 3);
    EXPECT(a_u32.load() == 3 && a_u32 == 3);
    EXPECT((a_u32 -= 2) == 1);
    EXPECT(a_u32.load() == 1 && a_u32 == 1);

    Atomic<u16> a_u16(5);
    EXPECT(a_u16-- == 5);
    EXPECT(a_u16.load() == 4 && a_u16 == 4);
    EXPECT(--a_u16 == 3);
    EXPECT(a_u16.load() == 3 && a_u16 == 3);
    EXPECT((a_u16 -= 2) == 1);
    EXPECT(a_u16.load() == 1 && a_u16 == 1);

    Atomic<u8> a_u8(5);
    EXPECT(a_u8-- == 5);
    EXPECT(a_u8.load() == 4 && a_u8 == 4);
    EXPECT(--a_u8 == 3);
    EXPECT(a_u8.load() == 3 && a_u8 == 3);
    EXPECT((a_u8 -= 2) == 1);
    EXPECT(a_u8.load() == 1 && a_u8 == 1);

    u32 v_u32[8];
    Atomic<u32*> a_pu32(&v_u32[7]);
    EXPECT(a_pu32-- == &v_u32[7]);
    EXPECT(a_pu32.load() == &v_u32[6] && a_pu32 == &v_u32[6]);
    EXPECT(--a_pu32 == &v_u32[5]);
    EXPECT(a_pu32.load() == &v_u32[5] && a_pu32 == &v_u32[5]);
    EXPECT((a_pu32 -= 2) == &v_u32[3]);
    EXPECT(a_pu32.load() == &v_u32[3] && a_pu32 == &v_u32[3]);

    u16 v_u16[8];
    Atomic<u16*> a_pu16(&v_u16[7]);
    EXPECT(a_pu16-- == &v_u16[7]);
    EXPECT(a_pu16.load() == &v_u16[6] && a_pu16 == &v_u16[6]);
    EXPECT(--a_pu16 == &v_u16[5]);
    EXPECT(a_pu16.load() == &v_u16[5] && a_pu16 == &v_u16[5]);
    EXPECT((a_pu16 -= 2) == &v_u16[3]);
    EXPECT(a_pu16.load() == &v_u16[3] && a_pu16 == &v_u16[3]);

    u8 v_u8[8];
    Atomic<u8*> a_pu8(&v_u8[7]);
    EXPECT(a_pu8-- == &v_u8[7]);
    EXPECT(a_pu8.load() == &v_u8[6] && a_pu8 == &v_u8[6]);
    EXPECT(--a_pu8 == &v_u8[5]);
    EXPECT(a_pu8.load() == &v_u8[5] && a_pu8 == &v_u8[5]);
    EXPECT((a_pu8 -= 2) == &v_u8[3]);
    EXPECT(a_pu8.load() == &v_u8[3] && a_pu8 == &v_u8[3]);
}

TEST_CASE(fetch_and)
{
    Atomic<u32> a_u32(0xdeadbeef);
    EXPECT(a_u32.fetch_and(0x8badf00d) == 0xdeadbeef);
    EXPECT(a_u32.load() == 0x8aadb00d && static_cast<u32>(a_u32) == 0x8aadb00d);
    a_u32 = 0xdeadbeef;
    EXPECT((a_u32 &= 0x8badf00d) == 0x8aadb00d);

    Atomic<u16> a_u16(0xbeef);
    EXPECT(a_u16.fetch_and(0xf00d) == 0xbeef);
    EXPECT(a_u16.load() == 0xb00d && static_cast<u16>(a_u16) == 0xb00d);
    a_u16 = 0xbeef;
    EXPECT((a_u16 &= 0xf00d) == 0xb00d);

    Atomic<u8> a_u8(0xef);
    EXPECT(a_u8.fetch_and(0x0d) == 0xef);
    EXPECT(a_u8.load() == 0x0d && static_cast<u8>(a_u8) == 0x0d);
    a_u8 = 0xef;
    EXPECT((a_u8 &= 0x0d) == 0x0d);
}

TEST_CASE(fetch_or)
{
    Atomic<u32> a_u32(0xaadb00d);
    EXPECT(a_u32.fetch_or(0xdeadbeef) == 0xaadb00d);
    EXPECT(a_u32.load() == 0xdeadbeef && static_cast<u32>(a_u32) == 0xdeadbeef);
    a_u32 = 0xaadb00d;
    EXPECT((a_u32 |= 0xdeadbeef) == 0xdeadbeef);

    Atomic<u16> a_u16(0xb00d);
    EXPECT(a_u16.fetch_or(0xbeef) == 0xb00d);
    EXPECT(a_u16.load() == 0xbeef && static_cast<u16>(a_u16) == 0xbeef);
    a_u16 = 0xb00d;
    EXPECT((a_u16 |= 0xbeef) == 0xbeef);

    Atomic<u8> a_u8(0x0d);
    EXPECT(a_u8.fetch_or(0xef) == 0x0d);
    EXPECT(a_u8.load() == 0xef && static_cast<u8>(a_u8) == 0xef);
    a_u8 = 0x0d;
    EXPECT((a_u8 |= 0xef) == 0xef);
}

TEST_CASE(fetch_xor)
{
    Atomic<u32> a_u32(0x55004ee2);
    EXPECT(a_u32.fetch_xor(0xdeadbeef) == 0x55004ee2);
    EXPECT(a_u32.load() == 0x8badf00d && static_cast<u32>(a_u32) == 0x8badf00d);
    a_u32 = 0x55004ee2;
    EXPECT((a_u32 ^= 0xdeadbeef) == 0x8badf00d);

    Atomic<u16> a_u16(0x4ee2);
    EXPECT(a_u16.fetch_xor(0xbeef) == 0x4ee2);
    EXPECT(a_u16.load() == 0xf00d && static_cast<u16>(a_u16) == 0xf00d);
    a_u16 = 0x4ee2;
    EXPECT((a_u16 ^= 0xbeef) == 0xf00d);

    Atomic<u8> a_u8(0xe2);
    EXPECT(a_u8.fetch_xor(0xef) == 0xe2);
    EXPECT(a_u8.load() == 0x0d && static_cast<u8>(a_u8) == 0x0d);
    a_u8 = 0xe2;
    EXPECT((a_u8 ^= 0xef) == 0x0d);
}