gem5  v20.1.0.0
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 using namespace std;
39 
40 namespace {
41 
42 class TestRC;
43 typedef list<TestRC *> LiveList;
44 LiveList liveList;
45 
46 int
47 liveListSize(){
48  return liveList.size();
49 }
50 
51 class TestRC : public RefCounted
52 {
53  protected:
54  LiveList::iterator liveIt;
55 
56  public:
57  TestRC()
58  {
59  liveList.push_front(this);
60  liveIt = liveList.begin();
61  }
62 
63  ~TestRC()
64  {
65  liveList.erase(liveIt);
66  }
67 
68  int testVal;
69 };
70 typedef RefCountingPtr<TestRC> Ptr;
71 
72 } // anonymous namespace
73 
74 TEST(RefcntTest, NullPointerCheck)
75 {
76  // Create an empty Ptr and verify it's data pointer is NULL.
77  Ptr nullCheck;
78  EXPECT_EQ(NULL, nullCheck.get());
79  EXPECT_EQ(0, liveListSize());
80 }
81 
82 TEST(RefcntTest, ConstructionFromPointer)
83 {
84  // Construct a Ptr from a TestRC pointer.
85  Ptr constFromPointer = new TestRC();
86  EXPECT_EQ(1, liveListSize());
87 }
88 
89 TEST(RefcntTest, ConstructionFromExistingPointer)
90 {
91  // Construct a Ptr from an existing Ptr.
92  Ptr constFromPointer1 = new TestRC();
93  Ptr constFromPointer2 = constFromPointer1;
94 
95  EXPECT_EQ(1, liveListSize());
96 }
97 
98 TEST(RefcntTest, DestroyPointer)
99 {
100  // Test a Ptr being destroyed.
101  Ptr *ptrPtr = new Ptr(new TestRC());
102  EXPECT_EQ(1, liveListSize());
103  delete ptrPtr;
104  EXPECT_EQ(0, liveListSize());
105 }
106 
107 TEST(RefcntTest, AssignmentFromAPointerFromAPointer)
108 {
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);
125 }
126 
127 TEST(RefcntTest, AccessToClassPointers)
128 {
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;
143  accessTest = NULL;
144  EXPECT_EQ(0, liveListSize());
145 }
146 
147 TEST(RefcntTest, BoolAndLogicalNotOperatorOverloads)
148 {
149  // Test bool and ! operator overloads.
150  Ptr boolTest = new TestRC();
151  EXPECT_EQ(boolTest, true);
152  EXPECT_EQ(!boolTest, false);
153  boolTest = NULL;
154  EXPECT_FALSE(boolTest);
155  EXPECT_TRUE(!boolTest);
156  EXPECT_EQ(0, liveListSize());
157 }
158 
159 TEST(RefcntTest, EqualityOperators)
160 {
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);
173 }
refcnt.hh
TEST
TEST(RefcntTest, NullPointerCheck)
Definition: refcnt.test.cc:74
EXPECT_EQ
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:110
EXPECT_FALSE
#define EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
Definition: unittest.hh:106
EXPECT_TRUE
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:103
RefCounted
Derive from RefCounted if you want to enable reference counting of this class.
Definition: refcnt.hh:57
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
RefCountingPtr
If you want a reference counting pointer to a mutable object, create it like this:
Definition: refcnt.hh:118
std::list
STL list class.
Definition: stl.hh:51

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17