ThreadTest.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "../testTools.h"
  2. #include "../../DFPSR/base/threading.h"
  3. #include "../../DFPSR/api/timeAPI.h"
  4. // The dummy tasks are too small to get a benefit from multi-threading. (0.18 ms overhead on 0.04 ms of total work)
  5. START_TEST(Thread)
  6. { // Basic version iterating over lambdas in a dynamic list
  7. const int jobCount = 50;
  8. int results[jobCount] = {}; // TODO: Make a safer example.
  9. List<std::function<void()>> jobs;
  10. for (int i = 0; i < jobCount; i++) {
  11. int* result = &results[i];
  12. jobs.push([result, i](){
  13. // Simulate a heavy workload
  14. time_sleepSeconds(0.01f);
  15. *result = i * 26 + 43;
  16. });
  17. }
  18. double totalStartTime = time_getSeconds();
  19. threadedWorkFromList(jobs);
  20. printText("Completed all jobs in ", (time_getSeconds() - totalStartTime) * 1000.0, " ms\n");
  21. for (int i = 0; i < jobCount; i++) {
  22. ASSERT_EQUAL(results[i], i * 26 + 43);
  23. }
  24. }
  25. { // Threaded split for automatic division of a big number of jobs
  26. List<int> items;
  27. for (int i = 0; i < 100; i++) {
  28. items.push(0);
  29. }
  30. int rowStart = 10; // Inclusive
  31. int rowEnd = 90; // Exclusive
  32. double totalStartTime = time_getSeconds();
  33. int* itemPtr = &items[0];
  34. threadedSplit(rowStart, rowEnd, [itemPtr](int startIndex, int stopIndex) {
  35. for (int i = startIndex; i < stopIndex; i++) {
  36. itemPtr[i] += i * 10;
  37. }
  38. });
  39. printText("Completed all jobs in ", (time_getSeconds() - totalStartTime) * 1000.0, " ms\n");
  40. for (int i = 0; i < rowStart; i++) {
  41. ASSERT_EQUAL(items[i], 0);
  42. }
  43. for (int i = rowStart; i < rowEnd; i++) {
  44. ASSERT_EQUAL(items[i], i * 10);
  45. }
  46. for (int i = rowEnd; i < items.length(); i++) {
  47. ASSERT_EQUAL(items[i], 0);
  48. }
  49. }
  50. END_TEST