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

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13