ThreadTests.cpp 979 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "tests/ThreadTests.h"
  2. #include "test/Test.h"
  3. #include "thread/Thread.h"
  4. static int runDone = 0;
  5. struct IntHolder {
  6. int value;
  7. };
  8. static int run(void*) {
  9. runDone = 1;
  10. return 7;
  11. }
  12. static void testStart() {
  13. runDone = 0;
  14. Core::Thread::Id id = Core::Thread::INVALID_ID;
  15. CORE_TEST_ERROR(Core::Thread::start(id, run, nullptr));
  16. int returnValue = 0;
  17. CORE_TEST_ERROR(Core::Thread::join(id, &returnValue));
  18. CORE_TEST_EQUAL(1, runDone);
  19. CORE_TEST_EQUAL(7, returnValue);
  20. }
  21. static void testLambda() {
  22. IntHolder i(0);
  23. Core::Thread::Id id = Core::Thread::INVALID_ID;
  24. CORE_TEST_ERROR(Core::Thread::start(
  25. id,
  26. [](void* p) {
  27. IntHolder* ip = static_cast<IntHolder*>(p);
  28. ip->value = 2;
  29. return 0;
  30. },
  31. &i));
  32. CORE_TEST_ERROR(Core::Thread::join(id, nullptr));
  33. CORE_TEST_EQUAL(2, i.value);
  34. }
  35. void Core::ThreadTests::test() {
  36. testStart();
  37. testLambda();
  38. }