tc_thread.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-09-03 liukang the first version
  9. */
  10. #include <rtthread.h>
  11. #include "utest.h"
  12. #include <thread>
  13. /**
  14. * @brief Function to test thread functionality.
  15. */
  16. static void test_thread(void)
  17. {
  18. int count = 0;
  19. /* Lambda function to increment the count. */
  20. auto func = [&]() mutable {
  21. for (int i = 0; i < 100; ++i)
  22. {
  23. ++count;
  24. }
  25. };
  26. /* Create and run a thread executing the lambda function. */
  27. std::thread t1(func);
  28. /* Wait for the thread to finish execution. */
  29. t1.join();
  30. /* Verify if the count is as expected after the first thread execution. */
  31. if (count != 100)
  32. {
  33. uassert_false(1);
  34. }
  35. /* Create and run another thread executing the same lambda function. */
  36. std::thread t2(func);
  37. t2.join();
  38. if (count != 200)
  39. {
  40. uassert_false(1);
  41. }
  42. /* If both assertions passed, the test is successful. */
  43. uassert_true(1);
  44. }
  45. /**
  46. * @brief Test case initialization function.
  47. * @return RT_EOK on success.
  48. */
  49. static rt_err_t utest_tc_init(void)
  50. {
  51. return RT_EOK;
  52. }
  53. /**
  54. * @brief Test case cleanup function.
  55. * @return RT_EOK on success.
  56. */
  57. static rt_err_t utest_tc_cleanup(void)
  58. {
  59. return RT_EOK;
  60. }
  61. /**
  62. * @brief Main test case function that runs the test.
  63. */
  64. static void testcase(void)
  65. {
  66. UTEST_UNIT_RUN(test_thread);
  67. }
  68. /* Export the test case with initialization and cleanup functions and a timeout of 10 ticks. */
  69. UTEST_TC_EXPORT(testcase, "components.libc.cpp.thread_tc", utest_tc_init, utest_tc_cleanup, 10);