Torque3D Documentation / _generateds / threadStaticTest.cpp

threadStaticTest.cpp

Engine/source/platform/test/threadStaticTest.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2014 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifdef TORQUE_TESTS_ENABLED
 25#include "testing/unitTesting.h"
 26
 27// This unit test will blow up without thread static support
 28#include "core/threadStatic.h"
 29#ifdef TORQUE_ENABLE_THREAD_STATICS
 30
 31// Declare a test thread static
 32DITTS(U32, gUnitTestFoo, 42);
 33DITTS(F32, gUnitTestF32, 1.0);
 34
 35TEST(ThreadStatic, BasicAPI)
 36{
 37   // ThreadStatic list should be initialized right now, so lets see if it has
 38   // any entries.
 39   EXPECT_FALSE(_TorqueThreadStaticReg::getStaticList().empty())
 40      << "Self-registration has failed, or no statics declared";
 41
 42   // Spawn a new copy.
 43   TorqueThreadStaticListHandle testInstance = _TorqueThreadStaticReg::spawnThreadStaticsInstance();
 44
 45   // Test the copy
 46   ASSERT_EQ(_TorqueThreadStaticReg::getStaticList(0).size(), testInstance->size())
 47      << "Spawned static list has a different size from master copy.";
 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
 55      // Make sure it is not the same memory
 56      EXPECT_NE(master, cpy)
 57         << "Copy not spawned properly.";
 58
 59      // Make sure the sizes are the same
 60      ASSERT_EQ(master->getMemInstSize(), cpy->getMemInstSize())
 61         << "Size mismatch between master and copy";
 62
 63      // Make sure the initialization occurred properly
 64      EXPECT_EQ(0, dMemcmp(master->getMemInstPtr(), cpy->getMemInstPtr(), master->getMemInstSize()))
 65         << "Initial value for spawned list is not correct";
 66   }
 67
 68   // Test metrics if enabled
 69#ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
 70   U32 fooHitCount = (*testInstance)[_gUnitTestFooTorqueThreadStatic::getListIndex()]->getHitCount();
 71#endif
 72
 73   // Set/get some values (If we test static metrics, this is hit 1)
 74   ATTS_(gUnitTestFoo, 1) = 55;
 75
 76   // Test to see that the master copy and the spawned copy differ
 77   // (If we test metrics, this is hit 2, for the instance, and hit 1 for the master)
 78   EXPECT_NE(ATTS_(gUnitTestFoo, 0), ATTS_(gUnitTestFoo, 1))
 79      << "Assignment for spawned instanced memory failed";
 80
 81#ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
 82   U32 fooHitCount2 = (*testInstance)[_gUnitTestFooTorqueThreadStatic::getListIndex()]->getHitCount();
 83   EXPECT_EQ(fooHitCount2, (fooHitCount + 2))
 84      << "Thread static metric hit count failed";
 85#endif
 86
 87   // Destroy instances
 88   _TorqueThreadStaticReg::destroyInstance(testInstance);
 89}
 90
 91#ifdef TORQUE_ENABLE_PROFILER
 92#include "math/mRandom.h"
 93#include "platform/profiler.h"
 94
 95// Declare a test thread static
 96DITTS(U32, gInstancedStaticFoo, 42);
 97static U32 gTrueStaticFoo = 42;
 98
 99TEST(ThreadStatic, StressThreadStatic)
100{
101   ASSERT_FALSE(gProfiler->isEnabled())
102      << "Profiler is currently enabled, test cannot continue";
103
104   // Spawn an instance
105   TorqueThreadStaticListHandle testInstance = _TorqueThreadStaticReg::spawnThreadStaticsInstance();
106
107   static const dsize_t TEST_SIZE = 100000;
108
109   // What we are going to do in this test is to test some U32 static
110   // performance. The test will be run TEST_SIZE times, and so first create
111   // an array of values to standardize the tests on.
112   U32 testValue[TEST_SIZE];
113
114   for(S32 i = 0; i < TEST_SIZE; i++)
115      testValue[i] = gRandGen.randI();
116
117   // Reset the profiler, tell it to dump to console when done
118   gProfiler->dumpToConsole();
119   gProfiler->enable(true);
120
121   // Value array is initialized, so lets put the foo's through the paces
122   PROFILE_START(ThreadStaticPerf_TrueStaticAssign);
123   for(int i = 0; i < TEST_SIZE; i++)
124      gTrueStaticFoo = testValue[i];
125   PROFILE_END();
126
127   PROFILE_START(ThreadStaticPerf_InstanceStaticAssign);
128   for(S32 i = 0; i < TEST_SIZE; i++)
129      ATTS_(gInstancedStaticFoo, 1) = testValue[i];
130   PROFILE_END();
131
132   gProfiler->enable(false);
133
134   // Clean up instance
135   _TorqueThreadStaticReg::destroyInstance(testInstance);
136}
137#endif
138
139#endif
140#endif
141