st_utest_coroutines.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* SPDX-License-Identifier: MIT */
  2. /* Copyright (c) 2013-2024 The SRS Authors */
  3. #include <st_utest.hpp>
  4. #include <st.h>
  5. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6. // The utest for empty coroutine.
  7. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. void* coroutine(void* /*arg*/)
  9. {
  10. st_usleep(0);
  11. return NULL;
  12. }
  13. VOID TEST(CoroutineTest, StartCoroutine)
  14. {
  15. st_thread_t trd = st_thread_create(coroutine, NULL, 1, 0);
  16. EXPECT_TRUE(trd != NULL);
  17. // Wait for joinable coroutine to quit.
  18. st_thread_join(trd, NULL);
  19. }
  20. VOID TEST(CoroutineTest, StartCoroutineX3)
  21. {
  22. st_thread_t trd0 = st_thread_create(coroutine, NULL, 1, 0);
  23. st_thread_t trd1 = st_thread_create(coroutine, NULL, 1, 0);
  24. st_thread_t trd2 = st_thread_create(coroutine, NULL, 1, 0);
  25. EXPECT_TRUE(trd0 != NULL && trd1 != NULL && trd2 != NULL);
  26. // Wait for joinable coroutine to quit.
  27. st_thread_join(trd1, NULL);
  28. st_thread_join(trd2, NULL);
  29. st_thread_join(trd0, NULL);
  30. }
  31. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. // The utest for adding coroutine.
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. void* coroutine_add(void* arg)
  35. {
  36. int v = 0;
  37. int* pi = (int*)arg;
  38. // Load the change of arg.
  39. while (v != *pi) {
  40. v = *pi;
  41. st_usleep(0);
  42. }
  43. // Add with const.
  44. v += 100;
  45. *pi = v;
  46. return NULL;
  47. }
  48. VOID TEST(CoroutineTest, StartCoroutineAdd)
  49. {
  50. int v = 0;
  51. st_thread_t trd = st_thread_create(coroutine_add, &v, 1, 0);
  52. EXPECT_TRUE(trd != NULL);
  53. // Wait for joinable coroutine to quit.
  54. st_thread_join(trd, NULL);
  55. EXPECT_EQ(100, v);
  56. }
  57. VOID TEST(CoroutineTest, StartCoroutineAddX3)
  58. {
  59. int v = 0;
  60. st_thread_t trd0 = st_thread_create(coroutine_add, &v, 1, 0);
  61. st_thread_t trd1 = st_thread_create(coroutine_add, &v, 1, 0);
  62. st_thread_t trd2 = st_thread_create(coroutine_add, &v, 1, 0);
  63. EXPECT_TRUE(trd0 != NULL && trd1 != NULL && trd2 != NULL);
  64. // Wait for joinable coroutine to quit.
  65. st_thread_join(trd0, NULL);
  66. st_thread_join(trd1, NULL);
  67. st_thread_join(trd2, NULL);
  68. EXPECT_EQ(300, v);
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. // The utest for output params coroutine.
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. int coroutine_params_x4(int a, int b, int c, int d)
  74. {
  75. int e = 0;
  76. st_usleep(0);
  77. e += a + b + c + d;
  78. e += 100;
  79. return e;
  80. }
  81. void* coroutine_params(void* arg)
  82. {
  83. int r0 = coroutine_params_x4(1, 2, 3, 4);
  84. *(int*)arg = r0;
  85. return NULL;
  86. }
  87. VOID TEST(CoroutineTest, StartCoroutineParams)
  88. {
  89. int r0 = 0;
  90. st_thread_t trd = st_thread_create(coroutine_params, &r0, 1, 0);
  91. EXPECT_TRUE(trd != NULL);
  92. // Wait for joinable coroutine to quit.
  93. st_thread_join(trd, NULL);
  94. EXPECT_EQ(110, r0);
  95. }