]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testRefCount.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / tests / testRefCount.cc
CommitLineData
1077c1b8 1/*
4ac4a490 2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
1077c1b8 3 *
4e0938ef
AJ
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.
1077c1b8 7 */
8
4e0938ef
AJ
9/* DEBUG: section -- Refcount allocator */
10
582c2af2 11#include "squid.h"
8bf217bd 12#include "base/RefCount.h"
1077c1b8 13
8bf217bd 14// XXX: upgrade these tests to CPPUnit testing framework
00d77d6b 15
8bf217bd
AJ
16class _ToRefCount : public RefCountable
17{
1077c1b8 18public:
19 _ToRefCount () {++Instances;}
20 ~_ToRefCount() {--Instances;}
00d77d6b 21
26ac0430 22 int someMethod() {
13e8565c 23 if (!Instances)
00d77d6b 24 exit(1);
25
26 return 1;
27 }
28
1077c1b8 29 static int Instances;
1077c1b8 30};
31
32typedef RefCount<_ToRefCount> ToRefCount;
33
34/* Must be zero at the end for the test to pass. */
35int _ToRefCount::Instances = 0;
36
00d77d6b 37class AlsoRefCountable : public RefCountable, public _ToRefCount
38{
00d77d6b 39public:
128ef305 40 typedef RefCount<AlsoRefCountable> Pointer;
00d77d6b 41
26ac0430 42 int doSomething() {
13e8565c 43 if (!Instances)
26ac0430
AJ
44 exit (1);
45 return 1;
46 }
128ef305 47};
48
1077c1b8 49int
50main (int argc, char **argv)
51{
52 {
00d77d6b 53 ToRefCount anObject(new _ToRefCount);
54 anObject->someMethod();
55 anObject = anObject;
56 ToRefCount objectTwo (anObject);
57 anObject = objectTwo;
58 {
59 ToRefCount anotherObject(new _ToRefCount);
60 anObject = anotherObject;
61 }
62
63 {
64 ToRefCount aForthObject (anObject);
65 anObject = ToRefCount(NULL);
66 aForthObject->someMethod();
67 aForthObject = NULL;
68 }
1077c1b8 69 }
00d77d6b 70
1077c1b8 71 /* Test creating an object, using it , and then making available as a
72 * refcounted one:
73 */
74 {
00d77d6b 75 _ToRefCount *aPointer = new _ToRefCount;
76 aPointer->someMethod();
77 ToRefCount anObject(aPointer);
1077c1b8 78 }
79 /* standalone pointers should be usable */
80 {
00d77d6b 81 ToRefCount anObject;
1077c1b8 82 }
83 /* Can we check pointers for equality */
84 {
00d77d6b 85 ToRefCount anObject;
86 ToRefCount anotherObject(new _ToRefCount);
87
88 if (anObject == anotherObject)
89 exit (1);
90
91 anotherObject = NULL;
92
93 if (!(anObject == anotherObject))
94 exit (1);
1077c1b8 95 }
96 /* Can we get the pointer for a const object */
97 {
00d77d6b 98 ToRefCount anObject (new _ToRefCount);
99 ToRefCount const aConstObject (anObject);
100 _ToRefCount const *aPointer = aConstObject.getRaw();
101
102 if (aPointer != anObject.getRaw())
103 exit (2);
1077c1b8 104 }
105 /* Can we get a refcounted pointer from a const object */
106 {
00d77d6b 107 _ToRefCount const * aPointer = new _ToRefCount;
108 ToRefCount anObject (aPointer);
1077c1b8 109 }
110 /* Can we get a pointer to nonconst from a nonconst refcounter */
111 {
00d77d6b 112 ToRefCount anObject (new _ToRefCount);
113 _ToRefCount *aPointer = anObject.getRaw();
b51e6f82
AJ
114 if (aPointer == NULL)
115 exit(3);
00d77d6b 116 aPointer = NULL;
1077c1b8 117 }
128ef305 118 /* Create a doubley inheriting refcount instance,
119 * cast to a single inheritance instance,
120 * then hope :}
121 */
122 {
00d77d6b 123 ToRefCount aBaseObject;
124 {
125 AlsoRefCountable::Pointer anObject (new AlsoRefCountable);
126 aBaseObject = anObject.getRaw();
127 }
128ef305 128 }
1077c1b8 129 return _ToRefCount::Instances == 0 ? 0 : 1;
130}
f53969cc 131