]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testRefCount.cc
merge from trunk r12441
[thirdparty/squid.git] / src / tests / testRefCount.cc
1 /*
2 * DEBUG: section -- Refcount allocator
3 * AUTHOR: Robert Collins
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 #include "squid.h"
34 #include "base/RefCount.h"
35
36 // XXX: upgrade these tests to CPPUnit testing framework
37
38 class _ToRefCount : public RefCountable
39 {
40 public:
41 _ToRefCount () {++Instances;}
42 ~_ToRefCount() {--Instances;}
43
44 int someMethod() {
45 if (!this)
46 exit(1);
47
48 return 1;
49 }
50
51 static int Instances;
52 };
53
54 typedef RefCount<_ToRefCount> ToRefCount;
55
56 /* Must be zero at the end for the test to pass. */
57 int _ToRefCount::Instances = 0;
58
59 class AlsoRefCountable : public RefCountable, public _ToRefCount
60 {
61 public:
62 typedef RefCount<AlsoRefCountable> Pointer;
63
64 int doSomething() {
65 if (!this)
66 exit (1);
67 return 1;
68 }
69 };
70
71 int
72 main (int argc, char **argv)
73 {
74 {
75 ToRefCount anObject(new _ToRefCount);
76 anObject->someMethod();
77 anObject = anObject;
78 ToRefCount objectTwo (anObject);
79 anObject = objectTwo;
80 {
81 ToRefCount anotherObject(new _ToRefCount);
82 anObject = anotherObject;
83 }
84
85 {
86 ToRefCount aForthObject (anObject);
87 anObject = ToRefCount(NULL);
88 aForthObject->someMethod();
89 aForthObject = NULL;
90 }
91 }
92
93 /* Test creating an object, using it , and then making available as a
94 * refcounted one:
95 */
96 {
97 _ToRefCount *aPointer = new _ToRefCount;
98 aPointer->someMethod();
99 ToRefCount anObject(aPointer);
100 }
101 /* standalone pointers should be usable */
102 {
103 ToRefCount anObject;
104 }
105 /* Can we check pointers for equality */
106 {
107 ToRefCount anObject;
108 ToRefCount anotherObject(new _ToRefCount);
109
110 if (anObject == anotherObject)
111 exit (1);
112
113 anotherObject = NULL;
114
115 if (!(anObject == anotherObject))
116 exit (1);
117 }
118 /* Can we get the pointer for a const object */
119 {
120 ToRefCount anObject (new _ToRefCount);
121 ToRefCount const aConstObject (anObject);
122 _ToRefCount const *aPointer = aConstObject.getRaw();
123
124 if (aPointer != anObject.getRaw())
125 exit (2);
126 }
127 /* Can we get a refcounted pointer from a const object */
128 {
129 _ToRefCount const * aPointer = new _ToRefCount;
130 ToRefCount anObject (aPointer);
131 }
132 /* Can we get a pointer to nonconst from a nonconst refcounter */
133 {
134 ToRefCount anObject (new _ToRefCount);
135 _ToRefCount *aPointer = anObject.getRaw();
136 if (aPointer == NULL)
137 exit(3);
138 aPointer = NULL;
139 }
140 /* Create a doubley inheriting refcount instance,
141 * cast to a single inheritance instance,
142 * then hope :}
143 */
144 {
145 ToRefCount aBaseObject;
146 {
147 AlsoRefCountable::Pointer anObject (new AlsoRefCountable);
148 aBaseObject = anObject.getRaw();
149 }
150 }
151 return _ToRefCount::Instances == 0 ? 0 : 1;
152 }