testRefCount.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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
17
19{
20public:
23
24 int someMethod() {
25 if (!Instances)
26 return 0;
27
28 return 1;
29 }
30
31 static int Instances;
32};
33
35
37
39{
40public:
42
44 if (!Instances)
45 return 0;
46 return 1;
47 }
48};
49
50void
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
82void
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
95void
97{
98 /* standalone pointers should be usable */
99 ToRefCount anObject;
100 CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
101}
102
103void
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
120void
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
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
144void
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
156void
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 {
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
#define RefCountable
The locking interface for use on Reference-Counted classes.
Definition: Lock.h:66
RefCount< AlsoRefCountable > Pointer
Definition: testRefCount.cc:41
C * getRaw() const
Definition: RefCount.h:80
static int Instances
Definition: testRefCount.cc:31
int someMethod()
Definition: testRefCount.cc:24
void testCountability()
Definition: testRefCount.cc:51
void testPointerFromRefCounter()
void testRefCountFromConst()
void testObjectToRefCounted()
Definition: testRefCount.cc:83
void testCheckPointers()
void testDoubleInheritToSingleInherit()
void testPointerConst()
void testStandalonePointer()
Definition: testRefCount.cc:96
CPPUNIT_TEST_SUITE_REGISTRATION(testRefCount)
RefCount< _ToRefCount > ToRefCount
Definition: testRefCount.cc:34

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors