testRefCount.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section -- Refcount allocator */
10 
11 #include "squid.h"
12 #include "base/RefCount.h"
13 #include "compat/cppunit.h"
14 #include "unitTestMain.h"
15 
16 class TestRefCount : public CPPUNIT_NS::TestFixture
17 {
28 
29 protected:
30  void testCountability();
32  void testStandalonePointer();
33  void testCheckPointers();
34  void testPointerConst();
35  void testRefCountFromConst();
38 };
39 
41 
42 class _ToRefCount : public RefCountable
43 {
44 public:
46  ~_ToRefCount() override {--Instances;}
47 
48  int someMethod() {
49  if (!Instances)
50  return 0;
51 
52  return 1;
53  }
54 
55  static int Instances;
56 };
57 
59 
61 
63 {
64 public:
66 
67  int doSomething() {
68  if (!Instances)
69  return 0;
70  return 1;
71  }
72 };
73 
74 void
76 {
77  {
78  CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
79  ToRefCount anObject(new _ToRefCount);
80  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
81  CPPUNIT_ASSERT_EQUAL(1, anObject->someMethod());
82  anObject = *&anObject; // test self-assign without -Wself-assign-overloaded warnings
83  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
84  ToRefCount objectTwo (anObject);
85  anObject = objectTwo;
86  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
87  {
88  ToRefCount anotherObject(new _ToRefCount);
89  anObject = anotherObject;
90  CPPUNIT_ASSERT_EQUAL(2, _ToRefCount::Instances);
91  }
92 
93  {
94  ToRefCount aForthObject (anObject);
95  CPPUNIT_ASSERT_EQUAL(2, _ToRefCount::Instances);
96  anObject = ToRefCount(nullptr);
97  CPPUNIT_ASSERT_EQUAL(2, _ToRefCount::Instances);
98  CPPUNIT_ASSERT_EQUAL(1, aForthObject->someMethod());
99  aForthObject = nullptr;
100  }
101  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
102  }
103  CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
104 }
105 
106 void
108 {
109  /* Test creating an object, using it , and then making available as a
110  * refcounted one:
111  */
112  CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
113  _ToRefCount *aPointer = new _ToRefCount;
114  CPPUNIT_ASSERT_EQUAL(1, aPointer->someMethod());
115  ToRefCount anObject(aPointer);
116  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
117 }
118 
119 void
121 {
122  /* standalone pointers should be usable */
123  ToRefCount anObject;
124  CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
125 }
126 
127 void
129 {
130  /* Can we check pointers for equality */
131  ToRefCount anObject;
132  CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
133  ToRefCount anotherObject(new _ToRefCount);
134 
135  CPPUNIT_ASSERT(anObject != anotherObject);
136 
137  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
138  anotherObject = nullptr;
139 
140  CPPUNIT_ASSERT_EQUAL(anObject, anotherObject);
141  CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
142 }
143 
144 void
146 {
147  /* Can we get the pointer for a const object */
148  CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
149  ToRefCount anObject (new _ToRefCount);
150  ToRefCount const aConstObject (anObject);
151  _ToRefCount const *aPointer = aConstObject.getRaw();
152 
153  CPPUNIT_ASSERT(aPointer == anObject.getRaw());
154  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
155 }
156 
158 {
159  /* Can we get a refcounted pointer from a const object */
160  CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
161  _ToRefCount const * aPointer = new _ToRefCount;
162  ToRefCount anObject (aPointer);
163 
164  CPPUNIT_ASSERT(aPointer == anObject.getRaw());
165  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
166 }
167 
168 void
170 {
171  /* Can we get a pointer to nonconst from a nonconst refcounter */
172  CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
173  ToRefCount anObject (new _ToRefCount);
174  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
175  _ToRefCount *aPointer = anObject.getRaw();
176  CPPUNIT_ASSERT(aPointer != nullptr);
177  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
178 }
179 
180 void
182 {
183 
184  CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
185  /* Create a doubley inheriting refcount instance,
186  * cast to a single inheritance instance,
187  * then hope :}
188  */
189  ToRefCount aBaseObject;
190  {
192  aBaseObject = anObject.getRaw();
193  CPPUNIT_ASSERT_EQUAL(1, anObject->doSomething());
194  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
195  }
196  CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
197 }
198 
199 int
200 main(int argc, char *argv[])
201 {
202  return TestProgram().run(argc, argv);
203 }
204 
void testDoubleInheritToSingleInherit()
CPPUNIT_TEST_SUITE_END()
void testRefCountFromConst()
int main(int argc, char *argv[])
RefCount< AlsoRefCountable > Pointer
Definition: testRefCount.cc:65
int someMethod()
Definition: testRefCount.cc:48
CPPUNIT_TEST_SUITE_REGISTRATION(TestRefCount)
implements test program's main() function while enabling customization
Definition: unitTestMain.h:25
static int Instances
Definition: testRefCount.cc:55
~_ToRefCount() override
Definition: testRefCount.cc:46
C * getRaw() const
Definition: RefCount.h:89
CPPUNIT_TEST_SUITE(TestRefCount)
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
void testPointerConst()
void testStandalonePointer()
CPPUNIT_TEST(testCountability)
void testCountability()
Definition: testRefCount.cc:75
void testPointerFromRefCounter()
void testCheckPointers()
void testObjectToRefCounted()
RefCount< _ToRefCount > ToRefCount
Definition: testRefCount.cc:58

 

Introduction

Documentation

Support

Miscellaneous