]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testRefCount.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / tests / testRefCount.cc
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 "testRefCount.h"
14 #include "unitTestMain.h"
15
16 CPPUNIT_TEST_SUITE_REGISTRATION( testRefCount );
17
18 class _ToRefCount : public RefCountable
19 {
20 public:
21 _ToRefCount () {++Instances;}
22 ~_ToRefCount() override {--Instances;}
23
24 int someMethod() {
25 if (!Instances)
26 return 0;
27
28 return 1;
29 }
30
31 static int Instances;
32 };
33
34 typedef RefCount<_ToRefCount> ToRefCount;
35
36 int _ToRefCount::Instances = 0;
37
38 class AlsoRefCountable : public RefCountable, public _ToRefCount
39 {
40 public:
41 typedef RefCount<AlsoRefCountable> Pointer;
42
43 int doSomething() {
44 if (!Instances)
45 return 0;
46 return 1;
47 }
48 };
49
50 void
51 testRefCount::testCountability()
52 {
53 {
54 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
55 ToRefCount anObject(new _ToRefCount);
56 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
57 CPPUNIT_ASSERT_EQUAL(1, anObject->someMethod());
58 anObject = *&anObject; // test self-assign without -Wself-assign-overloaded warnings
59 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
60 ToRefCount objectTwo (anObject);
61 anObject = objectTwo;
62 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
63 {
64 ToRefCount anotherObject(new _ToRefCount);
65 anObject = anotherObject;
66 CPPUNIT_ASSERT_EQUAL(2, _ToRefCount::Instances);
67 }
68
69 {
70 ToRefCount aForthObject (anObject);
71 CPPUNIT_ASSERT_EQUAL(2, _ToRefCount::Instances);
72 anObject = ToRefCount(nullptr);
73 CPPUNIT_ASSERT_EQUAL(2, _ToRefCount::Instances);
74 CPPUNIT_ASSERT_EQUAL(1, aForthObject->someMethod());
75 aForthObject = nullptr;
76 }
77 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
78 }
79 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
80 }
81
82 void
83 testRefCount::testObjectToRefCounted()
84 {
85 /* Test creating an object, using it , and then making available as a
86 * refcounted one:
87 */
88 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
89 _ToRefCount *aPointer = new _ToRefCount;
90 CPPUNIT_ASSERT_EQUAL(1, aPointer->someMethod());
91 ToRefCount anObject(aPointer);
92 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
93 }
94
95 void
96 testRefCount::testStandalonePointer()
97 {
98 /* standalone pointers should be usable */
99 ToRefCount anObject;
100 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
101 }
102
103 void
104 testRefCount::testCheckPointers()
105 {
106 /* Can we check pointers for equality */
107 ToRefCount anObject;
108 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
109 ToRefCount anotherObject(new _ToRefCount);
110
111 CPPUNIT_ASSERT(anObject != anotherObject);
112
113 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
114 anotherObject = nullptr;
115
116 CPPUNIT_ASSERT_EQUAL(anObject, anotherObject);
117 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
118 }
119
120 void
121 testRefCount::testPointerConst()
122 {
123 /* Can we get the pointer for a const object */
124 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
125 ToRefCount anObject (new _ToRefCount);
126 ToRefCount const aConstObject (anObject);
127 _ToRefCount const *aPointer = aConstObject.getRaw();
128
129 CPPUNIT_ASSERT(aPointer == anObject.getRaw());
130 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
131 }
132
133 void testRefCount::testRefCountFromConst()
134 {
135 /* Can we get a refcounted pointer from a const object */
136 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
137 _ToRefCount const * aPointer = new _ToRefCount;
138 ToRefCount anObject (aPointer);
139
140 CPPUNIT_ASSERT(aPointer == anObject.getRaw());
141 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
142 }
143
144 void
145 testRefCount::testPointerFromRefCounter()
146 {
147 /* Can we get a pointer to nonconst from a nonconst refcounter */
148 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
149 ToRefCount anObject (new _ToRefCount);
150 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
151 _ToRefCount *aPointer = anObject.getRaw();
152 CPPUNIT_ASSERT(aPointer != nullptr);
153 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
154 }
155
156 void
157 testRefCount::testDoubleInheritToSingleInherit()
158 {
159
160 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
161 /* Create a doubley inheriting refcount instance,
162 * cast to a single inheritance instance,
163 * then hope :}
164 */
165 ToRefCount aBaseObject;
166 {
167 AlsoRefCountable::Pointer anObject (new AlsoRefCountable);
168 aBaseObject = anObject.getRaw();
169 CPPUNIT_ASSERT_EQUAL(1, anObject->doSomething());
170 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
171 }
172 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
173 }
174