]> git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/VirtualDeleteOperator.cc
SourceFormat: Main reformat push
[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 {
41 public:
42 CallCounter();
43 void recordNew();
44 void recordDelete();
45 size_t news() const;
46 size_t deletes() const;
47 private:
48 size_t _news, _deletes;
49 };
50
51 CallCounter::CallCounter() : _news(0), _deletes(0) {}
52
53 void CallCounter::recordNew() { ++_news;}
54 void CallCounter::recordDelete() { ++_deletes;}
55 size_t CallCounter::news() const {return _news;}
56 size_t CallCounter::deletes() const {return _deletes;}
57
58 class BaseVirtual
59 {
60 public:
61 void *operator new (size_t);
62 void operator delete (void *);
63 virtual ~BaseVirtual();
64 static void DeleteABase(BaseVirtual *aBase);
65 static CallCounter Calls;
66 };
67
68 CallCounter BaseVirtual::Calls;
69
70 void *
71 BaseVirtual::operator new(size_t byteCount)
72 {
73 Calls.recordNew();
74 return ::operator new (byteCount);
75 }
76
77 void
78 BaseVirtual::operator delete(void *address)
79 {
80 Calls.recordDelete();
81 ::operator delete (address);
82 }
83
84 void
85 BaseVirtual::DeleteABase(BaseVirtual *aBase)
86 {
87 delete aBase;
88 }
89
90 BaseVirtual::~BaseVirtual() {}
91
92 class ChildVirtual : public BaseVirtual
93 {
94 public:
95 void *operator new (size_t);
96 void operator delete (void *);
97 virtual ~ChildVirtual();
98 static CallCounter Calls;
99 };
100
101 CallCounter ChildVirtual::Calls;
102
103 void *
104 ChildVirtual::operator new(size_t byteCount)
105 {
106 Calls.recordNew();
107 return ::operator new (byteCount);
108 }
109
110 void
111 ChildVirtual::operator delete(void *address)
112 {
113 Calls.recordDelete();
114 ::operator delete (address);
115 }
116
117 ChildVirtual::~ChildVirtual() {}
118
119 int
120 main(int argc, char **argv)
121 {
122 assert (BaseVirtual::Calls.news() == 0);
123 assert (BaseVirtual::Calls.deletes() == 0);
124 assert (ChildVirtual::Calls.news() == 0);
125 assert (ChildVirtual::Calls.deletes() == 0);
126 BaseVirtual *aBase = new ChildVirtual;
127 assert (BaseVirtual::Calls.news() == 0);
128 assert (BaseVirtual::Calls.deletes() == 0);
129 assert (ChildVirtual::Calls.news() == 1);
130 assert (ChildVirtual::Calls.deletes() == 0);
131 BaseVirtual::DeleteABase(aBase);
132 assert (BaseVirtual::Calls.news() == 0);
133 assert (BaseVirtual::Calls.deletes() == 0);
134 assert (ChildVirtual::Calls.news() == 1);
135 assert (ChildVirtual::Calls.deletes() == 1);
136 // deleting NULL works.
137 BaseVirtual::DeleteABase(NULL);
138 assert (BaseVirtual::Calls.news() == 0);
139 assert (BaseVirtual::Calls.deletes() == 0);
140 assert (ChildVirtual::Calls.news() == 1);
141 assert (ChildVirtual::Calls.deletes() == 1);
142 return 0;
143 }