]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testRefCount.cc
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section -- Refcount allocator */
12 #include "base/RefCount.h"
13 #include "testRefCount.h"
14 #include "unitTestMain.h"
16 CPPUNIT_TEST_SUITE_REGISTRATION( testRefCount
);
18 class _ToRefCount
: public RefCountable
21 _ToRefCount () {++Instances
;}
22 ~_ToRefCount() override
{--Instances
;}
34 typedef RefCount
<_ToRefCount
> ToRefCount
;
36 int _ToRefCount::Instances
= 0;
38 class AlsoRefCountable
: public RefCountable
, public _ToRefCount
41 typedef RefCount
<AlsoRefCountable
> Pointer
;
51 testRefCount::testCountability()
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
);
62 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances
);
64 ToRefCount
anotherObject(new _ToRefCount
);
65 anObject
= anotherObject
;
66 CPPUNIT_ASSERT_EQUAL(2, _ToRefCount::Instances
);
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;
77 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances
);
79 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances
);
83 testRefCount::testObjectToRefCounted()
85 /* Test creating an object, using it , and then making available as a
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
);
96 testRefCount::testStandalonePointer()
98 /* standalone pointers should be usable */
100 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances
);
104 testRefCount::testCheckPointers()
106 /* Can we check pointers for equality */
108 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances
);
109 ToRefCount
anotherObject(new _ToRefCount
);
111 CPPUNIT_ASSERT(anObject
!= anotherObject
);
113 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances
);
114 anotherObject
= nullptr;
116 CPPUNIT_ASSERT_EQUAL(anObject
, anotherObject
);
117 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances
);
121 testRefCount::testPointerConst()
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();
129 CPPUNIT_ASSERT(aPointer
== anObject
.getRaw());
130 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances
);
133 void testRefCount::testRefCountFromConst()
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
);
140 CPPUNIT_ASSERT(aPointer
== anObject
.getRaw());
141 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances
);
145 testRefCount::testPointerFromRefCounter()
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
);
157 testRefCount::testDoubleInheritToSingleInherit()
160 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances
);
161 /* Create a doubley inheriting refcount instance,
162 * cast to a single inheritance instance,
165 ToRefCount aBaseObject
;
167 AlsoRefCountable::Pointer
anObject (new AlsoRefCountable
);
168 aBaseObject
= anObject
.getRaw();
169 CPPUNIT_ASSERT_EQUAL(1, anObject
->doSomething());
170 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances
);
172 CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances
);