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.
35 #include <gtest/gtest.h>
39 #include "base/refcnt.hh"
46 typedef list
<TestRC
*> LiveList
;
51 return liveList
.size();
54 class TestRC
: public RefCounted
57 LiveList::iterator liveIt
;
62 liveList
.push_front(this);
63 liveIt
= liveList
.begin();
68 liveList
.erase(liveIt
);
73 typedef RefCountingPtr
<TestRC
> Ptr
;
75 } // anonymous namespace
77 TEST(RefcntTest
, NullPointerCheck
)
79 // Create an empty Ptr and verify it's data pointer is NULL.
81 EXPECT_EQ(NULL
, nullCheck
.get());
82 EXPECT_EQ(0, liveListSize());
85 TEST(RefcntTest
, ConstructionFromPointer
)
87 // Construct a Ptr from a TestRC pointer.
88 Ptr constFromPointer
= new TestRC();
89 EXPECT_EQ(1, liveListSize());
92 TEST(RefcntTest
, ConstructionFromExistingPointer
)
94 // Construct a Ptr from an existing Ptr.
95 Ptr constFromPointer1
= new TestRC();
96 Ptr constFromPointer2
= constFromPointer1
;
98 EXPECT_EQ(1, liveListSize());
101 TEST(RefcntTest
, DestroyPointer
)
103 // Test a Ptr being destroyed.
104 Ptr
*ptrPtr
= new Ptr(new TestRC());
105 EXPECT_EQ(1, liveListSize());
107 EXPECT_EQ(0, liveListSize());
110 TEST(RefcntTest
, AssignmentFromAPointerFromAPointer
)
112 // Test assignment from a pointer and from a Ptr.
113 Ptr assignmentTarget
;
114 TestRC
*assignmentSourcePointer
= new TestRC();
115 EXPECT_EQ(liveListSize(), 1);
116 assignmentTarget
= assignmentSourcePointer
;
117 EXPECT_EQ(liveListSize(), 1);
118 assignmentTarget
= NULL
;
119 EXPECT_EQ(liveListSize(), 0);
120 Ptr
assignmentSourcePtr(new TestRC());
121 EXPECT_EQ(liveListSize(), 1);
122 assignmentTarget
= assignmentSourcePtr
;
123 EXPECT_EQ(liveListSize(), 1);
124 assignmentSourcePtr
= NULL
;
125 EXPECT_EQ(liveListSize(), 1);
126 assignmentTarget
= NULL
;
127 EXPECT_EQ(liveListSize(), 0);
130 TEST(RefcntTest
, AccessToClassPointers
)
132 // Test access to members of the pointed to class and dereferencing.
133 TestRC
*accessTest
= new TestRC();
134 Ptr accessTestPtr
= accessTest
;
135 accessTest
->testVal
= 1;
136 EXPECT_EQ(1, accessTestPtr
->testVal
);
137 EXPECT_EQ(1, (*accessTestPtr
).testVal
);
138 accessTest
->testVal
= 2;
139 EXPECT_EQ(2, accessTestPtr
->testVal
);
140 EXPECT_EQ(2, (*accessTestPtr
).testVal
);
141 accessTestPtr
->testVal
= 3;
142 EXPECT_EQ(3, accessTest
->testVal
);
143 (*accessTestPtr
).testVal
= 4;
144 EXPECT_EQ(4, accessTest
->testVal
);
145 accessTestPtr
= NULL
;
147 EXPECT_EQ(0, liveListSize());
150 TEST(RefcntTest
, BoolAndLogicalNotOperatorOverloads
)
152 // Test bool and ! operator overloads.
153 Ptr boolTest
= new TestRC();
154 EXPECT_EQ(boolTest
, true);
155 EXPECT_EQ(!boolTest
, false);
157 EXPECT_FALSE(boolTest
);
158 EXPECT_TRUE(!boolTest
);
159 EXPECT_EQ(0, liveListSize());
162 TEST(RefcntTest
, EqualityOperators
)
164 // Test the equality operators.
165 TestRC
*equalTestA
= new TestRC();
166 Ptr equalTestAPtr
= equalTestA
;
167 Ptr equalTestAPtr2
= equalTestA
;
168 TestRC
*equalTestB
= new TestRC();
169 Ptr equalTestBPtr
= equalTestB
;
170 EXPECT_TRUE(equalTestA
== equalTestAPtr
);
171 EXPECT_TRUE(equalTestAPtr
== equalTestA
);
172 EXPECT_TRUE(equalTestAPtr
== equalTestAPtr2
);
173 EXPECT_TRUE(equalTestA
!= equalTestBPtr
);
174 EXPECT_TRUE(equalTestAPtr
!= equalTestB
);
175 EXPECT_TRUE(equalTestAPtr
!= equalTestBPtr
);