threadStaticTest.cpp 3.7 KB

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