gem5 v24.0.0.0
Loading...
Searching...
No Matches
refcnt.test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 The Regents of The University of California
3 * All rights resvered.
4 *
5 * Copyright (c) 2010 The Regents of The University of Michigan
6 * All rights reserved.
7 *
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.
18 *
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.
30 */
31
32#include <gtest/gtest.h>
33
34#include <list>
35
36#include "base/refcnt.hh"
37
38using namespace gem5;
39
40namespace {
41
42class TestRC;
43typedef std::list<TestRC *> LiveList;
44LiveList liveList;
45
46int
47liveListSize()
48{
49 return liveList.size();
50}
51
52class TestRC : public RefCounted
53{
54 protected:
55 LiveList::iterator liveIt;
56
57 public:
58 TestRC()
59 {
60 liveList.push_front(this);
61 liveIt = liveList.begin();
62 }
63
64 ~TestRC()
65 {
66 liveList.erase(liveIt);
67 }
68
69 int testVal;
70};
71typedef RefCountingPtr<TestRC> Ptr;
72
73} // anonymous namespace
74
75TEST(RefcntTest, NullPointerCheck)
76{
77 // Create an empty Ptr and verify it's data pointer is NULL.
78 Ptr nullCheck;
79 EXPECT_EQ(NULL, nullCheck.get());
80 EXPECT_EQ(0, liveListSize());
81}
82
83TEST(RefcntTest, ConstructionFromPointer)
84{
85 // Construct a Ptr from a TestRC pointer.
86 Ptr constFromPointer = new TestRC();
87 EXPECT_EQ(1, liveListSize());
88}
89
90TEST(RefcntTest, ConstructionFromExistingPointer)
91{
92 // Construct a Ptr from an existing Ptr.
93 Ptr constFromPointer1 = new TestRC();
94 Ptr constFromPointer2 = constFromPointer1;
95
96 EXPECT_EQ(1, liveListSize());
97}
98
99TEST(RefcntTest, DestroyPointer)
100{
101 // Test a Ptr being destroyed.
102 Ptr *ptrPtr = new Ptr(new TestRC());
103 EXPECT_EQ(1, liveListSize());
104 delete ptrPtr;
105 EXPECT_EQ(0, liveListSize());
106}
107
108TEST(RefcntTest, AssignmentFromAPointerFromAPointer)
109{
110 // Test assignment from a pointer and from a Ptr.
111 Ptr assignmentTarget;
112 TestRC *assignmentSourcePointer = new TestRC();
113 EXPECT_EQ(liveListSize(), 1);
114 assignmentTarget = assignmentSourcePointer;
115 EXPECT_EQ(liveListSize(), 1);
116 assignmentTarget = NULL;
117 EXPECT_EQ(liveListSize(), 0);
118 Ptr assignmentSourcePtr(new TestRC());
119 EXPECT_EQ(liveListSize(), 1);
120 assignmentTarget = assignmentSourcePtr;
121 EXPECT_EQ(liveListSize(), 1);
122 assignmentSourcePtr = NULL;
123 EXPECT_EQ(liveListSize(), 1);
124 assignmentTarget = NULL;
125 EXPECT_EQ(liveListSize(), 0);
126}
127
128TEST(RefcntTest, AccessToClassPointers)
129{
130 // Test access to members of the pointed to class and dereferencing.
131 TestRC *accessTest = new TestRC();
132 Ptr accessTestPtr = accessTest;
133 accessTest->testVal = 1;
134 EXPECT_EQ(1, accessTestPtr->testVal);
135 EXPECT_EQ(1, (*accessTestPtr).testVal);
136 accessTest->testVal = 2;
137 EXPECT_EQ(2, accessTestPtr->testVal);
138 EXPECT_EQ(2, (*accessTestPtr).testVal);
139 accessTestPtr->testVal = 3;
140 EXPECT_EQ(3, accessTest->testVal);
141 (*accessTestPtr).testVal = 4;
142 EXPECT_EQ(4, accessTest->testVal);
143 accessTestPtr = NULL;
144 accessTest = NULL;
145 EXPECT_EQ(0, liveListSize());
146}
147
148TEST(RefcntTest, BoolAndLogicalNotOperatorOverloads)
149{
150 // Test bool and ! operator overloads.
151 Ptr boolTest = new TestRC();
152 EXPECT_EQ(boolTest, true);
153 EXPECT_EQ(!boolTest, false);
154 boolTest = NULL;
155 EXPECT_FALSE(boolTest);
156 EXPECT_TRUE(!boolTest);
157 EXPECT_EQ(0, liveListSize());
158}
159
160TEST(RefcntTest, EqualityOperators)
161{
162 // Test the equality operators.
163 TestRC *equalTestA = new TestRC();
164 Ptr equalTestAPtr = equalTestA;
165 Ptr equalTestAPtr2 = equalTestA;
166 TestRC *equalTestB = new TestRC();
167 Ptr equalTestBPtr = equalTestB;
168 EXPECT_TRUE(equalTestA == equalTestAPtr);
169 EXPECT_TRUE(equalTestAPtr == equalTestA);
170 EXPECT_TRUE(equalTestAPtr == equalTestAPtr2);
171 EXPECT_TRUE(equalTestA != equalTestBPtr);
172 EXPECT_TRUE(equalTestAPtr != equalTestB);
173 EXPECT_TRUE(equalTestAPtr != equalTestBPtr);
174}
Derive from RefCounted if you want to enable reference counting of this class.
Definition refcnt.hh:61
If you want a reference counting pointer to a mutable object, create it like this:
Definition refcnt.hh:127
STL list class.
Definition stl.hh:51
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
Classes for managing reference counted objects.
TEST(RefcntTest, NullPointerCheck)

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0