|
|
@@ -20,7 +20,7 @@
|
|
|
*/
|
|
|
static void test_atomic_load_store_int32(void)
|
|
|
{
|
|
|
- constexpr int kRound = 10000000;
|
|
|
+ constexpr int kRound = 10000000;
|
|
|
std::atomic<int32_t> thread_count(0);
|
|
|
std::atomic<int32_t> count(100);
|
|
|
uassert_int_equal(count.load(), 100);
|
|
|
@@ -56,7 +56,7 @@ static void test_atomic_load_store_int32(void)
|
|
|
*/
|
|
|
static void test_atomic_load_store_int64(void)
|
|
|
{
|
|
|
- constexpr int kRound = 10000000;
|
|
|
+ constexpr int kRound = 10000000;
|
|
|
std::atomic<int64_t> thread_count(0);
|
|
|
std::atomic<int64_t> count(100);
|
|
|
uassert_int_equal(count.load(), 100);
|
|
|
@@ -112,7 +112,7 @@ static void test_atomic_basic_int32(void)
|
|
|
val.fetch_and(14);
|
|
|
uassert_int_equal(val.load(), 10);
|
|
|
|
|
|
- val.fetch_or(11); //1010
|
|
|
+ val.fetch_or(11); /* 1010 */
|
|
|
uassert_int_equal(val.load(), 11);
|
|
|
|
|
|
val.fetch_xor(4);
|
|
|
@@ -121,9 +121,9 @@ static void test_atomic_basic_int32(void)
|
|
|
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);
|
|
|
+ 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);
|
|
|
@@ -159,7 +159,7 @@ static void test_atomic_basic_int64(void)
|
|
|
val.fetch_and(14);
|
|
|
uassert_int_equal(val.load(), 10);
|
|
|
|
|
|
- val.fetch_or(11); //1010
|
|
|
+ val.fetch_or(11); /* 1010 */
|
|
|
uassert_int_equal(val.load(), 11);
|
|
|
|
|
|
val.fetch_xor(4);
|
|
|
@@ -168,9 +168,9 @@ static void test_atomic_basic_int64(void)
|
|
|
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);
|
|
|
+ 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);
|
|
|
@@ -191,7 +191,7 @@ static void test_atomic_bool(void)
|
|
|
flag.exchange(false);
|
|
|
uassert_false(flag.load());
|
|
|
bool expected = false;
|
|
|
- bool desired = true;
|
|
|
+ bool desired = true;
|
|
|
uassert_true(flag.compare_exchange_strong(expected, desired));
|
|
|
uassert_true(flag.load());
|
|
|
}
|
|
|
@@ -201,7 +201,7 @@ static void test_atomic_bool(void)
|
|
|
*/
|
|
|
static void test_atomic_pointer(void)
|
|
|
{
|
|
|
- int a = 1, b = 2;
|
|
|
+ int a = 1, b = 2;
|
|
|
std::atomic<int *> ptr(&a);
|
|
|
ptr.store(&b);
|
|
|
uassert_int_equal(*ptr.load(), 2);
|
|
|
@@ -217,7 +217,7 @@ static void test_memory_order(void)
|
|
|
{
|
|
|
std::atomic<int> x(0);
|
|
|
std::atomic<int> y(0);
|
|
|
- // Simple test for memory order
|
|
|
+ /* 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);
|
|
|
@@ -230,25 +230,36 @@ static void test_memory_order(void)
|
|
|
static void test_compare_exchange_weak(void)
|
|
|
{
|
|
|
std::atomic<int> val(1);
|
|
|
- int expected = 1;
|
|
|
- int desired = 2;
|
|
|
+ int expected = 1;
|
|
|
+ int desired = 2;
|
|
|
while (!val.compare_exchange_weak(expected, desired))
|
|
|
{
|
|
|
- expected = 1; // reset
|
|
|
+ /* 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 */
|
|
|
@@ -268,4 +279,6 @@ static void testcase(void)
|
|
|
/* Test compare_exchange_weak operation */
|
|
|
UTEST_UNIT_RUN(test_compare_exchange_weak);
|
|
|
}
|
|
|
-UTEST_TC_EXPORT(testcase, "components.libc.cpp.atomic_tc", utest_tc_init, utest_tc_cleanup, 10);
|
|
|
+
|
|
|
+/* 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);
|