tc_lambda.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-09-19 Rbb666 the first version
  9. */
  10. #include <rtthread.h>
  11. #include "utest.h"
  12. /**
  13. * @brief Test basic lambda expression.
  14. */
  15. static void test_lambda_basic(void)
  16. {
  17. auto lambda = []() { return 42; };
  18. if (lambda() != 42)
  19. {
  20. uassert_false(true);
  21. }
  22. uassert_true(true);
  23. }
  24. /**
  25. * @brief Test lambda with capture.
  26. */
  27. static void test_lambda_capture(void)
  28. {
  29. int x = 10;
  30. auto lambda = [x]() { return x * 2; };
  31. if (lambda() != 20)
  32. {
  33. uassert_false(true);
  34. }
  35. uassert_true(true);
  36. }
  37. static rt_err_t utest_tc_init(void)
  38. {
  39. return RT_EOK;
  40. }
  41. static rt_err_t utest_tc_cleanup(void)
  42. {
  43. return RT_EOK;
  44. }
  45. static void testcase(void)
  46. {
  47. /* Test basic lambda expression */
  48. UTEST_UNIT_RUN(test_lambda_basic);
  49. /* Test lambda with capture */
  50. UTEST_UNIT_RUN(test_lambda_capture);
  51. }
  52. UTEST_TC_EXPORT(testcase, "components.libc.cpp.lambda_tc", utest_tc_init, utest_tc_cleanup, 10);