Engine directory for ticket #1

This commit is contained in:
DavidWyand-GG 2012-09-19 11:15:01 -04:00
parent 352279af7a
commit 7dbfe6994d
3795 changed files with 1363358 additions and 0 deletions

View file

@ -0,0 +1,39 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "core/strings/stringFunctions.h"
#include "unit/test.h"
#include "console/console.h"
using namespace UnitTesting;
ConsoleFunction(unitTest_runTests, void, 1, 3, "([searchString[, bool skipInteractive]])"
"@brief Run unit tests, or just the tests that prefix match against the searchString.\n\n"
"@ingroup Console")
{
const char *searchString = (argc > 1 ? argv[1] : "");
bool skip = (argc > 2 ? dAtob(argv[2]) : false);
TestRun tr;
tr.test(searchString, skip);
}

View file

@ -0,0 +1,37 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "unit/memoryTester.h"
using namespace UnitTesting;
void MemoryTester::mark()
{
}
bool MemoryTester::check()
{
//UnitTesting::UnitPrint("MemoryTester::check - unavailable w/o TORQUE_DEBUG_GUARD defined!");
return true;
}

View file

@ -0,0 +1,37 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _UNIT_MEMORYTESTER_H_
#define _UNIT_MEMORYTESTER_H_
namespace UnitTesting
{
class MemoryTester
{
public:
void mark();
bool check();
};
}
#endif

283
Engine/source/unit/test.cpp Normal file
View file

@ -0,0 +1,283 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "core/strings/stringFunctions.h"
#include "console/console.h"
#include "unit/test.h"
namespace UnitTesting
{
//-----------------------------------------------------------------------------
TestRegistry *TestRegistry::_list = 0;
//-----------------------------------------------------------------------------
static const int MaxMarginCount = 32;
static const int MaxMarginValue = 128;
static int _Margin[MaxMarginCount] = { 3 };
static int* _MarginPtr = _Margin;
static char _MarginString[MaxMarginValue];
static void _printMargin()
{
if (*_MarginPtr)
::fwrite(_MarginString,1,*_MarginPtr,stdout);
}
void UnitMargin::Push(int margin)
{
if (_MarginPtr < _Margin + MaxMarginCount) {
*++_MarginPtr = (margin < MaxMarginValue)? margin: MaxMarginValue;
memset(_MarginString,' ',*_MarginPtr);
}
}
void UnitMargin::Pop()
{
if (_MarginPtr > _Margin) {
_MarginPtr--;
memset(_MarginString,' ',*_MarginPtr);
}
}
int UnitMargin::Current()
{
return *_MarginPtr;
}
void UnitPrint(const char* str)
{
static bool lineStart = true;
Platform::outputDebugString(str);
// Need to scan for '\n' in order to support margins
const char* ptr = str, *itr = ptr;
for (; *itr != 0; itr++)
if (*itr == '\n')
{
if (lineStart)
_printMargin();
::fwrite(ptr,1,itr - ptr + 1,stdout);
ptr = itr + 1;
lineStart = true;
}
// End the line with a carriage return unless the
// line ends with a line continuation char.
if (ptr != itr) {
if (lineStart)
_printMargin();
if (itr[-1] == '\\') {
::fwrite(ptr,1,itr - ptr - 1,stdout);
lineStart = false;
}
else {
::fwrite(ptr,1,itr - ptr,stdout);
::fwrite("\n",1,1,stdout);
lineStart = true;
}
}
else {
::fwrite("\n",1,1,stdout);
lineStart = true;
}
::fflush(stdout);
}
//-----------------------------------------------------------------------------
UnitTest::UnitTest() {
_testCount = 0;
_failureCount = 0;
_warningCount = 0;
_lastTestResult = true;
}
void UnitTest::fail(const char* msg)
{
Con::warnf("** Failed: %s",msg);
dFetchAndAdd( _failureCount, 1 );
}
void UnitTest::warn(const char* msg)
{
Con::warnf("** Warning: %s",msg);
dFetchAndAdd( _warningCount, 1 );
}
//-----------------------------------------------------------------------------
TestRegistry::TestRegistry(const char* name, bool interactive, const char *className)
{
// Check that no existing test uses the same class-name; this is guaranteed
// to lead to funkiness.
TestRegistry *walk = _list;
while(walk)
{
if(walk->_className)
{
AssertFatal(dStricmp(className, walk->_className), "TestRegistry::TestRegistry - got two unit tests with identical class names; they must have unique class names!");
}
walk = walk->_next;
}
// Add us to the list.
_next = _list;
_list = this;
// And fill in our fields.
_name = name;
_className = className;
_isInteractive = interactive;
}
DynamicTestRegistration::DynamicTestRegistration( const char *name, UnitTest *test ) : TestRegistry( name, false, NULL ), mUnitTest( test )
{
}
DynamicTestRegistration::~DynamicTestRegistration()
{
// Un-link ourselves from the test registry
TestRegistry *walk = _list;
// Easy case!
if( walk == this )
_list = _next;
else
{
// Search for us and remove
while( ( walk != 0 ) && ( walk->_next != 0 ) && ( walk->_next != this ) )
walk = walk->_next;
// When this loop is broken, walk will be the unit test in the list previous to this one
if( walk != 0 && walk->_next != 0 )
walk->_next = walk->_next->_next;
}
}
//-----------------------------------------------------------------------------
TestRun::TestRun()
{
_subCount = 0;
_testCount = 0;
_failureCount = 0;
_warningCount = 0;
}
void TestRun::printStats()
{
Con::printf("-- %d test%s run (with %d sub-test%s)",
_testCount,(_testCount != 1)? "s": "",
_subCount,(_subCount != 1)? "s": "");
if (_testCount)
{
if (_failureCount)
Con::printf("** %d reported failure%s",
_failureCount,(_failureCount != 1)? "s": "");
else if (_warningCount)
Con::printf("** %d reported warning%s",
_warningCount,(_warningCount != 1)? "s": "");
else
Con::printf("-- No reported failures");
}
}
void TestRun::test(TestRegistry* reg)
{
Con::printf("-- Testing: %s %s",reg->getName(), reg->isInteractive() ? "(interactive)" : "" );
UnitMargin::Push(_Margin[0]);
// Run the test.
UnitTest* test = reg->newTest();
test->run();
UnitMargin::Pop();
// Update stats.
_failureCount += test->getFailureCount();
_subCount += test->getTestCount();
_warningCount += test->getWarningCount();
_testCount++;
// Don't forget to delete the test!
delete test;
}
// [tom, 2/5/2007] To provide a predictable environment for the tests, this
// now changes the current directory to the executable's directory before
// running the tests. The previous current directory is restored on exit.
bool TestRun::test(const char* module, bool skipInteractive)
{
StringTableEntry cwdSave = Platform::getCurrentDirectory();
int len = strlen(module);
const char *skipMsg = skipInteractive ? "(skipping interactive tests)" : "";
// Indicate to the user what we're up to.
if (!len)
Con::printf("-- Running all unit tests %s", skipMsg);
else
Con::printf("-- Running %s tests %s",module, skipMsg);
for (TestRegistry* itr = TestRegistry::getFirst(); itr; itr = itr->getNext())
{
if (!len || !dStrnicmp(module,itr->getName(),len))
{
// Skip the test if it's interactive and we're in skipinteractive mode.
if(skipInteractive && itr->isInteractive())
continue;
// Otherwise, run the test!
Platform::setCurrentDirectory(Platform::getMainDotCsDir());
test(itr);
}
}
// Print out a nice report on how we did.
printStats();
Platform::setCurrentDirectory(cwdSave);
// And indicate our failure situation in the return value.
return !_failureCount;
}
} // Namespace

165
Engine/source/unit/test.h Normal file
View file

@ -0,0 +1,165 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef UNIT_UNITTESTING_H
#define UNIT_UNITTESTING_H
#ifndef _PLATFORMINTRINSICS_H_
# include "platform/platformIntrinsics.h"
#endif
namespace UnitTesting {
//-----------------------------------------------------------------------------
struct UnitMargin
{
static void Push(int margin);
static void Pop();
static int Current();
};
void UnitPrint(const char* msg);
//-----------------------------------------------------------------------------
class UnitTest {
int _testCount;
int _failureCount;
int _warningCount;
bool _lastTestResult;
public:
UnitTest();
virtual ~UnitTest() {};
/// Test an assertion and note if it has failed.
bool test(bool a,const char* msg) {
dFetchAndAdd( _testCount, 1 );
if (!a)
fail(msg);
_lastTestResult = a;
return a;
}
/// Report a failture condition.
void fail(const char* msg);
/// Report a warning
void warn(const char* msg);
int getTestCount() const { return _testCount; }
int getFailureCount() const { return _failureCount; }
int getWarningCount() const { return _warningCount; }
bool lastTestPassed() const { return _lastTestResult; }
/// Implement this with the specific test.
virtual void run() = 0;
};
//-----------------------------------------------------------------------------
class TestRegistry
{
friend class DynamicTestRegistration; // Bless me, Father, for I have sinned, but this is damn cool
static TestRegistry *_list;
TestRegistry *_next;
const char *_name;
const char *_className;
bool _isInteractive;
public:
TestRegistry(const char* name, bool interactive, const char *className);
virtual ~TestRegistry() {}
static TestRegistry* getFirst() { return _list; }
TestRegistry* getNext() { return _next; }
const char* getName() { return _name; }
const bool isInteractive() { return _isInteractive; }
virtual UnitTest* newTest() = 0;
};
template<class T>
class TestRegistration: public TestRegistry
{
public:
virtual ~TestRegistration()
{
}
TestRegistration(const char* name, bool interactive, const char *className)
: TestRegistry(name, interactive, className)
{
}
virtual UnitTest* newTest()
{
return new T;
}
};
class DynamicTestRegistration : public TestRegistry
{
UnitTest *mUnitTest;
public:
DynamicTestRegistration( const char *name, UnitTest *test );
virtual ~DynamicTestRegistration();
virtual UnitTest *newTest() { return mUnitTest; }
};
//-----------------------------------------------------------------------------
class TestRun {
int _testCount;
int _subCount;
int _failureCount;
int _warningCount;
void test(TestRegistry* reg);
public:
TestRun();
void printStats();
bool test(const char* module, bool skipInteractive = false);
};
#define CreateUnitTest(Class,Name) \
class Class; \
static UnitTesting::TestRegistration<Class> _UnitTester##Class (Name, false, #Class); \
class Class : public UnitTesting::UnitTest
#define CreateInteractiveTest(Class,Name) \
class Class; \
static UnitTesting::TestRegistration<Class> _UnitTester##Class (Name, true, #Class); \
class Class : public UnitTesting::UnitTest
} // Namespace
#endif

View file

@ -0,0 +1,143 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "unit/test.h"
#include "unit/memoryTester.h"
#include "component/simComponent.h"
using namespace UnitTesting;
//////////////////////////////////////////////////////////////////////////
class CachedInterfaceExampleComponent : public SimComponent
{
typedef SimComponent Parent;
ComponentProperty<U32> mMyId;
static U32 smNumInstances;
ComponentProperty<U32> *mpU32; // CodeReview [patw, 2, 17, 2007] Make ref objects when this is in Jugg
public:
DECLARE_CONOBJECT( CachedInterfaceExampleComponent );
CachedInterfaceExampleComponent() : mpU32( NULL )
{
mMyId = ( ( 1 << 24 ) | smNumInstances++ );
}
virtual ~CachedInterfaceExampleComponent()
{
smNumInstances--;
}
public:
//////////////////////////////////////////////////////////////////////////
virtual void registerInterfaces( const SimComponent *owner )
{
// Register a cached interface for this
registerCachedInterface( NULL, "aU32", this, &mMyId );
}
//////////////////////////////////////////////////////////////////////////
bool onComponentRegister( SimComponent *owner )
{
// Call up to the parent first
if( !Parent::onComponentRegister( owner ) )
return false;
// We want to get an interface from another object in our containing component
// to simulate component interdependency.
ComponentInterfaceList list;
// Enumerate the interfaces on the owner, only ignore interfaces that this object owns
if( !_getOwner()->getInterfaces( &list, NULL, "aU32", this, true ) )
return false;
// Sanity check before just assigning all willy-nilly
for( ComponentInterfaceListIterator i = list.begin(); i != list.end(); i++ )
{
mpU32 = dynamic_cast<ComponentProperty<U32> *>( (*i) );
if( mpU32 != NULL )
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////////////
// CodeReview [patw, 2, 17, 2007] I'm going to make another lightweight interface
// for this functionality later
void unit_test( UnitTest *test )
{
test->test( mpU32 != NULL, "Pointer to dependent interface is NULL" );
test->test( *(*mpU32) & ( 1 << 24 ), "Pointer to interface data is bogus." );
test->test( *(*mpU32) != *mMyId, "Two of me have the same ID, bad!" );
}
};
IMPLEMENT_CONOBJECT( CachedInterfaceExampleComponent );
U32 CachedInterfaceExampleComponent::smNumInstances = 0;
ConsoleDocClass( CachedInterfaceExampleComponent,
"@brief Legacy from older component system.\n\n"
"Not intended for game development, for editors or internal use only.\n\n "
"@internal");
//////////////////////////////////////////////////////////////////////////
CreateUnitTest(TestComponentInterfacing, "Components/Composition")
{
void run()
{
MemoryTester m;
m.mark();
SimComponent *testComponent = new SimComponent();
CachedInterfaceExampleComponent *componentA = new CachedInterfaceExampleComponent();
CachedInterfaceExampleComponent *componentB = new CachedInterfaceExampleComponent();
// Register sub-components
test( componentA->registerObject(), "Failed to register componentA" );
test( componentB->registerObject(), "Failed to register componentB" );
// Add the components
test( testComponent->addComponent( componentA ), "Failed to add component a to testComponent" );
test( testComponent->addComponent( componentB ), "Failed to add component b to testComponent" );
test( componentA->getOwner() == testComponent, "testComponent did not properly set the mOwner field of componentA to NULL." );
test( componentB->getOwner() == testComponent, "testComponent did not properly set the mOwner field of componentB to NULL." );
// Register the object with the simulation, kicking off the interface registration
test( testComponent->registerObject(), "Failed to register testComponent" );
// Interface tests
componentA->unit_test( this );
componentB->unit_test( this );
testComponent->deleteObject();
test( m.check(), "Component composition test leaked memory." );
}
};

View file

@ -0,0 +1,67 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "unit/test.h"
#include "console/simObject.h"
using namespace UnitTesting;
// Test to ensure that all console classes in the system are default-constructible.
CreateUnitTest( TestDefaultConstruction, "Console/DefaultConstruction" )
{
void run()
{
for( AbstractClassRep* classRep = AbstractClassRep::getClassList();
classRep != NULL;
classRep = classRep->getNextClass() )
{
// Create object.
ConsoleObject* object = classRep->create();
test( object, avar( "AbstractClassRep::create failed for class '%s'", classRep->getClassName() ) );
if( !object )
continue;
// Make sure it's a SimObject.
SimObject* simObject = dynamic_cast< SimObject* >( object );
if( !simObject )
{
SAFE_DELETE( object );
continue;
}
// Register the object.
bool result = simObject->registerObject();
test( result, avar( "registerObject failed for object of class '%s'", classRep->getClassName() ) );
if( result )
simObject->deleteObject();
else
SAFE_DELETE( simObject );
}
}
};

View file

@ -0,0 +1,116 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "unit/test.h"
#include "math/mMath.h"
#include "math/mRandom.h"
extern void default_matF_x_matF_C(const F32 *a, const F32 *b, F32 *mresult);
extern void mInstallLibrary_ASM();
// If we're x86 and not Mac, then include these. There's probably a better way to do this.
#if defined(TORQUE_CPU_X86) && !defined(TORQUE_OS_MAC)
extern "C" void Athlon_MatrixF_x_MatrixF(const F32 *matA, const F32 *matB, F32 *result);
extern "C" void SSE_MatrixF_x_MatrixF(const F32 *matA, const F32 *matB, F32 *result);
#endif
#if defined( __VEC__ )
extern void vec_MatrixF_x_MatrixF(const F32 *matA, const F32 *matB, F32 *result);
#endif
using namespace UnitTesting;
CreateUnitTest( TestMatrixMul, "Math/Matrix/Multiply" )
{
// The purpose of this test is to verify that the matrix multiplication operation
// always agrees with the different implementations of itself within a reasonable
// epsilon.
void run()
{
F32 m1[16], m2[16], mrC[16];
// I am not positive that the best way to do this is to use random numbers
// but I think that using some kind of standard matrix may not always catch
// all problems.
for( int i = 0; i < 16; i++ )
{
m1[i] = gRandGen.randF();
m2[i] = gRandGen.randF();
}
// C will be the baseline
default_matF_x_matF_C( m1, m2, mrC );
#if defined(TORQUE_CPU_X86) && !defined(TORQUE_OS_MAC)
// Check the CPU info
U32 cpuProperties = Platform::SystemInfo.processor.properties;
bool same = true;
// Test 3D NOW! if it is available
F32 mrAMD[16];
if( cpuProperties & CPU_PROP_3DNOW )
{
Athlon_MatrixF_x_MatrixF( m1, m2, mrAMD );
for( int i = 0; i < 16; i++ )
same &= mIsEqual( mrC[i], mrAMD[i] );
test( same, "Matrix multiplication verification failed. (C vs. 3D NOW!)" );
}
else
warn( "Could not test 3D NOW! matrix multiplication because CPU does not support 3D NOW!." );
same = true;
// Test SSE if it is available
F32 mrSSE[16];
if( cpuProperties & CPU_PROP_SSE )
{
SSE_MatrixF_x_MatrixF( m1, m2, mrSSE );
for( int i = 0; i < 16; i++ )
same &= mIsEqual( mrC[i], mrSSE[i] );
test( same, "Matrix multiplication verification failed. (C vs. SSE)" );
}
else
warn( "Could not test SSE matrix multiplication because CPU does not support SSE." );
same = true;
#endif
// If Altivec exists, test it!
#if defined( __VEC__ )
bool same = false;
F32 mrVEC[16];
vec_MatrixF_x_MatrixF( m1, m2, mrVEC );
for( int i = 0; i < 16; i++ )
same &= isEqual( mrC[i], mrVEC[i] );
test( same, "Matrix multiplication verification failed. (C vs. Altivec)" );
#else
warn( "Could not test Altivec matrix multiplication because CPU does not support Altivec." );
#endif
}
};

View file

@ -0,0 +1,102 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "console/simBase.h"
#include "console/consoleTypes.h"
#include "console/runtimeClassRep.h"
#include "unit/test.h"
using namespace UnitTesting;
//-----------------------------------------------------------------------------
class RuntimeRegisteredSimObject : public SimObject
{
typedef SimObject Parent;
protected:
bool mFoo;
public:
RuntimeRegisteredSimObject() : mFoo( false ) {};
DECLARE_RUNTIME_CONOBJECT(RuntimeRegisteredSimObject);
static void initPersistFields();
};
IMPLEMENT_RUNTIME_CONOBJECT(RuntimeRegisteredSimObject);
void RuntimeRegisteredSimObject::initPersistFields()
{
addField( "fooField", TypeBool, Offset( mFoo, RuntimeRegisteredSimObject ) );
}
//-----------------------------------------------------------------------------
CreateUnitTest( RuntimeClassRepUnitTest, "Console/RuntimeClassRep" )
{
void run()
{
// First test to make sure that the test class is not registered (don't know how it could be, but that's programming for you)
test( !RuntimeRegisteredSimObject::dynRTClassRep.isRegistered(), "RuntimeRegisteredSimObject class was already registered with the console" );
// This should not be able to find the class, and return null (this may AssertWarn as well)
ConsoleObject *conobj = ConsoleObject::create( "RuntimeRegisteredSimObject" );
test( conobj == NULL, "AbstractClassRep returned non-NULL value! That is really bad!" );
// Register with console system
RuntimeRegisteredSimObject::dynRTClassRep.consoleRegister();
// Make sure that the object knows it's registered
test( RuntimeRegisteredSimObject::dynRTClassRep.isRegistered(), "RuntimeRegisteredSimObject class failed console registration" );
// Now try again to create the instance
conobj = ConsoleObject::create( "RuntimeRegisteredSimObject" );
test( conobj != NULL, "AbstractClassRep::create method failed!" );
// Cast the instance, and test it
RuntimeRegisteredSimObject *rtinst = dynamic_cast<RuntimeRegisteredSimObject *>( conobj );
test( rtinst != NULL, "Casting failed for some reason" );
// Register it
rtinst->registerObject( "_utRRTestObject" );
test( rtinst->isProperlyAdded(), "registerObject failed on test object" );
// Now execute some script on it
Con::evaluate( "_utRRTestObject.fooField = true;" );
// Test to make sure field worked
test( dAtob( rtinst->getDataField( StringTable->insert( "fooField" ), NULL ) ), "Script test failed!" );
// BALETED
rtinst->deleteObject();
// Unregister the class
RuntimeRegisteredSimObject::dynRTClassRep.consoleUnRegister();
// And make sure we can't create another one
conobj = ConsoleObject::create( "RuntimeRegisteredSimObject" );
test( conobj == NULL, "Unregistration of type failed" );
}
};

View file

@ -0,0 +1,126 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "unit/test.h"
#include "unit/memoryTester.h"
#include "core/util/swizzle.h"
#include "math/mRandom.h"
using namespace UnitTesting;
class TestStruct
{
private:
static U32 smIdx;
U32 mIdx;
U32 mData;
public:
TestStruct( const S32 data = -1 ) : mData( data ), mIdx( smIdx++ ) {};
dsize_t Idx() const { return mIdx; }
U32 Data() const { return mData; }
void Data(U32 val) { mData = val; }
};
U32 TestStruct::smIdx = 0;
CreateUnitTest(TestSwizzle, "Utils/Swizzle")
{
void run()
{
//------------------------------------------------------------------------
// Debugger-Observable Functionality Tests
//------------------------------------------------------------------------
U8 simpleBuffer[] = { 0, 1, 2, 3 };
U8 simpleTest[] = { 0, 1, 2, 3 };
#define RESET_SIMPLE() dMemcpy( simpleTest, simpleBuffer, sizeof( simpleBuffer ) )
//------------------------------------------------------------------------
// No-switch test
dsize_t noSwzl4[] = { 0, 1, 2, 3 };
Swizzle<U8,4> noSwizzle4( noSwzl4 );
noSwizzle4.InPlace( simpleTest, sizeof( simpleTest ) );
test( dMemcmp( simpleTest, simpleBuffer, sizeof( simpleBuffer ) ) == 0, "No-switch test failed!" );
RESET_SIMPLE();
//------------------------------------------------------------------------
// No-brainer RGBA->BGRA test
dsize_t bgraSwzl[] = { 2, 1, 0, 3 };
Swizzle<U8,4> bgraSwizzle( bgraSwzl );
U8 bgraTest[] = { 2, 1, 0, 3 };
bgraSwizzle.InPlace( simpleTest, sizeof( simpleTest ) );
test( dMemcmp( simpleTest, bgraTest, sizeof( bgraTest ) ) == 0, "U8 RGBA->BGRA test failed" );
//------------------------------------------------------------------------
// Reverse test
bgraSwizzle.InPlace( simpleTest, sizeof( simpleTest ) );
test( dMemcmp( simpleTest, simpleBuffer, sizeof( simpleBuffer ) ) == 0, "U8 RGBA->BGRA reverse test failed" );
RESET_SIMPLE();
//------------------------------------------------------------------------
// Object support test
Swizzle<TestStruct,4> bgraObjSwizzle( bgraSwzl );
{
U32 objIdx[] = { 0, 1, 2, 3 };
FrameTemp<TestStruct> objTest( sizeof( objIdx ) / sizeof( U32 ) );
FrameTemp<U32> randData( sizeof( objIdx ) / sizeof( U32 ) );
bool same = true;
for( U32 i = 0; i < sizeof( objIdx ) / sizeof( U32 ); i++ )
{
// Make random data and assign it
randData[i] = gRandGen.randI();
objTest[i].Data( randData[i] );
// Continue object sanity check
same &= ( objTest[i].Idx() == objIdx[i] );
}
test( same, "Test object failed to be competent" );
bgraObjSwizzle.InPlace( ~objTest, sizeof( TestStruct ) * ( sizeof( objIdx ) / sizeof( U32 ) ) );
same = true;
for( U32 i = 0; i < sizeof( objIdx ) / sizeof( U32 ); i++ )
same &= ( objTest[i].Idx() == bgraTest[i] ) && ( objTest[i].Data() == randData[ (U32)bgraTest[ i ] ] );
test( same, "Object RGBA->BGRA test failed." );
bgraObjSwizzle.InPlace( ~objTest, sizeof( TestStruct ) * ( sizeof( objIdx ) / sizeof( U32 ) ) );
same = true;
for( U32 i = 0; i < sizeof( objIdx ) / sizeof( U32 ); i++ )
same &= ( objTest[i].Idx() == objIdx[i] ) && ( objTest[i].Data() == randData[i] );
test( same, "Object RGBA->BGRA reverse test failed." );
}
}
};

View file

@ -0,0 +1,101 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "unit/test.h"
#include "core/threadStatic.h"
#include "unit/memoryTester.h"
using namespace UnitTesting;
//-----------------------------------------------------------------------------
// This unit test will blow up without thread static support
#ifdef TORQUE_ENABLE_THREAD_STATICS
// Declare a test thread static
DITTS( U32, gUnitTestFoo, 42 );
DITTS( F32, gUnitTestF32, 1.0 );
CreateUnitTest( TestThreadStatic, "Core/ThreadStatic" )
{
void run()
{
MemoryTester m;
m.mark();
// ThreadStatic list should be initialized right now, so lets see if it has
// any entries.
test( !_TorqueThreadStaticReg::getStaticList().empty(), "Self-registration has failed, or no statics declared" );
// Spawn a new copy.
TorqueThreadStaticListHandle testInstance = _TorqueThreadStaticReg::spawnThreadStaticsInstance();
// Test the copy
test( _TorqueThreadStaticReg::getStaticList( 0 ).size() == testInstance->size(), "Spawned static list has a different size from master copy." );
// Make sure the size test passed before this is attempted
if( lastTestPassed() )
{
// Traverse the list and compare it to the initial value copy (index 0)
for( int i = 0; i < _TorqueThreadStaticReg::getStaticList().size(); i++ )
{
_TorqueThreadStatic *master = _TorqueThreadStaticReg::getStaticList()[i];
_TorqueThreadStatic *cpy = (*testInstance)[i];
// Make sure it is not the same memory
test( master != cpy, "Copy not spawned properly." );
// Make sure the sizes are the same
test( master->getMemInstSize() == cpy->getMemInstSize(), "Size mismatch between master and copy" );
// Make sure the initialization occurred properly
if( lastTestPassed() )
test( dMemcmp( master->getMemInstPtr(), cpy->getMemInstPtr(), master->getMemInstSize() ) == 0, "Initial value for spawned list is not correct" );
}
}
// Test metrics if enabled
#ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
U32 fooHitCount = (*testInstance)[_gUnitTestFooTorqueThreadStatic::getListIndex()]->getHitCount();
#endif
// Set/get some values (If we test static metrics, this is hit 1)
ATTS_(gUnitTestFoo, 1) = 55;
// Test to see that the master copy and the spawned copy differ
// (If we test metrics, this is hit 2, for the instance, and hit 1 for the master)
test( ATTS_(gUnitTestFoo, 0) != ATTS_(gUnitTestFoo, 1 ) , "Assignment for spawned instanced memory failed" );
#ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
U32 fooHitCount2 = (*testInstance)[_gUnitTestFooTorqueThreadStatic::getListIndex()]->getHitCount();
test( fooHitCount2 == ( fooHitCount + 2 ), "Thread static metric hit count failed" );
#endif
// Destroy instances
_TorqueThreadStaticReg::destroyInstance( testInstance );
// Now test the cleanup
test( m.check(), "Memory leak in dynamic static allocation stuff." );
}
};
#endif // TORQUE_ENABLE_THREAD_STATICS

View file

@ -0,0 +1,82 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "unit/test.h"
#include "core/threadStatic.h"
#include "math/mRandom.h"
#include "platform/profiler.h"
using namespace UnitTesting;
//-----------------------------------------------------------------------------
// This unit test will blow up without thread static support
#if defined(TORQUE_ENABLE_THREAD_STATICS) && defined(TORQUE_ENABLE_PROFILER)
// Declare a test thread static
DITTS( U32, gInstancedStaticFoo, 42 );
static U32 gTrueStaticFoo = 42;
CreateUnitTest( TestThreadStaticPerformance, "Core/ThreadStaticPerformance" )
{
void run()
{
// Bail if the profiler is turned on right now
if( !test( !gProfiler->isEnabled(), "Profiler is currently enabled, test cannot continue" ) )
return;
// Spawn an instance
TorqueThreadStaticListHandle testInstance = _TorqueThreadStaticReg::spawnThreadStaticsInstance();
static const dsize_t TEST_SIZE = 100000;
// What we are going to do in this test is to test some U32 static
// performance. The test will be run TEST_SIZE times, and so first create
// an array of values to standardize the tests on.
U32 testValue[TEST_SIZE];
for( int i = 0; i < TEST_SIZE; i++ )
testValue[i] = gRandGen.randI();
// Reset the profiler, tell it to dump to console when done
gProfiler->dumpToConsole();
gProfiler->enable( true );
// Value array is initialized, so lets put the foo's through the paces
PROFILE_START(ThreadStaticPerf_TrueStaticAssign);
for( int i = 0; i < TEST_SIZE; i++ )
gTrueStaticFoo = testValue[i];
PROFILE_END();
PROFILE_START(ThreadStaticPerf_InstanceStaticAssign);
for( int i = 0; i < TEST_SIZE; i++ )
ATTS_( gInstancedStaticFoo, 1 ) = testValue[i];
PROFILE_END();
gProfiler->enable( false );
// Clean up instance
_TorqueThreadStaticReg::destroyInstance( testInstance );
}
};
#endif // TORQUE_ENABLE_THREAD_STATICS

View file

@ -0,0 +1,53 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "unit/test.h"
#include "unit/memoryTester.h"
#include "core/util/tVector.h"
using namespace UnitTesting;
CreateUnitTest(TestVectorAllocate, "Types/Vector")
{
void run()
{
MemoryTester m;
m.mark();
Vector<S32> *vector = new Vector<S32>;
for(S32 i=0; i<1000; i++)
vector->push_back(10000 + i);
// Erase the first element, 500 times.
for(S32 i=0; i<500; i++)
vector->erase(U32(0));
vector->compact();
test(vector->size() == 500, "Vector was unexpectedly short!");
delete vector;
test(m.check(), "Vector allocation test leaked memory!");
}
};

View file

@ -0,0 +1,24 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "unit/unitTestComponentInterface.h"

View file

@ -0,0 +1,91 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _UNITTESTCOMPONENTINTERFACE_H_
#define _UNITTESTCOMPONENTINTERFACE_H_
#include "unit/test.h"
#include "component/simComponent.h"
#include "component/componentInterface.h"
// This is commented out because I want to keep the explicit namespace referencing
// so that the multiple inheritances from UnitTest doesn't screw anyone up. It will
// also make for more readable code in the derived test-interfaces. -patw
//using namespace UnitTesting;
/// This is a class that will make it very easy for a component author to provide
/// unit testing functionality from within an instantiated component.
class UnitTestComponentInterface : public ComponentInterface, UnitTesting::UnitTest
{
typedef ComponentInterface Parent;
private:
StringTableEntry mName;
UnitTesting::DynamicTestRegistration *mTestReg;
// Constructors/Destructors
public:
UnitTestComponentInterface( const char *name )
{
mName = StringTable->insert( name );
mTestReg = new UnitTesting::DynamicTestRegistration( name, this );
}
virtual ~UnitTestComponentInterface()
{
delete mTestReg;
}
// ComponentInterface overrides
public:
virtual bool isValid() const
{
return Parent::isValid() && ( mTestReg != NULL );
}
// UnitTest overrides
public:
/// This is the only function you need to overwrite to add a unit test interface
/// your component.
virtual void run() = 0;
};
// Macros
#ifndef TORQUE_DEBUG
# define DECLARE_UNITTEST_INTERFACE(x)
# define CACHE_UNITTEST_INTERFACE(x)
#else
//-----------------------------------------------------------------------------
# define DECLARE_UNITTEST_INTERFACE(x) \
class x##_UnitTestInterface : public UnitTestComponentInterface \
{\
typedef UnitTestComponentInterface Parent; \
public: \
x##_UnitTestInterface() : UnitTestComponentInterface( #x ) {}; \
virtual void run(); \
} _##x##UnitTestInterface
//-----------------------------------------------------------------------------
# define CACHE_UNITTEST_INTERFACE(x) registerCachedInterface( "unittest", #x, this, &_##x##UnitTestInterface )
#endif
#endif