separate testing environment

-Separate main for running unit tests
-Move unit tests into testing folder
This commit is contained in:
marauder2k7 2023-07-24 12:38:36 +01:00
parent 2e8f5795fa
commit c09f79d199
265 changed files with 84537 additions and 334 deletions

View file

@ -1,70 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 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.
//-----------------------------------------------------------------------------
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/threads/mutex.h"
#include "platform/threads/thread.h"
TEST(Mutex, BasicSynchronization)
{
// We test various scenarios wrt to locking and unlocking, in a single
// thread, just to make sure our basic primitives are working in the
// most basic case.
void *mutex1 = Mutex::createMutex();
EXPECT_TRUE(mutex1 != NULL)
<< "First Mutex::createMutex call failed - that's pretty bad!";
// This mutex is intentionally unused.
void *mutex2 = Mutex::createMutex();
EXPECT_TRUE(mutex2 != NULL)
<< "Second Mutex::createMutex call failed - that's pretty bad, too!";
EXPECT_TRUE(Mutex::lockMutex(mutex1, false))
<< "Nonblocking call to brand new mutex failed - should not be.";
EXPECT_TRUE(Mutex::lockMutex(mutex1, true))
<< "Failed relocking a mutex from the same thread - should be able to do this.";
// Try to acquire the mutex from another thread.
struct thread
{
static void body(void* mutex)
{
// We should not be able to lock the mutex from a separate thread, but
// we don't want to block either.
EXPECT_FALSE(Mutex::lockMutex(mutex, false));
}
};
Thread thread(&thread::body, mutex1);
thread.start();
thread.join();
// Unlock & kill mutex 1
Mutex::unlockMutex(mutex1);
Mutex::unlockMutex(mutex1);
Mutex::destroyMutex(mutex1);
// Kill mutex2, which was never touched.
Mutex::destroyMutex(mutex2);
}
#endif

View file

@ -1,90 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 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.
//-----------------------------------------------------------------------------
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/threads/semaphore.h"
#include "platform/threads/thread.h"
TEST(Semaphore, BasicSynchronization)
{
Semaphore *sem1 = new Semaphore(1);
Semaphore *sem2 = new Semaphore(1);
// Test that we can do non-blocking acquires that succeed.
EXPECT_TRUE(sem1->acquire(false))
<< "Should succeed at acquiring a new semaphore with count 1.";
EXPECT_TRUE(sem2->acquire(false))
<< "This one should succeed too, see previous test.";
// Test that we can do non-blocking acquires that fail.
EXPECT_FALSE(sem1->acquire(false))
<< "Should failed, as we've already got the sem.";
sem1->release();
EXPECT_FALSE(sem2->acquire(false))
<< "Should also fail.";
sem2->release();
// Test that we can do blocking acquires that succeed.
EXPECT_TRUE(sem1->acquire(true))
<< "Should succeed as we just released.";
EXPECT_TRUE(sem2->acquire(true))
<< "Should succeed as we just released.";
// Clean up.
delete sem1;
delete sem2;
}
TEST(Semaphore, MultiThreadSynchronization)
{
Semaphore semaphore(1);
struct thread
{
// Try to acquire the semaphore from another thread.
static void body1(void* sem)
{
Semaphore *semaphore = reinterpret_cast<Semaphore*>(sem);
EXPECT_TRUE(semaphore->acquire(true));
// Note that this semaphore is never released. Bad programmer!
}
// One more acquisition should fail!
static void body2(void* sem)
{
Semaphore *semaphore = reinterpret_cast<Semaphore*>(sem);
EXPECT_FALSE(semaphore->acquire(false));
}
};
Thread thread1(&thread::body1, &semaphore);
EXPECT_TRUE(semaphore.acquire(true));
thread1.start();
semaphore.release();
thread1.join();
Thread thread2(&thread::body2, &semaphore);
thread2.start();
thread2.join();
}
#endif

View file

@ -1,121 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 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.
//-----------------------------------------------------------------------------
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/threads/threadPool.h"
#include "console/console.h"
#include "core/util/tVector.h"
FIXTURE(ThreadPool)
{
public:
// Represents a single unit of work. In this test we just set an element in
// a result vector.
struct TestItem : public ThreadPool::WorkItem
{
U32 mIndex;
Vector<U32>& mResults;
TestItem(U32 index, Vector<U32>& results)
: mIndex(index), mResults(results) {}
protected:
virtual void execute()
{
mResults[mIndex] = mIndex;
}
};
// A worker that delays for some time. We'll use this to test the ThreadPool's
// synchronous and asynchronous operations.
struct DelayItem : public ThreadPool::WorkItem
{
U32 ms;
DelayItem(U32 _ms) : ms(_ms) {}
protected:
virtual void execute()
{
Platform::sleep(ms);
}
};
};
TEST_FIX(ThreadPool, BasicAPI)
{
// Construct the vector of results from the work items.
const U32 numItems = 100;
Vector<U32> results(__FILE__, __LINE__);
results.setSize(numItems);
for (U32 i = 0; i < numItems; i++)
results[i] = U32(-1);
// Launch the work items.
ThreadPool* pool = &ThreadPool::GLOBAL();
for (U32 i = 0; i < numItems; i++)
{
ThreadSafeRef<TestItem> item(new TestItem(i, results));
pool->queueWorkItem(item);
}
pool->waitForAllItems();
// Verify.
for (U32 i = 0; i < numItems; i++)
EXPECT_EQ(results[i], i) << "result mismatch";
results.clear();
}
TEST_FIX(ThreadPool, Asynchronous)
{
const U32 delay = 500; //ms
// Launch a single delaying work item.
ThreadPool* pool = &ThreadPool::GLOBAL();
ThreadSafeRef<DelayItem> item(new DelayItem(delay));
pool->queueWorkItem(item);
// The thread should not yet be finished.
EXPECT_EQ(false, item->hasExecuted());
// Wait til the item should have completed.
Platform::sleep(delay * 2);
EXPECT_EQ(true, item->hasExecuted());
}
TEST_FIX(ThreadPool, Synchronous)
{
const U32 delay = 500; //ms
// Launch a single delaying work item.
ThreadPool* pool = &ThreadPool::GLOBAL();
ThreadSafeRef<DelayItem> item(new DelayItem(delay));
pool->queueWorkItem(item);
// Wait for the item to complete.
pool->waitForAllItems();
EXPECT_EQ(true, item->hasExecuted());
}
#endif

View file

@ -1,228 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 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.
//-----------------------------------------------------------------------------
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/threads/threadSafeDeque.h"
#include "platform/threads/thread.h"
#include "core/util/tVector.h"
#include "console/console.h"
FIXTURE(ThreadSafeDeque)
{
public:
// Used by the concurrent test.
struct Value : public ThreadSafeRefCount<Value>
{
U32 mIndex;
U32 mTick;
Value() {}
Value(U32 index, U32 tick)
: mIndex(index), mTick(tick) {}
};
typedef ThreadSafeRef<Value> ValueRef;
struct Deque : public ThreadSafeDeque<ValueRef>
{
typedef ThreadSafeDeque<ValueRef> Parent;
U32 mPushIndex;
U32 mPopIndex;
Deque()
: mPushIndex(0), mPopIndex(0) {}
void pushBack(const ValueRef& value)
{
EXPECT_EQ(value->mIndex, mPushIndex) << "index out of line";
mPushIndex++;
Parent::pushBack(value);
}
bool tryPopFront(ValueRef& outValue)
{
if(Parent::tryPopFront(outValue))
{
EXPECT_EQ(outValue->mIndex, mPopIndex) << "index out of line";
mPopIndex++;
return true;
}
else
return false;
}
};
struct ProducerThread : public Thread
{
Vector<U32>& mValues;
Deque& mDeque;
ProducerThread(Vector<U32>& values, Deque& deque)
: mValues(values), mDeque(deque) {}
virtual void run(void*)
{
for(U32 i = 0; i < mValues.size(); i++)
{
U32 tick = Platform::getRealMilliseconds();
mValues[i] = tick;
ValueRef val = new Value(i, tick);
mDeque.pushBack(val);
}
}
};
struct ConsumerThread : public Thread
{
Vector<U32>& mValues;
Deque& mDeque;
ConsumerThread(Vector<U32>& values, Deque& deque)
: mValues(values), mDeque(deque) {}
virtual void run(void*)
{
S32 timeOut = mValues.size() * 32;
U32 endTime = Platform::getRealMilliseconds() + timeOut;
for (U32 i = 0; i < mValues.size(); i++)
{
ValueRef value;
bool timedOut = false;
while (!mDeque.tryPopFront(value))
{
if (timeOut && Platform::getRealMilliseconds() >= endTime)
{
timedOut = true;
break;
}
};
ASSERT_FALSE(timedOut)
<< "consumer thread timed out!";
if (timedOut) return;
EXPECT_EQ(i, value->mIndex);
EXPECT_EQ(value->mTick, mValues[i]);
}
}
};
};
// Test deque without concurrency.
TEST_FIX(ThreadSafeDeque, PopFront)
{
ThreadSafeDeque<char> deque;
String str = "teststring";
for(U32 i = 0; i < str.length(); i++)
deque.pushBack(str[i]);
EXPECT_FALSE(deque.isEmpty());
char ch;
for(U32 i = 0; i < str.length(); i++)
{
EXPECT_TRUE(deque.tryPopFront(ch));
EXPECT_EQ(str[i], ch);
}
ASSERT_TRUE(deque.isEmpty());
}
TEST_FIX(ThreadSafeDeque, PopBack)
{
ThreadSafeDeque<char> deque;
String str = "teststring";
const char* p1 = str.c_str() + 4;
const char* p2 = p1 + 1;
while(*p2)
{
deque.pushFront(*p1);
deque.pushBack(*p2);
--p1;
++p2;
}
char ch;
for(S32 i = str.length()-1; i >= 0; i--)
{
EXPECT_TRUE(deque.tryPopBack(ch));
EXPECT_EQ(str[i], ch);
}
ASSERT_TRUE(deque.isEmpty());
}
// Test deque in a concurrent setting.
// Test many items in a row
TEST_FIX(ThreadSafeDeque, Concurrent1)
{
const U32 NumValues = 100;
Deque mDeque;
Vector<U32> mValues;
mValues.setSize(NumValues);
ProducerThread pThread(mValues, mDeque);
ConsumerThread cThread(mValues, mDeque);
pThread.start();
cThread.start();
pThread.join();
cThread.join();
mValues.clear();
};
/*
// Test a few items many times to catch any race-condition in start-up
TEST_FIX(ThreadSafeDeque, Concurrent2)
{
for (int i = 0; i < 10000; ++i)
{
Deque mDeque;
Vector<U32> mValues;
mValues.setSize(5);
ProducerThread pThread(mValues, mDeque);
ConsumerThread cThread(mValues, mDeque);
cThread.start();
pThread.start();
pThread.join();
cThread.join();
mValues.clear();
if (::testing::Test::HasFailure()) break;
}
};
*/
#endif

View file

@ -1,146 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 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.
//-----------------------------------------------------------------------------
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/threads/threadSafePriorityQueue.h"
#include "platform/threads/thread.h"
#include "core/util/tVector.h"
#include "console/console.h"
// Test queue without concurrency.
TEST(ThreadSafePriorityQueue, Serial)
{
const U32 min = 0;
const U32 max = 9;
const U32 len = 11;
U32 indices[len] = { 2, 7, 4, 6, 1, 5, 3, 8, 6, 9, 0};
F32 priorities[len] = {0.2f, 0.7f, 0.4f, 0.6f, 0.1f, 0.5f, 0.3f, 0.8f, 0.6f, 0.9f, 0.0f};
ThreadSafePriorityQueue<U32, F32, true> minQueue;
ThreadSafePriorityQueue<U32, F32, false> maxQueue;
for(U32 i = 0; i < len; i++)
{
minQueue.insert(priorities[i], indices[i]);
maxQueue.insert(priorities[i], indices[i]);
}
EXPECT_FALSE(minQueue.isEmpty());
EXPECT_FALSE(maxQueue.isEmpty());
U32 index = min;
for(U32 i = 0; i < len; i++)
{
U32 popped;
EXPECT_TRUE(minQueue.takeNext(popped))
<< "Failed to pop element from minQueue";
EXPECT_LE(index, popped)
<< "Element from minQueue was not in sort order";
index = popped;
}
index = max;
for(U32 i = 0; i < len; i++)
{
U32 popped;
EXPECT_TRUE(maxQueue.takeNext(popped))
<< "Failed to pop element from maxQueue";
EXPECT_GE(index, popped)
<< "Element from maxQueue was not in sort order";
index = popped;
}
}
// Test queue with concurrency.
TEST(ThreadSafePriorityQueue, Concurrent)
{
#define MIN 0
#define MAX 9
#define LEN 11
typedef ThreadSafePriorityQueue<U32, F32, true> MinQueue;
typedef ThreadSafePriorityQueue<U32, F32, false> MaxQueue;
struct ProducerThread : public Thread
{
MinQueue& minQueue;
MaxQueue& maxQueue;
ProducerThread(MinQueue& min, MaxQueue& max)
: minQueue(min), maxQueue(max) {}
virtual void run(void*)
{
U32 indices[LEN] = { 2, 7, 4, 6, 1, 5, 3, 8, 6, 9, 0};
F32 priorities[LEN] = {0.2f, 0.7f, 0.4f, 0.6f, 0.1f, 0.5f, 0.3f, 0.8f, 0.6f, 0.9f, 0.0f};
for(U32 i = 0; i < LEN; i++)
{
minQueue.insert(priorities[i], indices[i]);
maxQueue.insert(priorities[i], indices[i]);
}
}
};
MinQueue minQueue;
MaxQueue maxQueue;
ProducerThread producers[] = {
ProducerThread(minQueue, maxQueue),
ProducerThread(minQueue, maxQueue),
ProducerThread(minQueue, maxQueue)
};
const U32 len = sizeof(producers) / sizeof(ProducerThread);
for(U32 i = 0; i < len; i++)
producers[i].start();
for(U32 i = 0; i < len; i++)
producers[i].join();
U32 index = MIN;
for(U32 i = 0; i < LEN * len; i++)
{
U32 popped;
EXPECT_TRUE(minQueue.takeNext(popped))
<< "Failed to pop element from minQueue";
EXPECT_LE(index, popped)
<< "Element from minQueue was not in sort order";
index = popped;
}
index = MAX;
for(U32 i = 0; i < LEN * len; i++)
{
U32 popped;
EXPECT_TRUE(maxQueue.takeNext(popped))
<< "Failed to pop element from maxQueue";
EXPECT_GE(index, popped)
<< "Element from maxQueue was not in sort order";
index = popped;
}
#undef MIN
#undef MAX
#undef LEN
}
#endif

View file

@ -1,205 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 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.
//-----------------------------------------------------------------------------
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/threads/threadSafeRefCount.h"
#include "platform/threads/thread.h"
#include "core/util/tVector.h"
#include "console/console.h"
FIXTURE(ThreadSafeRefCount)
{
public:
struct TestObjectDtor : public ThreadSafeRefCount<TestObjectDtor>
{
bool &flag;
TestObjectDtor(bool &f) : flag(f)
{
flag = false;
}
~TestObjectDtor()
{
flag = true;
}
};
typedef ThreadSafeRef<TestObjectDtor> TestObjectDtorRef;
enum
{
NUM_ADD_REFS_PER_THREAD = 10,
NUM_EXTRA_REFS_PER_THREAD = 10,
NUM_THREADS = 10
};
class TestObject : public ThreadSafeRefCount<TestObject> {};
typedef ThreadSafeRef<TestObject> TestObjectRef;
class TestThread : public Thread
{
public:
TestObjectRef mRef;
Vector<TestObjectRef> mExtraRefs;
TestThread(TestObjectRef ref) : mRef(ref) {}
void run(void* arg)
{
if (!arg)
{
// Create references.
for (U32 i = 0; i < NUM_ADD_REFS_PER_THREAD; i++)
mRef->addRef();
mExtraRefs.setSize(NUM_EXTRA_REFS_PER_THREAD);
for (U32 i = 0; i < NUM_EXTRA_REFS_PER_THREAD; i++)
mExtraRefs[i] = mRef;
}
else
{
// Clear references.
mExtraRefs.clear();
for (U32 i = 0; i < NUM_ADD_REFS_PER_THREAD; i++)
mRef->release();
}
}
};
};
TEST_FIX(ThreadSafeRefCount, Serial)
{
bool deleted = false;
TestObjectDtorRef ref1 = new TestObjectDtor(deleted);
ASSERT_FALSE(deleted);
EXPECT_FALSE(ref1->isShared());
EXPECT_TRUE(ref1 != NULL);
TestObjectDtorRef ref2 = ref1;
EXPECT_TRUE(ref1->isShared());
EXPECT_TRUE(ref2->isShared());
EXPECT_EQ(ref1, ref2);
ref1 = NULL;
EXPECT_FALSE(ref2->isShared());
ref2 = NULL;
ASSERT_TRUE(deleted);
}
TEST_FIX(ThreadSafeRefCount, Concurrent)
{
TestObjectRef mRef = new TestObject;
EXPECT_EQ(2, mRef->getRefCount()); // increments of 2
Vector<TestThread*> threads;
threads.setSize(NUM_THREADS);
// Create threads.
for (U32 i = 0; i < NUM_THREADS; i++)
threads[i] = new TestThread(mRef);
// Run phase 1: create references.
for (U32 i = 0; i < NUM_THREADS; i++)
threads[i]->start(NULL);
// Wait for completion.
for (U32 i = 0; i < NUM_THREADS; i++)
threads[i]->join();
EXPECT_EQ(2 + ((1 + NUM_ADD_REFS_PER_THREAD + NUM_EXTRA_REFS_PER_THREAD) * NUM_THREADS * 2),
mRef->getRefCount());
// Run phase 2: release references.
for (U32 i = 0; i < NUM_THREADS; i++)
threads[i]->start((void*) 1);
// Wait for completion.
for (U32 i = 0; i < NUM_THREADS; i++)
{
threads[i]->join();
delete threads[i];
}
EXPECT_EQ(2, mRef->getRefCount()); // increments of two
mRef = NULL;
}
TEST_FIX(ThreadSafeRefCount, Tagging)
{
TestObjectRef ref;
EXPECT_FALSE(ref.isTagged());
EXPECT_FALSE(bool(ref));
EXPECT_FALSE(bool(ref.ptr()));
EXPECT_TRUE(ref.trySetFromTo(ref, NULL));
EXPECT_FALSE(ref.isTagged());
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_Set));
EXPECT_TRUE(ref.isTagged());
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_Set));
EXPECT_TRUE(ref.isTagged());
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_Unset));
EXPECT_FALSE(ref.isTagged());
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_Unset));
EXPECT_FALSE(ref.isTagged());
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_SetOrFail));
EXPECT_TRUE(ref.isTagged());
EXPECT_FALSE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_SetOrFail));
EXPECT_TRUE(ref.isTagged());
EXPECT_FALSE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_FailIfSet));
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_UnsetOrFail));
EXPECT_FALSE(ref.isTagged());
EXPECT_FALSE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_UnsetOrFail));
EXPECT_FALSE(ref.isTagged());
EXPECT_FALSE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_FailIfUnset));
TestObjectRef objectA = new TestObject;
TestObjectRef objectB = new TestObject;
EXPECT_FALSE(objectA->isShared());
EXPECT_FALSE(objectB->isShared());
ref = objectA;
EXPECT_FALSE(ref.isTagged());
EXPECT_TRUE(ref == objectA);
EXPECT_TRUE(ref == objectA.ptr());
EXPECT_TRUE(objectA->isShared());
EXPECT_TRUE(ref.trySetFromTo(objectA, objectB, TestObjectRef::TAG_Set));
EXPECT_TRUE(ref.isTagged());
EXPECT_EQ(ref, objectB);
EXPECT_EQ(ref, objectB.ptr());
EXPECT_TRUE(objectB->isShared());
EXPECT_FALSE(objectA->isShared());
EXPECT_TRUE(ref.trySetFromTo(ref, objectA));
EXPECT_TRUE(ref.isTagged());
EXPECT_EQ(ref, objectA);
EXPECT_EQ(ref, objectA.ptr());
}
#endif

View file

@ -1,87 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 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.
//-----------------------------------------------------------------------------
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/threads/thread.h"
TEST(Thread, CallbackAPI)
{
#define VALUE_TO_SET 10
// This struct exists just so we can define run as a local function.
struct thread
{
// Do some work we can observe.
static void body(void* arg)
{
U32* value = reinterpret_cast<U32*>(arg);
*value = VALUE_TO_SET;
}
};
// Test most basic Thread API functions.
U32 value = ~VALUE_TO_SET;
Thread thread(&thread::body, reinterpret_cast<void*>(&value));
thread.start();
EXPECT_TRUE(thread.isAlive());
thread.join();
EXPECT_FALSE(thread.isAlive());
EXPECT_EQ(value, VALUE_TO_SET)
<< "Thread did not set expected value!";
#undef VALUE_TO_SET
}
TEST(Thread, InheritanceAPI)
{
#define VALUE_TO_SET 10
// This struct exists just so we can define run as a local function.
struct thread : public Thread
{
U32* mPtr;
thread(U32* ptr): mPtr(ptr) {}
// Do some work we can observe.
virtual void run(void*)
{
*mPtr = VALUE_TO_SET;
}
};
// Test most basic Thread API functions.
U32 value = ~VALUE_TO_SET;
thread thread(&value);
thread.start();
EXPECT_TRUE(thread.isAlive());
thread.join();
EXPECT_FALSE(thread.isAlive());
EXPECT_EQ(value, VALUE_TO_SET)
<< "Thread did not set expected value!";
#undef VALUE_TO_SET
}
#endif