]> git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/VirtualDeleteOperator.cc
Merge from trunk
[thirdparty/squid.git] / test-suite / VirtualDeleteOperator.cc
1
2 /*
3 * $Id: VirtualDeleteOperator.cc,v 1.2 2004/08/15 17:41:28 robertc Exp $
4 *
5 * AUTHOR: Robert Collins
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 * Copyright (c) 2003 Robert Collins <robertc@squid-cache.org>
34 */
35
36 #include "squid.h"
37 #include <iostream>
38
39 class CallCounter {
40 public:
41 CallCounter();
42 void recordNew();
43 void recordDelete();
44 size_t news() const;
45 size_t deletes() const;
46 private:
47 size_t _news, _deletes;
48 };
49
50 CallCounter::CallCounter() : _news(0), _deletes(0) {}
51
52 void CallCounter::recordNew() { ++_news;}
53 void CallCounter::recordDelete() { ++_deletes;}
54 size_t CallCounter::news() const {return _news;}
55 size_t CallCounter::deletes() const {return _deletes;}
56
57 class BaseVirtual {
58 public:
59 void *operator new (size_t);
60 void operator delete (void *);
61 virtual ~BaseVirtual();
62 static void DeleteABase(BaseVirtual *aBase);
63 static CallCounter Calls;
64 };
65
66 CallCounter BaseVirtual::Calls;
67
68 void *
69 BaseVirtual::operator new(size_t byteCount)
70 {
71 Calls.recordNew();
72 return ::operator new (byteCount);
73 }
74
75 void
76 BaseVirtual::operator delete(void *address)
77 {
78 Calls.recordDelete();
79 ::operator delete (address);
80 }
81
82 void
83 BaseVirtual::DeleteABase(BaseVirtual *aBase)
84 {
85 delete aBase;
86 }
87
88 BaseVirtual::~BaseVirtual(){}
89
90 class ChildVirtual : public BaseVirtual {
91 public:
92 void *operator new (size_t);
93 void operator delete (void *);
94 virtual ~ChildVirtual();
95 static CallCounter Calls;
96 };
97
98 CallCounter ChildVirtual::Calls;
99
100 void *
101 ChildVirtual::operator new(size_t byteCount)
102 {
103 Calls.recordNew();
104 return ::operator new (byteCount);
105 }
106
107 void
108 ChildVirtual::operator delete(void *address)
109 {
110 Calls.recordDelete();
111 ::operator delete (address);
112 }
113
114 ChildVirtual::~ChildVirtual(){}
115
116 int
117 main(int argc, char **argv)
118 {
119 assert (BaseVirtual::Calls.news() == 0);
120 assert (BaseVirtual::Calls.deletes() == 0);
121 assert (ChildVirtual::Calls.news() == 0);
122 assert (ChildVirtual::Calls.deletes() == 0);
123 BaseVirtual *aBase = new ChildVirtual;
124 assert (BaseVirtual::Calls.news() == 0);
125 assert (BaseVirtual::Calls.deletes() == 0);
126 assert (ChildVirtual::Calls.news() == 1);
127 assert (ChildVirtual::Calls.deletes() == 0);
128 BaseVirtual::DeleteABase(aBase);
129 assert (BaseVirtual::Calls.news() == 0);
130 assert (BaseVirtual::Calls.deletes() == 0);
131 assert (ChildVirtual::Calls.news() == 1);
132 assert (ChildVirtual::Calls.deletes() == 1);
133 // deleting NULL works.
134 BaseVirtual::DeleteABase(NULL);
135 assert (BaseVirtual::Calls.news() == 0);
136 assert (BaseVirtual::Calls.deletes() == 0);
137 assert (ChildVirtual::Calls.news() == 1);
138 assert (ChildVirtual::Calls.deletes() == 1);
139 return 0;
140 }