ThreadTests.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/Test.hpp"
  4. #include "core/Thread.hpp"
  5. static int runDone = 0;
  6. struct IntHolder {
  7. int value;
  8. };
  9. static void run(void*) {
  10. runDone = 1;
  11. }
  12. static void testStart() {
  13. runDone = 0;
  14. {
  15. Core::Thread t;
  16. TEST_FALSE(t.start(run, nullptr));
  17. }
  18. TEST(1, runDone);
  19. }
  20. static void testLambda() {
  21. IntHolder i(0);
  22. Core::Thread t;
  23. TEST_FALSE(t.start([](void* p) {
  24. IntHolder* ip = static_cast<IntHolder*>(p);
  25. ip->value = 2;
  26. }, &i));
  27. TEST_FALSE(t.join());
  28. TEST(2, i.value);
  29. }
  30. static void testJoinWithoutStart() {
  31. Core::Thread t;
  32. TEST_FALSE(t.join());
  33. }
  34. static void testAutoJoin() {
  35. Core::Thread t;
  36. TEST_FALSE(t.start([](void*) {}, nullptr));
  37. }
  38. static void testMove() {
  39. Core::Thread t;
  40. TEST_FALSE(t.start([](void*) {}, nullptr));
  41. Core::Thread m = Core::move(t);
  42. TEST_FALSE(m.join());
  43. }
  44. static void testMoveAssignment() {
  45. Core::Thread t;
  46. TEST_FALSE(t.start([](void*) {}, nullptr));
  47. Core::Thread m;
  48. m = Core::move(t);
  49. TEST_FALSE(m.join());
  50. }
  51. static void testMoveIntoActive() {
  52. Core::Thread t;
  53. TEST_FALSE(t.start([](void*) {}, nullptr));
  54. Core::Thread m;
  55. t = Core::move(m);
  56. }
  57. static void testDoubleJoin() {
  58. Core::Thread t;
  59. TEST_FALSE(t.start([](void*) {}, nullptr));
  60. TEST_FALSE(t.join());
  61. TEST_FALSE(t.join());
  62. }
  63. struct MutexCounter {
  64. Core::Mutex m{};
  65. int counter = 0;
  66. };
  67. static void incrementMutexCounter(void* p) {
  68. MutexCounter* mcp = static_cast<MutexCounter*>(p);
  69. for(int i = 0; i < 100'000; i++) {
  70. Core::MutexGuard mg(mcp->m);
  71. mcp->counter++;
  72. }
  73. }
  74. static void testMutex() {
  75. MutexCounter mc;
  76. Core::Thread t[2];
  77. TEST_FALSE(t[0].start(incrementMutexCounter, &mc));
  78. TEST_FALSE(t[1].start(incrementMutexCounter, &mc));
  79. TEST_FALSE(t[0].join());
  80. TEST_FALSE(t[1].join());
  81. TEST(200'000, mc.counter);
  82. }
  83. static void testDoubleStart() {
  84. Core::Thread t;
  85. TEST_FALSE(t.start([](void*) {}, nullptr));
  86. TEST_TRUE(t.start([](void*) {}, nullptr));
  87. }
  88. static void testFail() {
  89. #ifdef ERROR_SIMULATOR
  90. Core::Mutex m;
  91. failStepThrow = 1;
  92. m.lock();
  93. failStepThrow = 1;
  94. m.unlock();
  95. Core::Thread t;
  96. failStepThrow = 1;
  97. TEST_TRUE(t.start([](void*) {}, nullptr));
  98. TEST_FALSE(t.start([](void*) {}, nullptr));
  99. failStepThrow = 1;
  100. TEST_TRUE(t.join());
  101. #endif
  102. }
  103. void testThread() {
  104. testStart();
  105. testLambda();
  106. testJoinWithoutStart();
  107. testAutoJoin();
  108. testMove();
  109. testMoveAssignment();
  110. testMoveIntoActive();
  111. testDoubleJoin();
  112. testMutex();
  113. testDoubleStart();
  114. testFail();
  115. }