From 0b5d5b9f3330ff2c5ae4e324ee6a3c770450ab42 Mon Sep 17 00:00:00 2001 From: Rbb666 Date: Fri, 19 Sep 2025 11:31:20 +0800 Subject: [PATCH 1/6] [utest][cpp]rename thread_tc.cpp to tc_thread.cpp. --- examples/utest/testcases/cpp11/SConscript | 11 ++++++----- .../testcases/cpp11/{thread_tc.cpp => tc_thread.cpp} | 0 2 files changed, 6 insertions(+), 5 deletions(-) rename examples/utest/testcases/cpp11/{thread_tc.cpp => tc_thread.cpp} (100%) diff --git a/examples/utest/testcases/cpp11/SConscript b/examples/utest/testcases/cpp11/SConscript index cf92d755b08..706d689aeb0 100644 --- a/examples/utest/testcases/cpp11/SConscript +++ b/examples/utest/testcases/cpp11/SConscript @@ -2,12 +2,13 @@ Import('rtconfig') from building import * cwd = GetCurrentDir() -src = Split(''' -thread_tc.cpp -''') - +src = [] CPPPATH = [cwd] -group = DefineGroup('utestcases', src, depend = ['UTEST_CPP11_THREAD_TC'], CPPPATH = CPPPATH) +if GetDepend('RT_UTEST_USING_ALL_CASES') or GetDepend('UTEST_CPP11_THREAD_TC'): + src += Glob('tc_*.cpp') + +group = DefineGroup('utestcases', src, depend = ['RT_USING_UTESTCASES', 'RT_USING_CPLUSPLUS'], CPPPATH = CPPPATH) Return('group') + diff --git a/examples/utest/testcases/cpp11/thread_tc.cpp b/examples/utest/testcases/cpp11/tc_thread.cpp similarity index 100% rename from examples/utest/testcases/cpp11/thread_tc.cpp rename to examples/utest/testcases/cpp11/tc_thread.cpp From 9560b81610ab7dfff1495092959b8a8a08fa2db7 Mon Sep 17 00:00:00 2001 From: Rbb666 Date: Mon, 22 Sep 2025 15:31:52 +0800 Subject: [PATCH 2/6] [utest][cpp]add cpp-atomic testcase. --- examples/utest/testcases/cpp11/tc_atomic.cpp | 271 +++++++++++++++++++ 1 file changed, 271 insertions(+) create mode 100644 examples/utest/testcases/cpp11/tc_atomic.cpp diff --git a/examples/utest/testcases/cpp11/tc_atomic.cpp b/examples/utest/testcases/cpp11/tc_atomic.cpp new file mode 100644 index 00000000000..4f9ca4b4d82 --- /dev/null +++ b/examples/utest/testcases/cpp11/tc_atomic.cpp @@ -0,0 +1,271 @@ +/* + * 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 +#include "utest.h" +#include +#include +#include + +/** + * @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 thread_count(0); + std::atomic 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 thread_count(0); + std::atomic 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 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 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 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 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 x(0); + std::atomic 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 val(1); + int expected = 1; + int desired = 2; + while (!val.compare_exchange_weak(expected, desired)) + { + expected = 1; // reset + } + uassert_int_equal(val.load(), 2); +} + +static rt_err_t utest_tc_init(void) +{ + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +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); +} +UTEST_TC_EXPORT(testcase, "testcases.cpp11.atomic_tc", utest_tc_init, utest_tc_cleanup, 10); From 7ce1bab4ef20470a300a42ee3978f0c49beb9b84 Mon Sep 17 00:00:00 2001 From: Rbb666 Date: Mon, 22 Sep 2025 15:32:07 +0800 Subject: [PATCH 3/6] [utest][cpp]add cpp-mutex testcase. --- examples/utest/testcases/cpp11/tc_mutex.cpp | 120 ++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 examples/utest/testcases/cpp11/tc_mutex.cpp diff --git a/examples/utest/testcases/cpp11/tc_mutex.cpp b/examples/utest/testcases/cpp11/tc_mutex.cpp new file mode 100644 index 00000000000..12974fa7277 --- /dev/null +++ b/examples/utest/testcases/cpp11/tc_mutex.cpp @@ -0,0 +1,120 @@ +/* + * 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 +#include "utest.h" +#include +#include +#include + +/** + * @brief Test basic mutex operations with lock_guard. + */ +static void test_mutex(void) +{ + std::mutex m; + int count = 0; + auto func = [&]() mutable { + std::lock_guard lock(m); + for (int i = 0; i < 1000; ++i) + { + ++count; + } + }; + + std::thread t1(func); + std::thread t2(func); + + t1.join(); + t2.join(); + + uassert_int_equal(count, 2000); +} + +/** + * @brief Test recursive mutex allowing multiple locks by the same thread. + */ +static void test_recursive_mutex(void) +{ + std::recursive_mutex rm; + int count = 0; + auto func = [&]() mutable { + std::lock_guard lock1(rm); + std::lock_guard lock2(rm); + for (int i = 0; i < 1000; ++i) + { + ++count; + } + }; + + std::thread t1(func); + std::thread t2(func); + + t1.join(); + t2.join(); + + if (count != 2000) + { + uassert_false(true); + } + uassert_true(true); +} + +/** + * @brief Test try_lock on mutex. + */ +static void test_try_lock(void) +{ + std::mutex m; + if (m.try_lock()) + { + m.unlock(); + uassert_true(true); + } + else + { + uassert_false(true); + } +} + +/** + * @brief Test locking multiple mutexes with std::lock. + */ +static void test_lock_multiple(void) +{ + std::mutex m1, m2; + std::lock(m1, m2); + m1.unlock(); + m2.unlock(); + uassert_true(true); +} + +static rt_err_t utest_tc_init(void) +{ + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ + /* Test basic mutex operations with lock_guard */ + UTEST_UNIT_RUN(test_mutex); + /* Test recursive mutex allowing multiple locks by the same thread */ + UTEST_UNIT_RUN(test_recursive_mutex); + /* Test try_lock on mutex */ + UTEST_UNIT_RUN(test_try_lock); + /* Test locking multiple mutexes with std::lock */ + UTEST_UNIT_RUN(test_lock_multiple); +} +UTEST_TC_EXPORT(testcase, "testcases.cpp11.mutex_tc", utest_tc_init, utest_tc_cleanup, 10); From 73ffd1e38c3012108de5640be3ea89a46f63e9c0 Mon Sep 17 00:00:00 2001 From: Rbb666 Date: Mon, 22 Sep 2025 15:32:27 +0800 Subject: [PATCH 4/6] [utest][cpp]add cpp-auto testcase. --- examples/utest/testcases/cpp11/tc_auto.cpp | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/utest/testcases/cpp11/tc_auto.cpp diff --git a/examples/utest/testcases/cpp11/tc_auto.cpp b/examples/utest/testcases/cpp11/tc_auto.cpp new file mode 100644 index 00000000000..370d89f0204 --- /dev/null +++ b/examples/utest/testcases/cpp11/tc_auto.cpp @@ -0,0 +1,64 @@ +/* + * 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 +#include "utest.h" +#include + +/** + * @brief Test auto keyword. + */ +static void test_auto(void) +{ + auto x = 42; + auto y = 3.14; + if (x != 42 || y != 3.14) + { + uassert_false(true); + } + uassert_true(true); +} + +/** + * @brief Test range-based for loop. + */ +static void test_range_for(void) +{ + std::vector v = {1, 2, 3, 4, 5}; + int sum = 0; + for (auto i : v) + { + sum += i; + } + if (sum != 15) + { + uassert_false(true); + } + uassert_true(true); +} + +static rt_err_t utest_tc_init(void) +{ + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ + /* Test auto keyword */ + UTEST_UNIT_RUN(test_auto); + /* Test range-based for loop */ + UTEST_UNIT_RUN(test_range_for); +} +UTEST_TC_EXPORT(testcase, "testcases.cpp11.auto_tc", utest_tc_init, utest_tc_cleanup, 10); From 5e2b5781ad958f6812dae58a3ef88541680a3626 Mon Sep 17 00:00:00 2001 From: Rbb666 Date: Mon, 22 Sep 2025 15:32:41 +0800 Subject: [PATCH 5/6] [utest][cpp]add cpp-lambda testcase. --- examples/utest/testcases/cpp11/tc_lambda.cpp | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 examples/utest/testcases/cpp11/tc_lambda.cpp diff --git a/examples/utest/testcases/cpp11/tc_lambda.cpp b/examples/utest/testcases/cpp11/tc_lambda.cpp new file mode 100644 index 00000000000..1e3f70bd9b2 --- /dev/null +++ b/examples/utest/testcases/cpp11/tc_lambda.cpp @@ -0,0 +1,58 @@ +/* + * 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 +#include "utest.h" + +/** + * @brief Test basic lambda expression. + */ +static void test_lambda_basic(void) +{ + auto lambda = []() { return 42; }; + if (lambda() != 42) + { + uassert_false(true); + } + uassert_true(true); +} + +/** + * @brief Test lambda with capture. + */ +static void test_lambda_capture(void) +{ + int x = 10; + auto lambda = [x]() { return x * 2; }; + if (lambda() != 20) + { + uassert_false(true); + } + uassert_true(true); +} + +static rt_err_t utest_tc_init(void) +{ + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ + /* Test basic lambda expression */ + UTEST_UNIT_RUN(test_lambda_basic); + /* Test lambda with capture */ + UTEST_UNIT_RUN(test_lambda_capture); +} +UTEST_TC_EXPORT(testcase, "testcases.cpp11.lambda_tc", utest_tc_init, utest_tc_cleanup, 10); From 473d9e0f4684c9871801c663bb23ae6558f3c46c Mon Sep 17 00:00:00 2001 From: Rbb666 Date: Mon, 22 Sep 2025 15:33:02 +0800 Subject: [PATCH 6/6] [utest][cpp]add cpp-smartptr testcase. --- .../utest/testcases/cpp11/tc_smartptr.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/utest/testcases/cpp11/tc_smartptr.cpp diff --git a/examples/utest/testcases/cpp11/tc_smartptr.cpp b/examples/utest/testcases/cpp11/tc_smartptr.cpp new file mode 100644 index 00000000000..22064bbc173 --- /dev/null +++ b/examples/utest/testcases/cpp11/tc_smartptr.cpp @@ -0,0 +1,55 @@ +/* + * 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 +#include "utest.h" +#include + +/** + * @brief Test unique_ptr basic operations. + */ +static void test_unique_ptr(void) +{ + std::unique_ptr p(new int(42)); + uassert_int_equal(*p, 42); + *p = 24; + uassert_int_equal(*p, 24); +} + +/** + * @brief Test shared_ptr basic operations. + */ +static void test_shared_ptr(void) +{ + std::shared_ptr p1(new int(42)); + std::shared_ptr p2 = p1; + uassert_int_equal(*p1, 42); + uassert_int_equal(*p2, 42); + uassert_int_equal(p1.use_count(), 2); +} + +static rt_err_t utest_tc_init(void) +{ + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ + /* Test unique_ptr basic operations */ + UTEST_UNIT_RUN(test_unique_ptr); + /* Test shared_ptr basic operations */ + UTEST_UNIT_RUN(test_shared_ptr); +} +UTEST_TC_EXPORT(testcase, "testcases.cpp11.smartptr_tc", utest_tc_init, utest_tc_cleanup, 10);