Newer
Older
libkc / modules / test / src / test_thread.c
Nomura Kei on 29 May 2024 2 KB update
  1. #include <stdio.h>
  2. #include <errno.h>
  3.  
  4. #include <kc.h>
  5. #include <kc_ut.h>
  6. #include <kc_assert.h>
  7. #include <kc_memory.h>
  8. #include <kc_threads.h>
  9.  
  10. #include "ut.h"
  11.  
  12. // プロトタイプ宣言
  13. static void test_thread_new(void);
  14. // static void test_thread_sleep(void);
  15. static void test_thread_memory_error(void);
  16.  
  17. /**
  18. * KcThread 単体テストスイート
  19. */
  20. void suite_thread(void)
  21. {
  22. KcUt *ut = KcUt_get_instance();
  23. ut->add(ut, UT_TESTCASE, "thread new/delete", test_thread_new);
  24. // ut->add(ut, UT_TESTCASE, "thread sleep", test_thread_sleep);
  25. ut->add(ut, UT_TESTCASE, "thread memory error", test_thread_memory_error);
  26. }
  27.  
  28. static int pass_value = 0;
  29. static int test_func(void *args)
  30. {
  31. int *value = (int *)args;
  32. if (*value == 1)
  33. {
  34. pass_value &= 0x01;
  35. }
  36. if (*value == 2)
  37. {
  38. pass_value &= 0x02;
  39. }
  40. for (int i = 0; i < 10; i++)
  41. {
  42. printf("Thread %d : %03d\n", *value, i);
  43. KcThread_msleep(1, true);
  44. }
  45. return 0;
  46. }
  47.  
  48. /**
  49. * Thread 生成/破棄。
  50. *
  51. * @process KcThread_new を実行する。。
  52. * @result KcThread が生成されること。
  53. *
  54. * @process KcThread_delete にて Thread を破棄する。
  55. * @result Thread が破棄されること。
  56. */
  57. static void test_thread_new(void)
  58. {
  59. KcThread *thread_1 = KcThread_new(test_func);
  60. KcThread *thread_2 = KcThread_new(test_func);
  61.  
  62. pass_value = 0;
  63. bool is_alive_1 = thread_1->is_alive(thread_1);
  64. bool is_alive_2 = thread_2->is_alive(thread_2);
  65. assert_false(is_alive_1);
  66. assert_false(is_alive_2);
  67. int val_1 = 1;
  68. int val_2 = 2;
  69. thread_1->start(thread_1, &val_1);
  70. thread_2->start(thread_2, &val_2);
  71. KcThread_msleep(10, false);
  72. is_alive_1 = thread_1->is_alive(thread_1);
  73. is_alive_2 = thread_2->is_alive(thread_2);
  74. assert_true(is_alive_1);
  75. assert_true(is_alive_2);
  76. thread_1->join(thread_1);
  77. thread_2->join(thread_2);
  78. KcThread_delete(thread_1);
  79. KcThread_delete(thread_2);
  80. }
  81.  
  82. // for シグナル処理
  83. #if 0
  84. static volatile int test_thread_interrupted = 0;
  85. static void test_thread_sig_handler(int sig)
  86. {
  87. test_thread_interrupted = 1;
  88. }
  89. int test_sleep_func(void *args)
  90. {
  91. signal(SIGUSR1, test_thread_sig_handler);
  92. int *value = (int *)args;
  93. if (*value == 1)
  94. {
  95. KcThread_msleep(200, true);
  96. }
  97. else
  98. {
  99. KcThread_msleep(200, false);
  100. }
  101. return 0;
  102. }
  103.  
  104. /**
  105. * Thread sleep 中のシグナル発生。
  106. */
  107. static void test_thread_sleep(void)
  108. {
  109.  
  110. KcThread *thread = KcThread_new(test_sleep_func);
  111. int value = 1;
  112. test_thread_interrupted = 0;
  113. thread->start(thread, &value);
  114. KcThread_msleep(10, false);
  115.  
  116. thread->join(thread);
  117. KcThread_delete(thread);
  118. }
  119. #endif
  120.  
  121. /**
  122. * メモリ確保エラー
  123. */
  124. static void test_thread_memory_error(void)
  125. {
  126. ut_alloc_control(0)
  127. {
  128. KcThread *thread = KcThread_new(test_func);
  129. assert_null(thread);
  130. }
  131. }