ThreadTests.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "../Tests.hpp"
  2. #include "core/thread/Thread.hpp"
  3. static int runDone = 0;
  4. struct IntHolder {
  5. int value;
  6. };
  7. static int run(void*) {
  8. runDone = 1;
  9. return 7;
  10. }
  11. static void testStart() {
  12. runDone = 0;
  13. Core::Thread t;
  14. CORE_TEST_ERROR(t.start(run, nullptr));
  15. int returnValue = 0;
  16. CORE_TEST_ERROR(t.join(&returnValue));
  17. CORE_TEST_EQUAL(1, runDone);
  18. CORE_TEST_EQUAL(7, returnValue);
  19. }
  20. static void testLambda() {
  21. IntHolder i(0);
  22. Core::Thread t;
  23. CORE_TEST_ERROR(t.start(
  24. [](void* p) {
  25. IntHolder* ip = static_cast<IntHolder*>(p);
  26. ip->value = 2;
  27. return 0;
  28. },
  29. &i));
  30. CORE_TEST_ERROR(t.join(nullptr));
  31. CORE_TEST_EQUAL(2, i.value);
  32. }
  33. static void testJoinWithoutStart() {
  34. Core::Thread t;
  35. CORE_TEST_EQUAL(Core::Error::THREAD_ERROR, t.join(nullptr));
  36. }
  37. static void testAutoJoin() {
  38. Core::Thread t;
  39. CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
  40. }
  41. static void testMove() {
  42. Core::Thread t;
  43. CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
  44. Core::Thread m = Core::move(t);
  45. CORE_TEST_ERROR(m.join());
  46. }
  47. static void testMoveAssignment() {
  48. Core::Thread t;
  49. CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
  50. Core::Thread m;
  51. m = Core::move(t);
  52. CORE_TEST_ERROR(m.join());
  53. }
  54. static void testMoveIntoActive() {
  55. Core::Thread t;
  56. CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
  57. Core::Thread m;
  58. t = Core::move(m);
  59. }
  60. static void testDoubleJoin() {
  61. Core::Thread t;
  62. CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
  63. CORE_TEST_ERROR(t.join(nullptr));
  64. CORE_TEST_EQUAL(Core::Error::THREAD_ERROR, t.join(nullptr));
  65. }
  66. void Core::testThread() {
  67. testStart();
  68. testLambda();
  69. testJoinWithoutStart();
  70. testAutoJoin();
  71. testMove();
  72. testMoveAssignment();
  73. testMoveIntoActive();
  74. testDoubleJoin();
  75. }