1#include "shared.rsh"
2
3// Testing atomic operations
4static bool testUMax(uint32_t dst, uint32_t src) {
5    bool failed = false;
6    uint32_t old = dst;
7    uint32_t expect = (dst > src ? dst : src);
8    uint32_t ret = rsAtomicMax(&dst, src);
9    _RS_ASSERT(old == ret);
10    _RS_ASSERT(dst == expect);
11    return failed;
12}
13
14static bool testUMin(uint32_t dst, uint32_t src) {
15    bool failed = false;
16    uint32_t old = dst;
17    uint32_t expect = (dst < src ? dst : src);
18    uint32_t ret = rsAtomicMin(&dst, src);
19    _RS_ASSERT(old == ret);
20    _RS_ASSERT(dst == expect);
21    return failed;
22}
23
24static bool testUCas(uint32_t dst, uint32_t cmp, uint32_t swp) {
25    bool failed = false;
26    uint32_t old = dst;
27    uint32_t expect = (dst == cmp ? swp : dst);
28    uint32_t ret = rsAtomicCas(&dst, cmp, swp);
29    _RS_ASSERT(old == ret);
30    _RS_ASSERT(dst == expect);
31    return failed;
32}
33
34static bool test_atomics() {
35    bool failed = false;
36
37    failed |= testUMax(5, 6);
38    failed |= testUMax(6, 5);
39    failed |= testUMax(5, 0xf0000006);
40    failed |= testUMax(0xf0000006, 5);
41
42    failed |= testUMin(5, 6);
43    failed |= testUMin(6, 5);
44    failed |= testUMin(5, 0xf0000006);
45    failed |= testUMin(0xf0000006, 5);
46
47    failed |= testUCas(4, 4, 5);
48    failed |= testUCas(4, 5, 5);
49    failed |= testUCas(5, 5, 4);
50    failed |= testUCas(5, 4, 4);
51    failed |= testUCas(0xf0000004, 0xf0000004, 0xf0000005);
52    failed |= testUCas(0xf0000004, 0xf0000005, 0xf0000005);
53    failed |= testUCas(0xf0000005, 0xf0000005, 0xf0000004);
54    failed |= testUCas(0xf0000005, 0xf0000004, 0xf0000004);
55
56    if (failed) {
57        rsDebug("test_atomics FAILED", 0);
58    }
59    else {
60        rsDebug("test_atomics PASSED", 0);
61    }
62
63    return failed;
64}
65
66void atomic_test() {
67    bool failed = false;
68    failed |= test_atomics();
69
70    if (failed) {
71        rsSendToClientBlocking(RS_MSG_TEST_FAILED);
72    }
73    else {
74        rsSendToClientBlocking(RS_MSG_TEST_PASSED);
75    }
76}
77
78