Removed all local type definitions for GCC.

This commit is contained in:
Daniel Buckmaster 2014-09-29 14:31:07 +10:00
parent 6cc59a97cc
commit bedde94a9f
5 changed files with 194 additions and 168 deletions

View file

@ -25,22 +25,78 @@
#include "core/util/journal/journaledSignal.h" #include "core/util/journal/journaledSignal.h"
#include "core/util/safeDelete.h" #include "core/util/safeDelete.h"
TEST(Journal, BasicAPI) FIXTURE(Journal)
{ {
public:
// Used for basic API test.
struct receiver struct receiver
{ {
U32 lastTriggerValue; U16 lastTriggerValue;
void triggerReceiver(U16 msg) void trigger(U16 msg)
{ {
lastTriggerValue = msg; lastTriggerValue = msg;
} }
} rec; };
// Used for non-basic test.
typedef JournaledSignal<void(U32, U16)> EventA;
typedef JournaledSignal<void(U8, S8)> EventB;
typedef JournaledSignal<void(U32, S32)> EventC;
// Root, non-dynamic signal receiver.
struct multiReceiver {
U32 recvA, recvB, recvC;
EventA *dynamicA;
EventB *dynamicB;
EventC *dynamicC;
void receiverRoot(U8 msg)
{
if(msg==1)
{
dynamicA = new EventA();
dynamicA->notify(this, &multiReceiver::receiverA);
}
if(msg==2)
{
dynamicB = new EventB();
dynamicB->notify(this, &multiReceiver::receiverB);
}
if(msg==3)
{
dynamicC = new EventC();
dynamicC->notify(this, &multiReceiver::receiverC);
}
}
void receiverA(U32, U16 d)
{
recvA += d;
}
void receiverB(U8, S8 d)
{
recvB += d;
}
void receiverC(U32, S32 d)
{
recvC += d;
}
};
};
TEST_FIX(Journal, BasicAPI)
{
receiver rec;
rec.lastTriggerValue = 0; rec.lastTriggerValue = 0;
// Set up a journaled signal to test with. // Set up a journaled signal to test with.
JournaledSignal<void(U16)> testEvent; JournaledSignal<void(U16)> testEvent;
testEvent.notify(&rec, &receiver::triggerReceiver); testEvent.notify(&rec, &receiver::trigger);
// Initialize journal recording and fire off some events... // Initialize journal recording and fire off some events...
Journal::Record("test.jrn"); Journal::Record("test.jrn");
@ -71,63 +127,16 @@ TEST(Journal, BasicAPI)
<< "Should encounter last journaled value (18)."; << "Should encounter last journaled value (18).";
} }
TEST(Journal, DynamicSignals) TEST_FIX(Journal, DynamicSignals)
{ {
typedef JournaledSignal<void(U32, U16)> EventA; multiReceiver rec;
typedef JournaledSignal<void(U8, S8)> EventB;
typedef JournaledSignal<void(U32, S32)> EventC;
// Root, non-dynamic signal receiver.
struct receiver {
U32 recvA, recvB, recvC;
EventA *dynamicA;
EventB *dynamicB;
EventC *dynamicC;
void receiverRoot(U8 msg)
{
if(msg==1)
{
dynamicA = new EventA();
dynamicA->notify(this, &receiver::receiverA);
}
if(msg==2)
{
dynamicB = new EventB();
dynamicB->notify(this, &receiver::receiverB);
}
if(msg==3)
{
dynamicC = new EventC();
dynamicC->notify(this, &receiver::receiverC);
}
}
void receiverA(U32, U16 d)
{
recvA += d;
}
void receiverB(U8, S8 d)
{
recvB += d;
}
void receiverC(U32, S32 d)
{
recvC += d;
}
} rec;
// Reset our state values. // Reset our state values.
rec.recvA = rec.recvB = rec.recvC = 0; rec.recvA = rec.recvB = rec.recvC = 0;
// Set up a signal to start with. // Set up a signal to start with.
JournaledSignal<void(U8)> testEvent; JournaledSignal<void(U8)> testEvent;
testEvent.notify(&rec, &receiver::receiverRoot); testEvent.notify(&rec, &multiReceiver::receiverRoot);
// Initialize journal recording and fire off some events... // Initialize journal recording and fire off some events...
Journal::Record("test.jrn"); Journal::Record("test.jrn");

View file

@ -25,22 +25,40 @@
#include "core/util/tVector.h" #include "core/util/tVector.h"
// Define some test data used below. // Define some test data used below.
static const S32 ints[] = {0, 10, 2, 3, 14, 4, 12, 6, 16, 7, 8, 1, 11, 5, 13, 9, 15}; FIXTURE(Vector)
static const U32 length = sizeof(ints) / sizeof(S32);
static S32 QSORT_CALLBACK sortInts(const S32* a, const S32* b)
{ {
S32 av = *a; public:
S32 bv = *b; struct Dtor
{
bool* ptr;
Dtor() {} // Needed for vector increment.
Dtor(bool* ptr): ptr(ptr) {}
~Dtor()
{
*ptr = true;
}
};
if (av < bv) static const S32 ints[];
return -1; static const U32 length;
else if (av > bv) static S32 QSORT_CALLBACK sortInts(const S32* a, const S32* b)
return 1; {
else S32 av = *a;
return 0; S32 bv = *b;
}
TEST(Vector, Allocation) if (av < bv)
return -1;
else if (av > bv)
return 1;
else
return 0;
}
};
const S32 VectorFixture::ints[] = {0, 10, 2, 3, 14, 4, 12, 6, 16, 7, 8, 1, 11, 5, 13, 9, 15};
const U32 VectorFixture::length = sizeof(VectorFixture::ints) / sizeof(S32);
TEST_FIX(Vector, Allocation)
{ {
Vector<S32> *vector = new Vector<S32>; Vector<S32> *vector = new Vector<S32>;
for (S32 i = 0; i < 1000; i++) for (S32 i = 0; i < 1000; i++)
@ -57,19 +75,8 @@ TEST(Vector, Allocation)
delete vector; delete vector;
} }
TEST(Vector, Deallocation) TEST_FIX(Vector, Deallocation)
{ {
struct Dtor
{
bool* ptr;
Dtor() {} // Needed for vector increment.
Dtor(bool* ptr): ptr(ptr) {}
~Dtor()
{
*ptr = true;
}
};
bool dtorVals[10]; bool dtorVals[10];
Vector<Dtor> v; Vector<Dtor> v;
@ -101,7 +108,7 @@ TEST(Vector, Deallocation)
<< "Element " << i << "'s destructor was not called"; << "Element " << i << "'s destructor was not called";
} }
TEST(Vector, Sorting) TEST_FIX(Vector, Sorting)
{ {
Vector<S32> v; Vector<S32> v;

View file

@ -26,8 +26,9 @@
#include "console/console.h" #include "console/console.h"
#include "core/util/tVector.h" #include "core/util/tVector.h"
TEST(ThreadPool, BasicAPI) FIXTURE(ThreadPool)
{ {
public:
// Represents a single unit of work. In this test we just set an element in // Represents a single unit of work. In this test we just set an element in
// a result vector. // a result vector.
struct TestItem : public ThreadPool::WorkItem struct TestItem : public ThreadPool::WorkItem
@ -43,7 +44,10 @@ TEST(ThreadPool, BasicAPI)
mResults[mIndex] = mIndex; mResults[mIndex] = mIndex;
} }
}; };
};
TEST_FIX(ThreadPool, BasicAPI)
{
// Construct the vector of results from the work items. // Construct the vector of results from the work items.
const U32 numItems = 100; const U32 numItems = 100;
Vector<U32> results(__FILE__, __LINE__); Vector<U32> results(__FILE__, __LINE__);

View file

@ -28,57 +28,10 @@
#include "core/util/tVector.h" #include "core/util/tVector.h"
#include "console/console.h" #include "console/console.h"
// Test deque without concurrency. FIXTURE(ThreadSafeDeque)
TEST(ThreadSafeDeque, PopFront)
{ {
ThreadSafeDeque<char> deque; public:
String str = "teststring"; // Used by the concurrent test.
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(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(ThreadSafeDeque, Concurrent1)
{
const U32 NumValues = 100;
struct Value : public ThreadSafeRefCount<Value> struct Value : public ThreadSafeRefCount<Value>
{ {
U32 mIndex; U32 mIndex;
@ -121,9 +74,6 @@ TEST(ThreadSafeDeque, Concurrent1)
} }
}; };
Deque mDeque;
Vector<U32> mValues;
struct ProducerThread : public Thread struct ProducerThread : public Thread
{ {
Vector<U32>& mValues; Vector<U32>& mValues;
@ -163,6 +113,61 @@ TEST(ThreadSafeDeque, Concurrent1)
} }
} }
}; };
};
// 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_FIX(ThreadSafeDeque, Concurrent1)
{
const U32 NumValues = 100;
Deque mDeque;
Vector<U32> mValues;
mValues.setSize(NumValues); mValues.setSize(NumValues);

View file

@ -27,42 +27,23 @@
#include "core/util/tVector.h" #include "core/util/tVector.h"
#include "console/console.h" #include "console/console.h"
TEST(ThreadSafeRefCount, Serial) FIXTURE(ThreadSafeRefCount)
{ {
struct TestObject : public ThreadSafeRefCount<TestObject> public:
struct TestObjectDtor : public ThreadSafeRefCount<TestObjectDtor>
{ {
bool &flag; bool &flag;
TestObject(bool &f) : flag(f) TestObjectDtor(bool &f) : flag(f)
{ {
flag = false; flag = false;
} }
~TestObject() ~TestObjectDtor()
{ {
flag = true; flag = true;
} }
}; };
typedef ThreadSafeRef<TestObject> TestObjectRef; typedef ThreadSafeRef<TestObjectDtor> TestObjectDtorRef;
bool deleted = false;
TestObjectRef ref1 = new TestObject(deleted);
ASSERT_FALSE(deleted);
EXPECT_FALSE(ref1->isShared());
EXPECT_TRUE(ref1 != NULL);
TestObjectRef 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(ThreadSafeRefCount, Concurrent)
{
enum enum
{ {
NUM_ADD_REFS_PER_THREAD = 10, NUM_ADD_REFS_PER_THREAD = 10,
@ -72,7 +53,6 @@ TEST(ThreadSafeRefCount, Concurrent)
class TestObject : public ThreadSafeRefCount<TestObject> {}; class TestObject : public ThreadSafeRefCount<TestObject> {};
typedef ThreadSafeRef<TestObject> TestObjectRef; typedef ThreadSafeRef<TestObject> TestObjectRef;
TestObjectRef mRef;
class TestThread : public Thread class TestThread : public Thread
{ {
@ -104,7 +84,31 @@ TEST(ThreadSafeRefCount, Concurrent)
} }
}; };
mRef = new TestObject; };
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 EXPECT_EQ(2, mRef->getRefCount()); // increments of 2
Vector<TestThread*> threads; Vector<TestThread*> threads;
@ -141,11 +145,8 @@ TEST(ThreadSafeRefCount, Concurrent)
mRef = NULL; mRef = NULL;
} }
TEST(ThreadSafeRefCount, Tagging) TEST_FIX(ThreadSafeRefCount, Tagging)
{ {
struct TestObject : public ThreadSafeRefCount<TestObject> {};
typedef ThreadSafeRef<TestObject> TestObjectRef;
TestObjectRef ref; TestObjectRef ref;
EXPECT_FALSE(ref.isTagged()); EXPECT_FALSE(ref.isTagged());
EXPECT_FALSE(bool(ref)); EXPECT_FALSE(bool(ref));