ThreadTests.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <threads.h>
  2. #include "../Tests.hpp"
  3. #include "core/thread/Mutex.hpp"
  4. #include "core/thread/Thread.hpp"
  5. static int runDone = 0;
  6. struct IntHolder {
  7. int value;
  8. };
  9. static int run(void*) {
  10. runDone = 1;
  11. return 7;
  12. }
  13. static void testStart() {
  14. runDone = 0;
  15. Core::Thread t;
  16. CORE_TEST_ERROR(t.start(run, nullptr));
  17. int returnValue = 0;
  18. CORE_TEST_ERROR(t.join(&returnValue));
  19. CORE_TEST_EQUAL(1, runDone);
  20. CORE_TEST_EQUAL(7, returnValue);
  21. }
  22. static void testLambda() {
  23. IntHolder i(0);
  24. Core::Thread t;
  25. CORE_TEST_ERROR(t.start(
  26. [](void* p) {
  27. IntHolder* ip = static_cast<IntHolder*>(p);
  28. ip->value = 2;
  29. return 0;
  30. },
  31. &i));
  32. CORE_TEST_ERROR(t.join(nullptr));
  33. CORE_TEST_EQUAL(2, i.value);
  34. }
  35. static void testJoinWithoutStart() {
  36. Core::Thread t;
  37. CORE_TEST_EQUAL(Core::Error::THREAD_ERROR, t.join(nullptr));
  38. }
  39. static void testAutoJoin() {
  40. Core::Thread t;
  41. CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
  42. }
  43. static void testMove() {
  44. Core::Thread t;
  45. CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
  46. Core::Thread m = Core::move(t);
  47. CORE_TEST_ERROR(m.join());
  48. }
  49. static void testMoveAssignment() {
  50. Core::Thread t;
  51. CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
  52. Core::Thread m;
  53. m = Core::move(t);
  54. CORE_TEST_ERROR(m.join());
  55. }
  56. static void testMoveIntoActive() {
  57. Core::Thread t;
  58. CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
  59. Core::Thread m;
  60. t = Core::move(m);
  61. }
  62. static void testDoubleJoin() {
  63. Core::Thread t;
  64. CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
  65. CORE_TEST_ERROR(t.join(nullptr));
  66. CORE_TEST_EQUAL(Core::Error::THREAD_ERROR, t.join(nullptr));
  67. }
  68. struct MutexCounter {
  69. Core::Mutex m{};
  70. int counter = 0;
  71. };
  72. static int incrementMutexCounter(void* p) {
  73. MutexCounter* mcp = static_cast<MutexCounter*>(p);
  74. for(int i = 0; i < 10000; i++) {
  75. (void)mcp->m.lock();
  76. mcp->counter++;
  77. (void)mcp->m.unlock();
  78. }
  79. return 0;
  80. }
  81. static void testMutex() {
  82. MutexCounter mc;
  83. CORE_TEST_ERROR(mc.m.init());
  84. CORE_TEST_EQUAL(Core::Error::INVALID_STATE, mc.m.init());
  85. Core::Thread t[2];
  86. CORE_TEST_ERROR(t[0].start(incrementMutexCounter, &mc));
  87. CORE_TEST_ERROR(t[1].start(incrementMutexCounter, &mc));
  88. CORE_TEST_ERROR(t[0].join(nullptr));
  89. CORE_TEST_ERROR(t[1].join(nullptr));
  90. CORE_TEST_EQUAL(20000, mc.counter);
  91. }
  92. void Core::testThread() {
  93. testStart();
  94. testLambda();
  95. testJoinWithoutStart();
  96. testAutoJoin();
  97. testMove();
  98. testMoveAssignment();
  99. testMoveIntoActive();
  100. testDoubleJoin();
  101. testMutex();
  102. }