]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testRefCount.cc
2cd30359a04250df618a0e0b4df38816d9b7b125
2 * Copyright (C) 1996-2017 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"
14 // XXX: upgrade these tests to CPPUnit testing framework
16 class _ToRefCount
: public RefCountable
19 _ToRefCount () {++Instances
;}
20 ~_ToRefCount() {--Instances
;}
32 typedef RefCount
<_ToRefCount
> ToRefCount
;
34 /* Must be zero at the end for the test to pass. */
35 int _ToRefCount::Instances
= 0;
37 class AlsoRefCountable
: public RefCountable
, public _ToRefCount
40 typedef RefCount
<AlsoRefCountable
> Pointer
;
50 main (int argc
, char **argv
)
53 ToRefCount
anObject(new _ToRefCount
);
54 anObject
->someMethod();
56 ToRefCount
objectTwo (anObject
);
59 ToRefCount
anotherObject(new _ToRefCount
);
60 anObject
= anotherObject
;
64 ToRefCount
aForthObject (anObject
);
65 anObject
= ToRefCount(NULL
);
66 aForthObject
->someMethod();
71 /* Test creating an object, using it , and then making available as a
75 _ToRefCount
*aPointer
= new _ToRefCount
;
76 aPointer
->someMethod();
77 ToRefCount
anObject(aPointer
);
79 /* standalone pointers should be usable */
83 /* Can we check pointers for equality */
86 ToRefCount
anotherObject(new _ToRefCount
);
88 if (anObject
== anotherObject
)
93 if (!(anObject
== anotherObject
))
96 /* Can we get the pointer for a const object */
98 ToRefCount
anObject (new _ToRefCount
);
99 ToRefCount
const aConstObject (anObject
);
100 _ToRefCount
const *aPointer
= aConstObject
.getRaw();
102 if (aPointer
!= anObject
.getRaw())
105 /* Can we get a refcounted pointer from a const object */
107 _ToRefCount
const * aPointer
= new _ToRefCount
;
108 ToRefCount
anObject (aPointer
);
110 /* Can we get a pointer to nonconst from a nonconst refcounter */
112 ToRefCount
anObject (new _ToRefCount
);
113 _ToRefCount
*aPointer
= anObject
.getRaw();
114 if (aPointer
== NULL
)
118 /* Create a doubley inheriting refcount instance,
119 * cast to a single inheritance instance,
123 ToRefCount aBaseObject
;
125 AlsoRefCountable::Pointer
anObject (new AlsoRefCountable
);
126 aBaseObject
= anObject
.getRaw();
129 return _ToRefCount::Instances
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;