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
16class TestRefCount : public CPPUNIT_NS::TestFixture
17{
28
29protected:
30 void testCountability();
33 void testCheckPointers();
34 void testPointerConst();
38};
39
41
43{
44public:
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{
64public:
66
68 if (!Instances)
69 return 0;
70 return 1;
71 }
72};
73
74void
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
106void
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
119void
121{
122 /* standalone pointers should be usable */
123 ToRefCount anObject;
124 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
125}
126
127void
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
144void
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
168void
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
180void
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
199int
200main(int argc, char *argv[])
201{
202 return TestProgram().run(argc, argv);
203}
204
#define RefCountable
The locking interface for use on Reference-Counted classes.
Definition: Lock.h:66
RefCount< AlsoRefCountable > Pointer
Definition: testRefCount.cc:65
C * getRaw() const
Definition: RefCount.h:89
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
CPPUNIT_TEST(testCountability)
CPPUNIT_TEST(testStandalonePointer)
void testObjectToRefCounted()
CPPUNIT_TEST_SUITE_END()
CPPUNIT_TEST(testCheckPointers)
CPPUNIT_TEST(testObjectToRefCounted)
void testPointerFromRefCounter()
CPPUNIT_TEST(testRefCountFromConst)
void testStandalonePointer()
CPPUNIT_TEST(testDoubleInheritToSingleInherit)
void testPointerConst()
void testRefCountFromConst()
void testCountability()
Definition: testRefCount.cc:75
CPPUNIT_TEST_SUITE(TestRefCount)
CPPUNIT_TEST(testPointerFromRefCounter)
void testDoubleInheritToSingleInherit()
CPPUNIT_TEST(testPointerConst)
void testCheckPointers()
static int Instances
Definition: testRefCount.cc:55
int someMethod()
Definition: testRefCount.cc:48
~_ToRefCount() override
Definition: testRefCount.cc:46
int main(int argc, char *argv[])
CPPUNIT_TEST_SUITE_REGISTRATION(TestRefCount)
RefCount< _ToRefCount > ToRefCount
Definition: testRefCount.cc:58

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors