refcnttest.cc (7824:b36af60dcb91) | refcnttest.cc (7842:4c0f7929ee33) |
---|---|
1/* 2 * Copyright (c) 2010 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; --- 18 unchanged lines hidden (view full) --- 27 * 28 * Authors: Gabe Black 29 */ 30 31#include <cassert> 32#include <iostream> 33#include <list> 34 | 1/* 2 * Copyright (c) 2010 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; --- 18 unchanged lines hidden (view full) --- 27 * 28 * Authors: Gabe Black 29 */ 30 31#include <cassert> 32#include <iostream> 33#include <list> 34 |
35#include "base/cprintf.hh" |
|
35#include "base/refcnt.hh" | 36#include "base/refcnt.hh" |
37#include "unittest/unittest.hh" |
|
36 37using namespace std; | 38 39using namespace std; |
40using UnitTest::setCase; |
|
38 39namespace { 40 | 41 42namespace { 43 |
44bool printNewDel = false; 45 |
|
41class TestRC; 42typedef list<TestRC *> LiveList; 43LiveList liveList; 44 45int 46live() 47{ 48 return liveList.size(); --- 13 unchanged lines hidden (view full) --- 62{ 63 protected: 64 const char *_tag; 65 LiveList::iterator liveIt; 66 67 public: 68 TestRC(const char *newTag) : _tag(newTag) 69 { | 46class TestRC; 47typedef list<TestRC *> LiveList; 48LiveList liveList; 49 50int 51live() 52{ 53 return liveList.size(); --- 13 unchanged lines hidden (view full) --- 67{ 68 protected: 69 const char *_tag; 70 LiveList::iterator liveIt; 71 72 public: 73 TestRC(const char *newTag) : _tag(newTag) 74 { |
70 cout << " Creating object \"" << _tag << "\"\n"; | 75 if (printNewDel) 76 cprintf(" Creating object \"%s\"\n", _tag); |
71 liveList.push_front(this); 72 liveIt = liveList.begin(); 73 } 74 75 ~TestRC() 76 { | 77 liveList.push_front(this); 78 liveIt = liveList.begin(); 79 } 80 81 ~TestRC() 82 { |
77 cout << " Destroying object \"" << _tag << "\"\n"; | 83 if (printNewDel) 84 cprintf(" Destroying object \"%s\"\n", _tag); |
78 liveList.erase(liveIt); 79 } 80 81 const char * 82 tag() 83 { 84 return _tag; 85 } 86 87 int testVal; 88}; 89 90typedef RefCountingPtr<TestRC> Ptr; 91 | 85 liveList.erase(liveIt); 86 } 87 88 const char * 89 tag() 90 { 91 return _tag; 92 } 93 94 int testVal; 95}; 96 97typedef RefCountingPtr<TestRC> Ptr; 98 |
92} | 99} // anonymous namespace |
93 94int 95main() 96{ 97 assert(live() == 0); 98 assert(liveChange() == 0); 99 100 // Create an empty Ptr and verify it's data pointer is NULL. | 100 101int 102main() 103{ 104 assert(live() == 0); 105 assert(liveChange() == 0); 106 107 // Create an empty Ptr and verify it's data pointer is NULL. |
101 cout << "NULL check.\n"; | 108 setCase("NULL check"); |
102 Ptr nullCheck; | 109 Ptr nullCheck; |
103 assert(nullCheck.get() == NULL); | 110 EXPECT_EQ(nullCheck.get(), NULL); |
104 | 111 |
105 assert(liveChange() == 0); | 112 EXPECT_EQ(liveChange(), 0); |
106 107 // Construct a Ptr from a TestRC pointer. | 113 114 // Construct a Ptr from a TestRC pointer. |
108 cout << "Construction from pointer.\n"; | 115 setCase("construction from pointer"); |
109 Ptr constFromPointer = new TestRC("construction from pointer"); 110 | 116 Ptr constFromPointer = new TestRC("construction from pointer"); 117 |
111 assert(liveChange() == 1); | 118 EXPECT_EQ(liveChange(), 1); |
112 113 // Construct a Ptr from an existing Ptr. | 119 120 // Construct a Ptr from an existing Ptr. |
114 cout << "Construction from a Ptr.\n"; | 121 setCase("construction from a Ptr"); |
115 Ptr constFromPtr = constFromPointer; 116 | 122 Ptr constFromPtr = constFromPointer; 123 |
117 assert(liveChange() == 0); | 124 EXPECT_EQ(liveChange(), 0); |
118 119 // Test a Ptr being destroyed. | 125 126 // Test a Ptr being destroyed. |
120 cout << "Destroying a Ptr.\n"; | 127 setCase("destroying a Ptr"); |
121 Ptr *ptrPtr = new Ptr(new TestRC("destroying a ptr")); | 128 Ptr *ptrPtr = new Ptr(new TestRC("destroying a ptr")); |
122 assert(liveChange() == 1); | 129 EXPECT_EQ(liveChange(), 1); |
123 delete ptrPtr; | 130 delete ptrPtr; |
124 assert(liveChange() == -1); | 131 EXPECT_EQ(liveChange(), -1); |
125 126 // Test assignment from a pointer and from a Ptr. | 132 133 // Test assignment from a pointer and from a Ptr. |
127 cout << "Assignment operators.\n"; | 134 setCase("assignment operators"); |
128 Ptr assignmentTarget; 129 TestRC *assignmentSourcePointer = new TestRC("assignment source 1"); | 135 Ptr assignmentTarget; 136 TestRC *assignmentSourcePointer = new TestRC("assignment source 1"); |
130 assert(liveChange() == 1); | 137 EXPECT_EQ(liveChange(), 1); |
131 assignmentTarget = assignmentSourcePointer; | 138 assignmentTarget = assignmentSourcePointer; |
132 assert(liveChange() == 0); | 139 EXPECT_EQ(liveChange(), 0); |
133 assignmentTarget = NULL; | 140 assignmentTarget = NULL; |
134 assert(liveChange() == -1); | 141 EXPECT_EQ(liveChange(), -1); |
135 Ptr assignmentSourcePtr(new TestRC("assignment source 2")); | 142 Ptr assignmentSourcePtr(new TestRC("assignment source 2")); |
136 assert(liveChange() == 1); | 143 EXPECT_EQ(liveChange(), 1); |
137 assignmentTarget = assignmentSourcePtr; | 144 assignmentTarget = assignmentSourcePtr; |
138 assert(liveChange() == 0); | 145 EXPECT_EQ(liveChange(), 0); |
139 assignmentSourcePtr = NULL; | 146 assignmentSourcePtr = NULL; |
140 assert(liveChange() == 0); | 147 EXPECT_EQ(liveChange(), 0); |
141 assignmentTarget = NULL; | 148 assignmentTarget = NULL; |
142 assert(liveChange() == -1); | 149 EXPECT_EQ(liveChange(), -1); |
143 144 // Test access to members of the pointed to class and dereferencing. | 150 151 // Test access to members of the pointed to class and dereferencing. |
145 cout << "Access to members.\n"; | 152 setCase("access to members"); |
146 TestRC *accessTest = new TestRC("access test"); 147 Ptr accessTestPtr = accessTest; 148 accessTest->testVal = 1; | 153 TestRC *accessTest = new TestRC("access test"); 154 Ptr accessTestPtr = accessTest; 155 accessTest->testVal = 1; |
149 assert(accessTestPtr->testVal == 1); 150 assert((*accessTestPtr).testVal == 1); | 156 EXPECT_EQ(accessTestPtr->testVal, 1); 157 EXPECT_EQ((*accessTestPtr).testVal, 1); |
151 accessTest->testVal = 2; | 158 accessTest->testVal = 2; |
152 assert(accessTestPtr->testVal == 2); 153 assert((*accessTestPtr).testVal == 2); | 159 EXPECT_EQ(accessTestPtr->testVal, 2); 160 EXPECT_EQ((*accessTestPtr).testVal, 2); |
154 accessTestPtr->testVal = 3; | 161 accessTestPtr->testVal = 3; |
155 assert(accessTest->testVal == 3); | 162 EXPECT_EQ(accessTest->testVal, 3); |
156 (*accessTestPtr).testVal = 4; | 163 (*accessTestPtr).testVal = 4; |
157 assert(accessTest->testVal == 4); | 164 EXPECT_EQ(accessTest->testVal, 4); |
158 accessTestPtr = NULL; 159 accessTest = NULL; | 165 accessTestPtr = NULL; 166 accessTest = NULL; |
160 assert(liveChange() == 0); | 167 EXPECT_EQ(liveChange(), 0); |
161 162 // Test bool and ! operator overloads. | 168 169 // Test bool and ! operator overloads. |
163 cout << "Conversion to bool and ! overload.\n"; | 170 setCase("conversion to bool and ! overload"); |
164 Ptr boolTest = new TestRC("bool test"); | 171 Ptr boolTest = new TestRC("bool test"); |
165 assert(boolTest == true); 166 assert(!boolTest == false); | 172 EXPECT_EQ(boolTest, true); 173 EXPECT_EQ(!boolTest, false); |
167 boolTest = NULL; | 174 boolTest = NULL; |
168 assert(boolTest == false); 169 assert(!boolTest == true); 170 assert(liveChange() == 0); | 175 EXPECT_EQ(boolTest, false); 176 EXPECT_EQ(!boolTest, true); 177 EXPECT_EQ(liveChange(), 0); |
171 172 // Test the equality operators. | 178 179 // Test the equality operators. |
173 cout << "Equality operators.\n"; | 180 setCase("equality operators"); |
174 TestRC *equalTestA = new TestRC("equal test a"); 175 Ptr equalTestAPtr = equalTestA; 176 Ptr equalTestAPtr2 = equalTestA; 177 TestRC *equalTestB = new TestRC("equal test b"); 178 Ptr equalTestBPtr = equalTestB; | 181 TestRC *equalTestA = new TestRC("equal test a"); 182 Ptr equalTestAPtr = equalTestA; 183 Ptr equalTestAPtr2 = equalTestA; 184 TestRC *equalTestB = new TestRC("equal test b"); 185 Ptr equalTestBPtr = equalTestB; |
179 assert(equalTestA == equalTestAPtr); 180 assert(equalTestAPtr == equalTestA); 181 assert(equalTestAPtr == equalTestAPtr2); 182 assert(equalTestA != equalTestBPtr); 183 assert(equalTestAPtr != equalTestB); 184 assert(equalTestAPtr != equalTestBPtr); | 186 EXPECT_TRUE(equalTestA == equalTestAPtr); 187 EXPECT_TRUE(equalTestAPtr == equalTestA); 188 EXPECT_TRUE(equalTestAPtr == equalTestAPtr2); 189 EXPECT_TRUE(equalTestA != equalTestBPtr); 190 EXPECT_TRUE(equalTestAPtr != equalTestB); 191 EXPECT_TRUE(equalTestAPtr != equalTestBPtr); |
185 | 192 |
186 cout << flush; | 193 return UnitTest::printResults(); |
187} | 194} |