testThreadPool.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "unit/test.h"
  23. #include "platform/threads/threadPool.h"
  24. #include "console/console.h"
  25. #include "core/util/tVector.h"
  26. #ifndef TORQUE_SHIPPING
  27. using namespace UnitTesting;
  28. #define TEST( x ) test( ( x ), "FAIL: " #x )
  29. // Simple test that creates and verifies an array of numbers using
  30. // thread pool work items.
  31. CreateUnitTest( TestThreadPool, "Platform/ThreadPool/Simple" )
  32. {
  33. enum { DEFAULT_NUM_ITEMS = 4000 };
  34. static Vector< U32 > results;
  35. struct TestItem : public ThreadPool::WorkItem
  36. {
  37. typedef ThreadPool::WorkItem Parent;
  38. U32 mIndex;
  39. TestItem( U32 index )
  40. : mIndex( index ) {}
  41. protected:
  42. virtual void execute()
  43. {
  44. results[ mIndex ] = mIndex;
  45. }
  46. };
  47. void run()
  48. {
  49. U32 numItems = Con::getIntVariable( "$testThreadPool::numValues", DEFAULT_NUM_ITEMS );
  50. ThreadPool* pool = &ThreadPool::GLOBAL();
  51. results.setSize( numItems );
  52. for( U32 i = 0; i < numItems; ++ i )
  53. results[ i ] = U32( -1 );
  54. for( U32 i = 0; i < numItems; ++ i )
  55. {
  56. ThreadSafeRef< TestItem > item( new TestItem( i ) );
  57. pool->queueWorkItem( item );
  58. }
  59. pool->flushWorkItems();
  60. for( U32 i = 0; i < numItems; ++ i )
  61. test( results[ i ] == i, "result mismatch" );
  62. results.clear();
  63. }
  64. };
  65. Vector< U32 > TestThreadPool::results( __FILE__, __LINE__ );
  66. #endif // !TORQUE_SHIPPING