Unit tests: Convert the refcnttest unit test to use the new EXPECT macros.

This commit is contained in:
Gabe Black 2011-01-18 01:27:04 -08:00
parent c04571d601
commit a39096a8c3

View file

@ -32,12 +32,17 @@
#include <iostream> #include <iostream>
#include <list> #include <list>
#include "base/cprintf.hh"
#include "base/refcnt.hh" #include "base/refcnt.hh"
#include "unittest/unittest.hh"
using namespace std; using namespace std;
using UnitTest::setCase;
namespace { namespace {
bool printNewDel = false;
class TestRC; class TestRC;
typedef list<TestRC *> LiveList; typedef list<TestRC *> LiveList;
LiveList liveList; LiveList liveList;
@ -67,14 +72,16 @@ class TestRC : public RefCounted
public: public:
TestRC(const char *newTag) : _tag(newTag) TestRC(const char *newTag) : _tag(newTag)
{ {
cout << " Creating object \"" << _tag << "\"\n"; if (printNewDel)
cprintf(" Creating object \"%s\"\n", _tag);
liveList.push_front(this); liveList.push_front(this);
liveIt = liveList.begin(); liveIt = liveList.begin();
} }
~TestRC() ~TestRC()
{ {
cout << " Destroying object \"" << _tag << "\"\n"; if (printNewDel)
cprintf(" Destroying object \"%s\"\n", _tag);
liveList.erase(liveIt); liveList.erase(liveIt);
} }
@ -89,7 +96,7 @@ class TestRC : public RefCounted
typedef RefCountingPtr<TestRC> Ptr; typedef RefCountingPtr<TestRC> Ptr;
} } // anonymous namespace
int int
main() main()
@ -98,90 +105,90 @@ main()
assert(liveChange() == 0); assert(liveChange() == 0);
// Create an empty Ptr and verify it's data pointer is NULL. // Create an empty Ptr and verify it's data pointer is NULL.
cout << "NULL check.\n"; setCase("NULL check");
Ptr nullCheck; Ptr nullCheck;
assert(nullCheck.get() == NULL); EXPECT_EQ(nullCheck.get(), NULL);
assert(liveChange() == 0); EXPECT_EQ(liveChange(), 0);
// Construct a Ptr from a TestRC pointer. // Construct a Ptr from a TestRC pointer.
cout << "Construction from pointer.\n"; setCase("construction from pointer");
Ptr constFromPointer = new TestRC("construction from pointer"); Ptr constFromPointer = new TestRC("construction from pointer");
assert(liveChange() == 1); EXPECT_EQ(liveChange(), 1);
// Construct a Ptr from an existing Ptr. // Construct a Ptr from an existing Ptr.
cout << "Construction from a Ptr.\n"; setCase("construction from a Ptr");
Ptr constFromPtr = constFromPointer; Ptr constFromPtr = constFromPointer;
assert(liveChange() == 0); EXPECT_EQ(liveChange(), 0);
// Test a Ptr being destroyed. // Test a Ptr being destroyed.
cout << "Destroying a Ptr.\n"; setCase("destroying a Ptr");
Ptr *ptrPtr = new Ptr(new TestRC("destroying a ptr")); Ptr *ptrPtr = new Ptr(new TestRC("destroying a ptr"));
assert(liveChange() == 1); EXPECT_EQ(liveChange(), 1);
delete ptrPtr; delete ptrPtr;
assert(liveChange() == -1); EXPECT_EQ(liveChange(), -1);
// Test assignment from a pointer and from a Ptr. // Test assignment from a pointer and from a Ptr.
cout << "Assignment operators.\n"; setCase("assignment operators");
Ptr assignmentTarget; Ptr assignmentTarget;
TestRC *assignmentSourcePointer = new TestRC("assignment source 1"); TestRC *assignmentSourcePointer = new TestRC("assignment source 1");
assert(liveChange() == 1); EXPECT_EQ(liveChange(), 1);
assignmentTarget = assignmentSourcePointer; assignmentTarget = assignmentSourcePointer;
assert(liveChange() == 0); EXPECT_EQ(liveChange(), 0);
assignmentTarget = NULL; assignmentTarget = NULL;
assert(liveChange() == -1); EXPECT_EQ(liveChange(), -1);
Ptr assignmentSourcePtr(new TestRC("assignment source 2")); Ptr assignmentSourcePtr(new TestRC("assignment source 2"));
assert(liveChange() == 1); EXPECT_EQ(liveChange(), 1);
assignmentTarget = assignmentSourcePtr; assignmentTarget = assignmentSourcePtr;
assert(liveChange() == 0); EXPECT_EQ(liveChange(), 0);
assignmentSourcePtr = NULL; assignmentSourcePtr = NULL;
assert(liveChange() == 0); EXPECT_EQ(liveChange(), 0);
assignmentTarget = NULL; assignmentTarget = NULL;
assert(liveChange() == -1); EXPECT_EQ(liveChange(), -1);
// Test access to members of the pointed to class and dereferencing. // Test access to members of the pointed to class and dereferencing.
cout << "Access to members.\n"; setCase("access to members");
TestRC *accessTest = new TestRC("access test"); TestRC *accessTest = new TestRC("access test");
Ptr accessTestPtr = accessTest; Ptr accessTestPtr = accessTest;
accessTest->testVal = 1; accessTest->testVal = 1;
assert(accessTestPtr->testVal == 1); EXPECT_EQ(accessTestPtr->testVal, 1);
assert((*accessTestPtr).testVal == 1); EXPECT_EQ((*accessTestPtr).testVal, 1);
accessTest->testVal = 2; accessTest->testVal = 2;
assert(accessTestPtr->testVal == 2); EXPECT_EQ(accessTestPtr->testVal, 2);
assert((*accessTestPtr).testVal == 2); EXPECT_EQ((*accessTestPtr).testVal, 2);
accessTestPtr->testVal = 3; accessTestPtr->testVal = 3;
assert(accessTest->testVal == 3); EXPECT_EQ(accessTest->testVal, 3);
(*accessTestPtr).testVal = 4; (*accessTestPtr).testVal = 4;
assert(accessTest->testVal == 4); EXPECT_EQ(accessTest->testVal, 4);
accessTestPtr = NULL; accessTestPtr = NULL;
accessTest = NULL; accessTest = NULL;
assert(liveChange() == 0); EXPECT_EQ(liveChange(), 0);
// Test bool and ! operator overloads. // Test bool and ! operator overloads.
cout << "Conversion to bool and ! overload.\n"; setCase("conversion to bool and ! overload");
Ptr boolTest = new TestRC("bool test"); Ptr boolTest = new TestRC("bool test");
assert(boolTest == true); EXPECT_EQ(boolTest, true);
assert(!boolTest == false); EXPECT_EQ(!boolTest, false);
boolTest = NULL; boolTest = NULL;
assert(boolTest == false); EXPECT_EQ(boolTest, false);
assert(!boolTest == true); EXPECT_EQ(!boolTest, true);
assert(liveChange() == 0); EXPECT_EQ(liveChange(), 0);
// Test the equality operators. // Test the equality operators.
cout << "Equality operators.\n"; setCase("equality operators");
TestRC *equalTestA = new TestRC("equal test a"); TestRC *equalTestA = new TestRC("equal test a");
Ptr equalTestAPtr = equalTestA; Ptr equalTestAPtr = equalTestA;
Ptr equalTestAPtr2 = equalTestA; Ptr equalTestAPtr2 = equalTestA;
TestRC *equalTestB = new TestRC("equal test b"); TestRC *equalTestB = new TestRC("equal test b");
Ptr equalTestBPtr = equalTestB; Ptr equalTestBPtr = equalTestB;
assert(equalTestA == equalTestAPtr); EXPECT_TRUE(equalTestA == equalTestAPtr);
assert(equalTestAPtr == equalTestA); EXPECT_TRUE(equalTestAPtr == equalTestA);
assert(equalTestAPtr == equalTestAPtr2); EXPECT_TRUE(equalTestAPtr == equalTestAPtr2);
assert(equalTestA != equalTestBPtr); EXPECT_TRUE(equalTestA != equalTestBPtr);
assert(equalTestAPtr != equalTestB); EXPECT_TRUE(equalTestAPtr != equalTestB);
assert(equalTestAPtr != equalTestBPtr); EXPECT_TRUE(equalTestAPtr != equalTestBPtr);
cout << flush; return UnitTest::printResults();
} }