From e8d3d0fca4e2555432dc476ec2e942ce500d8e1a Mon Sep 17 00:00:00 2001 From: Chen Wang Date: Wed, 15 Oct 2025 15:11:32 +0800 Subject: utest: move cpp from examples to components/libc/cplusplus Also update the case names to compatible with naming rules. Signed-off-by: Chen Wang --- Kconfig.utestcases | 2 +- components/libc/cplusplus/utest/Kconfig | 9 + components/libc/cplusplus/utest/SConscript | 14 ++ components/libc/cplusplus/utest/tc_atomic.cpp | 271 ++++++++++++++++++++++++ components/libc/cplusplus/utest/tc_auto.cpp | 64 ++++++ components/libc/cplusplus/utest/tc_lambda.cpp | 58 +++++ components/libc/cplusplus/utest/tc_mutex.cpp | 120 +++++++++++ components/libc/cplusplus/utest/tc_smartptr.cpp | 55 +++++ components/libc/cplusplus/utest/tc_thread.cpp | 59 ++++++ examples/utest/testcases/cpp11/Kconfig | 9 - examples/utest/testcases/cpp11/SConscript | 14 -- examples/utest/testcases/cpp11/tc_atomic.cpp | 271 ------------------------ examples/utest/testcases/cpp11/tc_auto.cpp | 64 ------ examples/utest/testcases/cpp11/tc_lambda.cpp | 58 ----- examples/utest/testcases/cpp11/tc_mutex.cpp | 120 ----------- examples/utest/testcases/cpp11/tc_smartptr.cpp | 55 ----- examples/utest/testcases/cpp11/tc_thread.cpp | 59 ------ 17 files changed, 651 insertions(+), 651 deletions(-) create mode 100644 components/libc/cplusplus/utest/Kconfig create mode 100644 components/libc/cplusplus/utest/SConscript create mode 100644 components/libc/cplusplus/utest/tc_atomic.cpp create mode 100644 components/libc/cplusplus/utest/tc_auto.cpp create mode 100644 components/libc/cplusplus/utest/tc_lambda.cpp create mode 100644 components/libc/cplusplus/utest/tc_mutex.cpp create mode 100644 components/libc/cplusplus/utest/tc_smartptr.cpp create mode 100644 components/libc/cplusplus/utest/tc_thread.cpp delete mode 100644 examples/utest/testcases/cpp11/Kconfig delete mode 100644 examples/utest/testcases/cpp11/SConscript delete mode 100644 examples/utest/testcases/cpp11/tc_atomic.cpp delete mode 100644 examples/utest/testcases/cpp11/tc_auto.cpp delete mode 100644 examples/utest/testcases/cpp11/tc_lambda.cpp delete mode 100644 examples/utest/testcases/cpp11/tc_mutex.cpp delete mode 100644 examples/utest/testcases/cpp11/tc_smartptr.cpp delete mode 100644 examples/utest/testcases/cpp11/tc_thread.cpp diff --git a/Kconfig.utestcases b/Kconfig.utestcases index 8bedaf2d40..c4d446348e 100644 --- a/Kconfig.utestcases +++ b/Kconfig.utestcases @@ -9,7 +9,6 @@ if RT_USING_UTESTCASES rsource "components/utilities/utest/utest/Kconfig" rsource "src/utest/Kconfig" -rsource "examples/utest/testcases/cpp11/Kconfig" rsource "examples/utest/testcases/mm/Kconfig" rsource "examples/utest/testcases/tmpfs/Kconfig" @@ -22,6 +21,7 @@ rsource "components/drivers/serial/utest/Kconfig" rsource "components/drivers/smp_call/utest/Kconfig" rsource "components/dfs/utest/Kconfig" rsource "components/net/utest/Kconfig" +rsource "components/libc/cplusplus/utest/Kconfig" endif diff --git a/components/libc/cplusplus/utest/Kconfig b/components/libc/cplusplus/utest/Kconfig new file mode 100644 index 0000000000..3259e26a39 --- /dev/null +++ b/components/libc/cplusplus/utest/Kconfig @@ -0,0 +1,9 @@ +menu "CPP11 Testcase" + +config RT_UTEST_CPP11_THREAD + bool "Cpp11 thread test" + select RT_USING_CPLUSPLUS + select RT_USING_CPLUSPLUS11 + default n + +endmenu diff --git a/components/libc/cplusplus/utest/SConscript b/components/libc/cplusplus/utest/SConscript new file mode 100644 index 0000000000..f9c8b3e1fc --- /dev/null +++ b/components/libc/cplusplus/utest/SConscript @@ -0,0 +1,14 @@ +Import('rtconfig') +from building import * + +cwd = GetCurrentDir() +src = [] +CPPPATH = [cwd] + +if GetDepend('RT_UTEST_CPP11_THREAD'): + src += Glob('tc_*.cpp') + +group = DefineGroup('utestcases', src, depend = ['RT_USING_UTESTCASES', 'RT_USING_CPLUSPLUS'], CPPPATH = CPPPATH) + +Return('group') + diff --git a/components/libc/cplusplus/utest/tc_atomic.cpp b/components/libc/cplusplus/utest/tc_atomic.cpp new file mode 100644 index 0000000000..8b5bdbd9ba --- /dev/null +++ b/components/libc/cplusplus/utest/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, "components.libc.cpp.atomic_tc", utest_tc_init, utest_tc_cleanup, 10); diff --git a/components/libc/cplusplus/utest/tc_auto.cpp b/components/libc/cplusplus/utest/tc_auto.cpp new file mode 100644 index 0000000000..8818c23b89 --- /dev/null +++ b/components/libc/cplusplus/utest/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, "components.libc.cpp.auto_tc", utest_tc_init, utest_tc_cleanup, 10); diff --git a/components/libc/cplusplus/utest/tc_lambda.cpp b/components/libc/cplusplus/utest/tc_lambda.cpp new file mode 100644 index 0000000000..061836005b --- /dev/null +++ b/components/libc/cplusplus/utest/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, "components.libc.cpp.lambda_tc", utest_tc_init, utest_tc_cleanup, 10); diff --git a/components/libc/cplusplus/utest/tc_mutex.cpp b/components/libc/cplusplus/utest/tc_mutex.cpp new file mode 100644 index 0000000000..afa07648e4 --- /dev/null +++ b/components/libc/cplusplus/utest/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, "components.libc.cpp.mutex_tc", utest_tc_init, utest_tc_cleanup, 10); diff --git a/components/libc/cplusplus/utest/tc_smartptr.cpp b/components/libc/cplusplus/utest/tc_smartptr.cpp new file mode 100644 index 0000000000..1a9fa7eb11 --- /dev/null +++ b/components/libc/cplusplus/utest/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, "components.libc.cpp.smartptr_tc", utest_tc_init, utest_tc_cleanup, 10); diff --git a/components/libc/cplusplus/utest/tc_thread.cpp b/components/libc/cplusplus/utest/tc_thread.cpp new file mode 100644 index 0000000000..c60a9ab602 --- /dev/null +++ b/components/libc/cplusplus/utest/tc_thread.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-09-03 liukang the first version + */ + +#include +#include "utest.h" +#include + +static void test_thread(void) +{ + int count = 0; + auto func = [&]() mutable + { + for (int i = 0; i < 100; ++i) + { + ++count; + } + }; + + std::thread t1(func); + t1.join(); + + if (count != 100) + { + uassert_false(1); + } + + std::thread t2(func); + t2.join(); + + if (count != 200) + { + uassert_false(1); + } + + uassert_true(1); +} + +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) +{ + UTEST_UNIT_RUN(test_thread); +} +UTEST_TC_EXPORT(testcase, "components.libc.cpp.thread_tc", utest_tc_init, utest_tc_cleanup, 10); diff --git a/examples/utest/testcases/cpp11/Kconfig b/examples/utest/testcases/cpp11/Kconfig deleted file mode 100644 index 3259e26a39..0000000000 --- a/examples/utest/testcases/cpp11/Kconfig +++ /dev/null @@ -1,9 +0,0 @@ -menu "CPP11 Testcase" - -config RT_UTEST_CPP11_THREAD - bool "Cpp11 thread test" - select RT_USING_CPLUSPLUS - select RT_USING_CPLUSPLUS11 - default n - -endmenu diff --git a/examples/utest/testcases/cpp11/SConscript b/examples/utest/testcases/cpp11/SConscript deleted file mode 100644 index f9c8b3e1fc..0000000000 --- a/examples/utest/testcases/cpp11/SConscript +++ /dev/null @@ -1,14 +0,0 @@ -Import('rtconfig') -from building import * - -cwd = GetCurrentDir() -src = [] -CPPPATH = [cwd] - -if GetDepend('RT_UTEST_CPP11_THREAD'): - 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/tc_atomic.cpp b/examples/utest/testcases/cpp11/tc_atomic.cpp deleted file mode 100644 index 4f9ca4b4d8..0000000000 --- a/examples/utest/testcases/cpp11/tc_atomic.cpp +++ /dev/null @@ -1,271 +0,0 @@ -/* - * 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); diff --git a/examples/utest/testcases/cpp11/tc_auto.cpp b/examples/utest/testcases/cpp11/tc_auto.cpp deleted file mode 100644 index 370d89f020..0000000000 --- a/examples/utest/testcases/cpp11/tc_auto.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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); diff --git a/examples/utest/testcases/cpp11/tc_lambda.cpp b/examples/utest/testcases/cpp11/tc_lambda.cpp deleted file mode 100644 index 1e3f70bd9b..0000000000 --- a/examples/utest/testcases/cpp11/tc_lambda.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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); diff --git a/examples/utest/testcases/cpp11/tc_mutex.cpp b/examples/utest/testcases/cpp11/tc_mutex.cpp deleted file mode 100644 index 12974fa727..0000000000 --- a/examples/utest/testcases/cpp11/tc_mutex.cpp +++ /dev/null @@ -1,120 +0,0 @@ -/* - * 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); diff --git a/examples/utest/testcases/cpp11/tc_smartptr.cpp b/examples/utest/testcases/cpp11/tc_smartptr.cpp deleted file mode 100644 index 22064bbc17..0000000000 --- a/examples/utest/testcases/cpp11/tc_smartptr.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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); diff --git a/examples/utest/testcases/cpp11/tc_thread.cpp b/examples/utest/testcases/cpp11/tc_thread.cpp deleted file mode 100644 index 21dc9c8bc8..0000000000 --- a/examples/utest/testcases/cpp11/tc_thread.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2006-2019, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2021-09-03 liukang the first version - */ - -#include -#include "utest.h" -#include - -static void test_thread(void) -{ - int count = 0; - auto func = [&]() mutable - { - for (int i = 0; i < 100; ++i) - { - ++count; - } - }; - - std::thread t1(func); - t1.join(); - - if (count != 100) - { - uassert_false(1); - } - - std::thread t2(func); - t2.join(); - - if (count != 200) - { - uassert_false(1); - } - - uassert_true(1); -} - -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) -{ - UTEST_UNIT_RUN(test_thread); -} -UTEST_TC_EXPORT(testcase, "testcases.cpp11.thread_tc", utest_tc_init, utest_tc_cleanup, 10); -- cgit v1.3.1