2 * Copyright (c) 2010 The Regents of The University of Michigan
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;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "base/cprintf.hh"
36 #include "base/refcnt.hh"
37 #include "unittest/unittest.hh"
40 using UnitTest::setCase
;
44 bool printNewDel
= false;
47 typedef list
<TestRC
*> LiveList
;
53 return liveList
.size();
59 static int oldLive
= 0;
61 int diff
= newLive
- oldLive
;
66 class TestRC
: public RefCounted
70 LiveList::iterator liveIt
;
73 TestRC(const char *newTag
) : _tag(newTag
)
76 cprintf(" Creating object \"%s\"\n", _tag
);
77 liveList
.push_front(this);
78 liveIt
= liveList
.begin();
84 cprintf(" Destroying object \"%s\"\n", _tag
);
85 liveList
.erase(liveIt
);
97 typedef RefCountingPtr
<TestRC
> Ptr
;
99 } // anonymous namespace
105 assert(liveChange() == 0);
107 // Create an empty Ptr and verify it's data pointer is NULL.
108 setCase("NULL check");
110 EXPECT_EQ(nullCheck
.get(), NULL
);
112 EXPECT_EQ(liveChange(), 0);
114 // Construct a Ptr from a TestRC pointer.
115 setCase("construction from pointer");
116 Ptr constFromPointer
= new TestRC("construction from pointer");
118 EXPECT_EQ(liveChange(), 1);
120 // Construct a Ptr from an existing Ptr.
121 setCase("construction from a Ptr");
122 Ptr constFromPtr
= constFromPointer
;
124 EXPECT_EQ(liveChange(), 0);
126 // Test a Ptr being destroyed.
127 setCase("destroying a Ptr");
128 Ptr
*ptrPtr
= new Ptr(new TestRC("destroying a ptr"));
129 EXPECT_EQ(liveChange(), 1);
131 EXPECT_EQ(liveChange(), -1);
133 // Test assignment from a pointer and from a Ptr.
134 setCase("assignment operators");
135 Ptr assignmentTarget
;
136 TestRC
*assignmentSourcePointer
= new TestRC("assignment source 1");
137 EXPECT_EQ(liveChange(), 1);
138 assignmentTarget
= assignmentSourcePointer
;
139 EXPECT_EQ(liveChange(), 0);
140 assignmentTarget
= NULL
;
141 EXPECT_EQ(liveChange(), -1);
142 Ptr
assignmentSourcePtr(new TestRC("assignment source 2"));
143 EXPECT_EQ(liveChange(), 1);
144 assignmentTarget
= assignmentSourcePtr
;
145 EXPECT_EQ(liveChange(), 0);
146 assignmentSourcePtr
= NULL
;
147 EXPECT_EQ(liveChange(), 0);
148 assignmentTarget
= NULL
;
149 EXPECT_EQ(liveChange(), -1);
151 // Test access to members of the pointed to class and dereferencing.
152 setCase("access to members");
153 TestRC
*accessTest
= new TestRC("access test");
154 Ptr accessTestPtr
= accessTest
;
155 accessTest
->testVal
= 1;
156 EXPECT_EQ(accessTestPtr
->testVal
, 1);
157 EXPECT_EQ((*accessTestPtr
).testVal
, 1);
158 accessTest
->testVal
= 2;
159 EXPECT_EQ(accessTestPtr
->testVal
, 2);
160 EXPECT_EQ((*accessTestPtr
).testVal
, 2);
161 accessTestPtr
->testVal
= 3;
162 EXPECT_EQ(accessTest
->testVal
, 3);
163 (*accessTestPtr
).testVal
= 4;
164 EXPECT_EQ(accessTest
->testVal
, 4);
165 accessTestPtr
= NULL
;
167 EXPECT_EQ(liveChange(), 0);
169 // Test bool and ! operator overloads.
170 setCase("conversion to bool and ! overload");
171 Ptr boolTest
= new TestRC("bool test");
172 EXPECT_EQ(boolTest
, true);
173 EXPECT_EQ(!boolTest
, false);
175 EXPECT_EQ(boolTest
, false);
176 EXPECT_EQ(!boolTest
, true);
177 EXPECT_EQ(liveChange(), 0);
179 // Test the equality operators.
180 setCase("equality operators");
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
;
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
);
193 return UnitTest::printResults();