1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "shared.rsh"
18
19// Testing atomic operations
20static bool testUMax(uint32_t dst, uint32_t src) {
21    bool failed = false;
22    uint32_t old = dst;
23    uint32_t expect = (dst > src ? dst : src);
24    uint32_t ret = rsAtomicMax(&dst, src);
25    _RS_ASSERT(old == ret);
26    _RS_ASSERT(dst == expect);
27    return failed;
28}
29
30static bool testUMin(uint32_t dst, uint32_t src) {
31    bool failed = false;
32    uint32_t old = dst;
33    uint32_t expect = (dst < src ? dst : src);
34    uint32_t ret = rsAtomicMin(&dst, src);
35    _RS_ASSERT(old == ret);
36    _RS_ASSERT(dst == expect);
37    return failed;
38}
39
40static bool testUCas(uint32_t dst, uint32_t cmp, uint32_t swp) {
41    bool failed = false;
42    uint32_t old = dst;
43    uint32_t expect = (dst == cmp ? swp : dst);
44    uint32_t ret = rsAtomicCas(&dst, cmp, swp);
45    _RS_ASSERT(old == ret);
46    _RS_ASSERT(dst == expect);
47    return failed;
48}
49
50static bool test_atomics() {
51    bool failed = false;
52
53    failed |= testUMax(5, 6);
54    failed |= testUMax(6, 5);
55    failed |= testUMax(5, 0xf0000006);
56    failed |= testUMax(0xf0000006, 5);
57
58    failed |= testUMin(5, 6);
59    failed |= testUMin(6, 5);
60    failed |= testUMin(5, 0xf0000006);
61    failed |= testUMin(0xf0000006, 5);
62
63    failed |= testUCas(4, 4, 5);
64    failed |= testUCas(4, 5, 5);
65    failed |= testUCas(5, 5, 4);
66    failed |= testUCas(5, 4, 4);
67    failed |= testUCas(0xf0000004, 0xf0000004, 0xf0000005);
68    failed |= testUCas(0xf0000004, 0xf0000005, 0xf0000005);
69    failed |= testUCas(0xf0000005, 0xf0000005, 0xf0000004);
70    failed |= testUCas(0xf0000005, 0xf0000004, 0xf0000004);
71
72    if (failed) {
73        rsDebug("test_atomics FAILED", 0);
74    }
75    else {
76        rsDebug("test_atomics PASSED", 0);
77    }
78
79    return failed;
80}
81
82void atomic_test() {
83    bool failed = false;
84    failed |= test_atomics();
85
86    if (failed) {
87        rsSendToClientBlocking(RS_MSG_TEST_FAILED);
88    }
89    else {
90        rsSendToClientBlocking(RS_MSG_TEST_PASSED);
91    }
92}
93
94