threadPoolTest.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/threads/threadPool.h"
  25. #include "console/console.h"
  26. #include "core/util/tVector.h"
  27. FIXTURE(ThreadPool)
  28. {
  29. public:
  30. // Represents a single unit of work. In this test we just set an element in
  31. // a result vector.
  32. struct TestItem : public ThreadPool::WorkItem
  33. {
  34. U32 mIndex;
  35. Vector<U32>& mResults;
  36. TestItem(U32 index, Vector<U32>& results)
  37. : mIndex(index), mResults(results) {}
  38. protected:
  39. virtual void execute()
  40. {
  41. mResults[mIndex] = mIndex;
  42. }
  43. };
  44. };
  45. TEST_FIX(ThreadPool, BasicAPI)
  46. {
  47. // Construct the vector of results from the work items.
  48. const U32 numItems = 100;
  49. Vector<U32> results(__FILE__, __LINE__);
  50. results.setSize(numItems);
  51. for (U32 i = 0; i < numItems; i++)
  52. results[i] = U32(-1);
  53. // Launch the work items.
  54. ThreadPool* pool = &ThreadPool::GLOBAL();
  55. for (U32 i = 0; i < numItems; i++)
  56. {
  57. ThreadSafeRef<TestItem> item(new TestItem(i, results));
  58. pool->queueWorkItem(item);
  59. }
  60. // Wait for all items to complete.
  61. pool->flushWorkItems();
  62. // Verify.
  63. for (U32 i = 0; i < numItems; i++)
  64. EXPECT_EQ(results[i], i) << "result mismatch";
  65. results.clear();
  66. }
  67. #endif