summaryrefslogtreecommitdiff
path: root/components/libc/cplusplus/utest/tc_atomic.cpp
blob: ae7c3b8841063b8bf1752dcf0aeb390b1365f0f2 (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
/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-09-19     Rbb666       the first version
 */

#include <rtthread.h>
#include "utest.h"
#include <thread>
#include <atomic>
#include <cstdint>

/**
 * @brief Test load and store operations for int32_t atomic variables in multi-threaded environment.
 *        Verifies atomicity by performing increment and decrement operations concurrently.
 */
static void test_atomic_load_store_int32(void)
{
    constexpr int kRound = 10000000;
    std::atomic<int32_t> thread_count(0);
    std::atomic<int32_t> count(100);
    uassert_int_equal(count.load(), 100);

    auto func1 = [&]() mutable {
        for (int i = 0; i < kRound; ++i)
        {
            ++count;
        }
        ++thread_count;
    };

    auto func2 = [&]() mutable {
        for (int i = 0; i < kRound; ++i)
        {
            --count;
        }
        ++thread_count;
    };

    std::thread t1(func1);
    std::thread t2(func2);
    t1.join();
    t2.join();

    uassert_int_equal(count.load(), 100);
    uassert_int_equal(thread_count.load(), 2);
}

/**
 * @brief Test load and store operations for int64_t atomic variables in multi-threaded environment.
 *        Verifies atomicity by performing increment and decrement operations concurrently.
 */
static void test_atomic_load_store_int64(void)
{
    constexpr int kRound = 10000000;
    std::atomic<int64_t> thread_count(0);
    std::atomic<int64_t> count(100);
    uassert_int_equal(count.load(), 100);

    auto func1 = [&]() mutable {
        for (int i = 0; i < kRound; ++i)
        {
            ++count;
        }
        ++thread_count;
    };

    auto func2 = [&]() mutable {
        for (int i = 0; i < kRound; ++i)
        {
            --count;
        }
        ++thread_count;
    };

    std::thread t1(func1);
    std::thread t2(func2);
    t1.join();
    t2.join();

    uassert_int_equal(count.load(), 100);
    uassert_int_equal(thread_count.load(), 2);
}

/**
 * @brief Test basic atomic operations for int32_t, including load, store, fetch_add, fetch_sub,
 *        fetch_and, fetch_or, fetch_xor, exchange, and compare_exchange_strong.
 */
static void test_atomic_basic_int32(void)
{
    std::atomic<int32_t> val;
    val = 10;
    uassert_int_equal(val.load(), 10);

    val++;
    uassert_int_equal(val.load(), 11);

    val--;
    uassert_int_equal(val.load(), 10);

    auto a = val.load();
    val.store(a);
    val.fetch_add(1);
    uassert_int_equal(val.load(), 11);
    val.fetch_sub(1);
    uassert_int_equal(val.load(), 10);

    val.fetch_and(14);
    uassert_int_equal(val.load(), 10);

    val.fetch_or(11); /* 1010 */
    uassert_int_equal(val.load(), 11);

    val.fetch_xor(4);
    uassert_int_equal(val.load(), 15);

    val.exchange(1);
    uassert_int_equal(val.load(), 1);

    int32_t x = 2;
    int32_t y = 3;
    bool exchanged = val.compare_exchange_strong(x, y);
    uassert_false(exchanged);
    uassert_int_equal(val.load(), 1);
    uassert_int_equal(x, 1);
    exchanged = val.compare_exchange_strong(x, y);
    uassert_true(exchanged);
    uassert_int_equal(val.load(), 3);
    uassert_int_equal(x, 1);
}

/**
 * @brief Test basic atomic operations for int64_t, including load, store, fetch_add, fetch_sub,
 *        fetch_and, fetch_or, fetch_xor, exchange, and compare_exchange_strong.
 */
static void test_atomic_basic_int64(void)
{
    std::atomic<int64_t> val;
    val = 10;
    uassert_int_equal(val.load(), 10);

    val++;
    uassert_int_equal(val.load(), 11);

    val--;
    uassert_int_equal(val.load(), 10);

    auto a = val.load();
    val.store(a);
    val.fetch_add(1);
    uassert_int_equal(val.load(), 11);
    val.fetch_sub(1);
    uassert_int_equal(val.load(), 10);

    val.fetch_and(14);
    uassert_int_equal(val.load(), 10);

    val.fetch_or(11); /* 1010 */
    uassert_int_equal(val.load(), 11);

    val.fetch_xor(4);
    uassert_int_equal(val.load(), 15);

    val.exchange(1);
    uassert_int_equal(val.load(), 1);

    int64_t x = 2;
    int64_t y = 3;
    bool exchanged = val.compare_exchange_strong(x, y);
    uassert_false(exchanged);
    uassert_int_equal(val.load(), 1);
    uassert_int_equal(x, 1);
    exchanged = val.compare_exchange_strong(x, y);
    uassert_true(exchanged);
    uassert_int_equal(val.load(), 3);
    uassert_int_equal(x, 1);
}

/**
 * @brief Test atomic operations for bool type, including store, load, exchange, and compare_exchange_strong.
 */
static void test_atomic_bool(void)
{
    std::atomic<bool> flag(false);
    flag.store(true);
    uassert_true(flag.load());
    flag.exchange(false);
    uassert_false(flag.load());
    bool expected = false;
    bool desired = true;
    uassert_true(flag.compare_exchange_strong(expected, desired));
    uassert_true(flag.load());
}

/**
 * @brief Test atomic operations for pointer type (int*), including store, load, and exchange.
 */
static void test_atomic_pointer(void)
{
    int a = 1, b = 2;
    std::atomic<int *> ptr(&a);
    ptr.store(&b);
    uassert_int_equal(*ptr.load(), 2);
    int *old = ptr.exchange(&a);
    uassert_ptr_equal(old, &b);
    uassert_int_equal(*ptr.load(), 1);
}

/**
 * @brief Test memory ordering constraints using memory_order_release and memory_order_acquire.
 */
static void test_memory_order(void)
{
    std::atomic<int> x(0);
    std::atomic<int> y(0);
    /* Simple test for memory order */
    x.store(1, std::memory_order_release);
    y.store(2, std::memory_order_release);
    uassert_int_equal(x.load(std::memory_order_acquire), 1);
    uassert_int_equal(y.load(std::memory_order_acquire), 2);
}

/**
 * @brief Test compare_exchange_weak operation, which may fail spuriously and requires looping.
 */
static void test_compare_exchange_weak(void)
{
    std::atomic<int> val(1);
    int expected = 1;
    int desired = 2;
    while (!val.compare_exchange_weak(expected, desired))
    {
        /* Reset */
        expected = 1; 
    }
    uassert_int_equal(val.load(), 2);
}
/**
 * @brief Test case initialization function.
 * @return RT_EOK on success.
 */
static rt_err_t utest_tc_init(void)
{
    return RT_EOK;
}

/**
 * @brief Test case cleanup function.
 * @return RT_EOK on success.
 */
static rt_err_t utest_tc_cleanup(void)
{
    return RT_EOK;
}

/**
 * @brief Main test case function that runs the test.
 */
static void testcase(void)
{
    /* Test load and store operations for int32_t atomic variables in multi-threaded environment */
    UTEST_UNIT_RUN(test_atomic_load_store_int32);
    /* Test load and store operations for int64_t atomic variables in multi-threaded environment */
    UTEST_UNIT_RUN(test_atomic_load_store_int64);
    /* Test basic atomic operations for int32_t */
    UTEST_UNIT_RUN(test_atomic_basic_int32);
    /* Test basic atomic operations for int64_t */
    UTEST_UNIT_RUN(test_atomic_basic_int64);
    /* Test atomic operations for bool type */
    UTEST_UNIT_RUN(test_atomic_bool);
    /* Test atomic operations for pointer type */
    UTEST_UNIT_RUN(test_atomic_pointer);
    /* Test memory ordering constraints */
    UTEST_UNIT_RUN(test_memory_order);
    /* Test compare_exchange_weak operation */
    UTEST_UNIT_RUN(test_compare_exchange_weak);
}

/* Export the test case with initialization and cleanup functions and a timeout of 10 ticks. */
UTEST_TC_EXPORT(testcase, "components.libc.cpp.atomic_tc", utest_tc_init, utest_tc_cleanup, 10);