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

Generated on Tue Mar 23 2021 19:41:24 for gem5 by doxygen 1.8.17