1 /* 2 * Copyright (C) 1996-2021 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 14 // XXX: upgrade these tests to CPPUnit testing framework 15 16 class _ToRefCount : public RefCountable 17 { 18 public: _ToRefCount()19 _ToRefCount () {++Instances;} ~_ToRefCount()20 ~_ToRefCount() {--Instances;} 21 someMethod()22 int someMethod() { 23 if (!Instances) 24 exit(1); 25 26 return 1; 27 } 28 29 static int Instances; 30 }; 31 32 typedef RefCount<_ToRefCount> ToRefCount; 33 34 /* Must be zero at the end for the test to pass. */ 35 int _ToRefCount::Instances = 0; 36 37 class AlsoRefCountable : public RefCountable, public _ToRefCount 38 { 39 public: 40 typedef RefCount<AlsoRefCountable> Pointer; 41 doSomething()42 int doSomething() { 43 if (!Instances) 44 exit (1); 45 return 1; 46 } 47 }; 48 49 int main(int argc,char ** argv)50main (int argc, char **argv) 51 { 52 { 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 } 69 } 70 71 /* Test creating an object, using it , and then making available as a 72 * refcounted one: 73 */ 74 { 75 _ToRefCount *aPointer = new _ToRefCount; 76 aPointer->someMethod(); 77 ToRefCount anObject(aPointer); 78 } 79 /* standalone pointers should be usable */ 80 { 81 ToRefCount anObject; 82 } 83 /* Can we check pointers for equality */ 84 { 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); 95 } 96 /* Can we get the pointer for a const object */ 97 { 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); 104 } 105 /* Can we get a refcounted pointer from a const object */ 106 { 107 _ToRefCount const * aPointer = new _ToRefCount; 108 ToRefCount anObject (aPointer); 109 } 110 /* Can we get a pointer to nonconst from a nonconst refcounter */ 111 { 112 ToRefCount anObject (new _ToRefCount); 113 _ToRefCount *aPointer = anObject.getRaw(); 114 if (aPointer == NULL) 115 exit(3); 116 aPointer = NULL; 117 } 118 /* Create a doubley inheriting refcount instance, 119 * cast to a single inheritance instance, 120 * then hope :} 121 */ 122 { 123 ToRefCount aBaseObject; 124 { 125 AlsoRefCountable::Pointer anObject (new AlsoRefCountable); 126 aBaseObject = anObject.getRaw(); 127 } 128 } 129 return _ToRefCount::Instances == 0 ? 0 : 1; 130 } 131 132