testThreadStatic.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "platform/platform.h"
  23. #include "unit/test.h"
  24. #include "core/threadStatic.h"
  25. #include "unit/memoryTester.h"
  26. using namespace UnitTesting;
  27. //-----------------------------------------------------------------------------
  28. // This unit test will blow up without thread static support
  29. #ifdef TORQUE_ENABLE_THREAD_STATICS
  30. // Declare a test thread static
  31. DITTS( U32, gUnitTestFoo, 42 );
  32. DITTS( F32, gUnitTestF32, 1.0 );
  33. CreateUnitTest( TestThreadStatic, "Core/ThreadStatic" )
  34. {
  35. void run()
  36. {
  37. MemoryTester m;
  38. m.mark();
  39. // ThreadStatic list should be initialized right now, so lets see if it has
  40. // any entries.
  41. test( !_TorqueThreadStaticReg::getStaticList().empty(), "Self-registration has failed, or no statics declared" );
  42. // Spawn a new copy.
  43. TorqueThreadStaticListHandle testInstance = _TorqueThreadStaticReg::spawnThreadStaticsInstance();
  44. // Test the copy
  45. test( _TorqueThreadStaticReg::getStaticList( 0 ).size() == testInstance->size(), "Spawned static list has a different size from master copy." );
  46. // Make sure the size test passed before this is attempted
  47. if( lastTestPassed() )
  48. {
  49. // Traverse the list and compare it to the initial value copy (index 0)
  50. for( S32 i = 0; i < _TorqueThreadStaticReg::getStaticList().size(); i++ )
  51. {
  52. _TorqueThreadStatic *master = _TorqueThreadStaticReg::getStaticList()[i];
  53. _TorqueThreadStatic *cpy = (*testInstance)[i];
  54. // Make sure it is not the same memory
  55. test( master != cpy, "Copy not spawned properly." );
  56. // Make sure the sizes are the same
  57. test( master->getMemInstSize() == cpy->getMemInstSize(), "Size mismatch between master and copy" );
  58. // Make sure the initialization occurred properly
  59. if( lastTestPassed() )
  60. test( dMemcmp( master->getMemInstPtr(), cpy->getMemInstPtr(), master->getMemInstSize() ) == 0, "Initial value for spawned list is not correct" );
  61. }
  62. }
  63. // Test metrics if enabled
  64. #ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
  65. U32 fooHitCount = (*testInstance)[_gUnitTestFooTorqueThreadStatic::getListIndex()]->getHitCount();
  66. #endif
  67. // Set/get some values (If we test static metrics, this is hit 1)
  68. ATTS_(gUnitTestFoo, 1) = 55;
  69. // Test to see that the master copy and the spawned copy differ
  70. // (If we test metrics, this is hit 2, for the instance, and hit 1 for the master)
  71. test( ATTS_(gUnitTestFoo, 0) != ATTS_(gUnitTestFoo, 1 ) , "Assignment for spawned instanced memory failed" );
  72. #ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
  73. U32 fooHitCount2 = (*testInstance)[_gUnitTestFooTorqueThreadStatic::getListIndex()]->getHitCount();
  74. test( fooHitCount2 == ( fooHitCount + 2 ), "Thread static metric hit count failed" );
  75. #endif
  76. // Destroy instances
  77. _TorqueThreadStaticReg::destroyInstance( testInstance );
  78. // Now test the cleanup
  79. test( m.check(), "Memory leak in dynamic static allocation stuff." );
  80. }
  81. };
  82. #endif // TORQUE_ENABLE_THREAD_STATICS