2 * Copyright (c) 2019 The Regents of The University of California
5 * Copyright (c) 2010 The Regents of The University of Michigan
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer;
12 * redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution;
15 * neither the name of the copyright holders nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <gtest/gtest.h>
36 #include "base/refcnt.hh"
43 typedef list
<TestRC
*> LiveList
;
48 return liveList
.size();
51 class TestRC
: public RefCounted
54 LiveList::iterator liveIt
;
59 liveList
.push_front(this);
60 liveIt
= liveList
.begin();
65 liveList
.erase(liveIt
);
70 typedef RefCountingPtr
<TestRC
> Ptr
;
72 } // anonymous namespace
74 TEST(RefcntTest
, NullPointerCheck
)
76 // Create an empty Ptr and verify it's data pointer is NULL.
78 EXPECT_EQ(NULL
, nullCheck
.get());
79 EXPECT_EQ(0, liveListSize());
82 TEST(RefcntTest
, ConstructionFromPointer
)
84 // Construct a Ptr from a TestRC pointer.
85 Ptr constFromPointer
= new TestRC();
86 EXPECT_EQ(1, liveListSize());
89 TEST(RefcntTest
, ConstructionFromExistingPointer
)
91 // Construct a Ptr from an existing Ptr.
92 Ptr constFromPointer1
= new TestRC();
93 Ptr constFromPointer2
= constFromPointer1
;
95 EXPECT_EQ(1, liveListSize());
98 TEST(RefcntTest
, DestroyPointer
)
100 // Test a Ptr being destroyed.
101 Ptr
*ptrPtr
= new Ptr(new TestRC());
102 EXPECT_EQ(1, liveListSize());
104 EXPECT_EQ(0, liveListSize());
107 TEST(RefcntTest
, AssignmentFromAPointerFromAPointer
)
109 // Test assignment from a pointer and from a Ptr.
110 Ptr assignmentTarget
;
111 TestRC
*assignmentSourcePointer
= new TestRC();
112 EXPECT_EQ(liveListSize(), 1);
113 assignmentTarget
= assignmentSourcePointer
;
114 EXPECT_EQ(liveListSize(), 1);
115 assignmentTarget
= NULL
;
116 EXPECT_EQ(liveListSize(), 0);
117 Ptr
assignmentSourcePtr(new TestRC());
118 EXPECT_EQ(liveListSize(), 1);
119 assignmentTarget
= assignmentSourcePtr
;
120 EXPECT_EQ(liveListSize(), 1);
121 assignmentSourcePtr
= NULL
;
122 EXPECT_EQ(liveListSize(), 1);
123 assignmentTarget
= NULL
;
124 EXPECT_EQ(liveListSize(), 0);
127 TEST(RefcntTest
, AccessToClassPointers
)
129 // Test access to members of the pointed to class and dereferencing.
130 TestRC
*accessTest
= new TestRC();
131 Ptr accessTestPtr
= accessTest
;
132 accessTest
->testVal
= 1;
133 EXPECT_EQ(1, accessTestPtr
->testVal
);
134 EXPECT_EQ(1, (*accessTestPtr
).testVal
);
135 accessTest
->testVal
= 2;
136 EXPECT_EQ(2, accessTestPtr
->testVal
);
137 EXPECT_EQ(2, (*accessTestPtr
).testVal
);
138 accessTestPtr
->testVal
= 3;
139 EXPECT_EQ(3, accessTest
->testVal
);
140 (*accessTestPtr
).testVal
= 4;
141 EXPECT_EQ(4, accessTest
->testVal
);
142 accessTestPtr
= NULL
;
144 EXPECT_EQ(0, liveListSize());
147 TEST(RefcntTest
, BoolAndLogicalNotOperatorOverloads
)
149 // Test bool and ! operator overloads.
150 Ptr boolTest
= new TestRC();
151 EXPECT_EQ(boolTest
, true);
152 EXPECT_EQ(!boolTest
, false);
154 EXPECT_FALSE(boolTest
);
155 EXPECT_TRUE(!boolTest
);
156 EXPECT_EQ(0, liveListSize());
159 TEST(RefcntTest
, EqualityOperators
)
161 // Test the equality operators.
162 TestRC
*equalTestA
= new TestRC();
163 Ptr equalTestAPtr
= equalTestA
;
164 Ptr equalTestAPtr2
= equalTestA
;
165 TestRC
*equalTestB
= new TestRC();
166 Ptr equalTestBPtr
= equalTestB
;
167 EXPECT_TRUE(equalTestA
== equalTestAPtr
);
168 EXPECT_TRUE(equalTestAPtr
== equalTestA
);
169 EXPECT_TRUE(equalTestAPtr
== equalTestAPtr2
);
170 EXPECT_TRUE(equalTestA
!= equalTestBPtr
);
171 EXPECT_TRUE(equalTestAPtr
!= equalTestB
);
172 EXPECT_TRUE(equalTestAPtr
!= equalTestBPtr
);