]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testRefCount.cc
2 * DEBUG: section -- Refcount allocator
3 * AUTHOR: Robert Collins
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
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.
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.
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.
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.
34 #include "base/RefCount.h"
36 // XXX: upgrade these tests to CPPUnit testing framework
38 class _ToRefCount
: public RefCountable
41 _ToRefCount () {++Instances
;}
42 ~_ToRefCount() {--Instances
;}
54 typedef RefCount
<_ToRefCount
> ToRefCount
;
56 /* Must be zero at the end for the test to pass. */
57 int _ToRefCount::Instances
= 0;
59 class AlsoRefCountable
: public RefCountable
, public _ToRefCount
62 typedef RefCount
<AlsoRefCountable
> Pointer
;
72 main (int argc
, char **argv
)
75 ToRefCount
anObject(new _ToRefCount
);
76 anObject
->someMethod();
78 ToRefCount
objectTwo (anObject
);
81 ToRefCount
anotherObject(new _ToRefCount
);
82 anObject
= anotherObject
;
86 ToRefCount
aForthObject (anObject
);
87 anObject
= ToRefCount(NULL
);
88 aForthObject
->someMethod();
93 /* Test creating an object, using it , and then making available as a
97 _ToRefCount
*aPointer
= new _ToRefCount
;
98 aPointer
->someMethod();
99 ToRefCount
anObject(aPointer
);
101 /* standalone pointers should be usable */
105 /* Can we check pointers for equality */
108 ToRefCount
anotherObject(new _ToRefCount
);
110 if (anObject
== anotherObject
)
113 anotherObject
= NULL
;
115 if (!(anObject
== anotherObject
))
118 /* Can we get the pointer for a const object */
120 ToRefCount
anObject (new _ToRefCount
);
121 ToRefCount
const aConstObject (anObject
);
122 _ToRefCount
const *aPointer
= aConstObject
.getRaw();
124 if (aPointer
!= anObject
.getRaw())
127 /* Can we get a refcounted pointer from a const object */
129 _ToRefCount
const * aPointer
= new _ToRefCount
;
130 ToRefCount
anObject (aPointer
);
132 /* Can we get a pointer to nonconst from a nonconst refcounter */
134 ToRefCount
anObject (new _ToRefCount
);
135 _ToRefCount
*aPointer
= anObject
.getRaw();
136 if (aPointer
== NULL
)
140 /* Create a doubley inheriting refcount instance,
141 * cast to a single inheritance instance,
145 ToRefCount aBaseObject
;
147 AlsoRefCountable::Pointer
anObject (new AlsoRefCountable
);
148 aBaseObject
= anObject
.getRaw();
151 return _ToRefCount::Instances
== 0 ? 0 : 1;