splay.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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/*
10 * based on ftp://ftp.cs.cmu.edu/user/sleator/splaying/top-down-splay.c
11 * http://bobo.link.cs.cmu.edu/cgi-bin/splay/splay-cgi.pl
12 */
13
14#include "squid.h"
15#include "splay.h"
16#include "util.h"
17
18#include <cstdlib>
19#include <functional>
20#if HAVE_UNISTD_H
21#include <unistd.h>
22#endif
23#include <random>
24
26{
27
28public:
29 intnode() : i(0) {}
30
31 intnode (int anInt) : i (anInt) {}
32
33 int i;
34};
35
36static int
37compareintvoid(void *const &a, void *const &n)
38{
39 intnode *A = (intnode *)a;
40 intnode *B = (intnode *)n;
41 return A->i - B->i;
42}
43
44static int
45compareint(intnode *const &a, intnode *const &b)
46{
47 return a->i - b->i;
48}
49
51{
52
53public:
54 static void BeginWalk();
55 static int LastValue;
56 static bool ExpectedFail;
57 static void VisitVoid(void *const &);
58 static void VisitNode(intnode *const &);
59 static void VisitNodeRef(intnode const &);
60 static void CheckNode(intnode const &);
61};
62
64bool SplayCheck::ExpectedFail (false);
65
66void
68{
69 LastValue = 0;
70}
71
72void
74{
75 intnode *A = (intnode *)node;
76 CheckNode(*A);
77}
78
79void
81{
82 if (LastValue > A.i) {
83 /* failure */
84
85 if (!ExpectedFail)
86 exit(EXIT_FAILURE);
87 } else
88 /* success */
89 if (ExpectedFail)
90 exit(EXIT_FAILURE);
91
92 LastValue = A.i;
93}
94
95void
97{
98 CheckNode (*a);
99}
100
101void
103{
104 CheckNode (a);
105}
106
107static void
108destintvoid(void *&data)
109{
110 intnode *i = (intnode *)data;
111 xfree (i);
112}
113
114static void
116{
117 delete data;
118}
119
120static int
121compareintref(intnode const &a, intnode const &b)
122{
123 return a.i - b.i;
124}
125
126static void
128{}
129
130int
131main(int, char *[])
132{
133 std::mt19937 generator;
134 std::uniform_int_distribution<int> distribution;
135 auto nextRandom = std::bind (distribution, generator);
136
137 {
138 /* test void * splay containers */
139 const auto top = new Splay<void *>();
140
141 for (int i = 0; i < 100; ++i) {
142 intnode *I = (intnode *)xcalloc(sizeof(intnode), 1);
143 I->i = nextRandom();
144 top->insert(I, compareintvoid);
145 }
146
148 top->visit(SplayCheck::VisitVoid);
149
151 top->visit(SplayCheck::VisitVoid);
152 top->destroy(destintvoid);
153 }
154
155 /* test typesafe splay containers */
156 {
157 /* intnode* */
158 const auto safeTop = new Splay<intnode *>();
159
160 for ( int i = 0; i < 100; ++i) {
161 intnode *I;
162 I = new intnode;
163 I->i = nextRandom();
164 safeTop->insert(I, compareint);
165 }
166
168 safeTop->visit(SplayCheck::VisitNode);
169
170 safeTop->destroy(destint);
171 }
172 {
173 /* intnode */
174 const auto safeTop = new Splay<intnode>();
175
176 for (int i = 0; i < 100; ++i) {
177 intnode I;
178 I.i = nextRandom();
179 safeTop->insert(I, compareintref);
180 }
181
183 safeTop->visit(SplayCheck::VisitNodeRef);
184
185 safeTop->destroy(destintref);
186 }
187
188 /* check the check routine */
189 {
191 intnode I;
192 I.i = 1;
193 /* check we don't segfault on NULL splay calls */
195 I.i = 0;
198 }
199
200 {
201 /* check for begin() */
202 Splay<intnode> *safeTop = new Splay<intnode>();
203
204 if (safeTop->start() != nullptr)
205 exit(EXIT_FAILURE);
206
207 if (safeTop->finish() != nullptr)
208 exit(EXIT_FAILURE);
209
210 for (int i = 0; i < 100; ++i) {
211 intnode I;
212 I.i = nextRandom();
213
214 if (I.i > 50 && I.i < 10000000)
215 safeTop->insert(I, compareintref);
216 }
217
218 {
219 intnode I;
220 I.i = 50;
221 safeTop->insert (I, compareintref);
222 I.i = 10000000;
223 safeTop->insert (I, compareintref);
224 }
225
226 if (!safeTop->start())
227 exit(EXIT_FAILURE);
228
229 if (safeTop->start()->data.i != 50)
230 exit(EXIT_FAILURE);
231
232 if (!safeTop->finish())
233 exit(EXIT_FAILURE);
234
235 if (safeTop->finish()->data.i != 10000000)
236 exit(EXIT_FAILURE);
237
238 safeTop->destroy(destintref);
239 }
240
241 {
242 Splay<intnode *> aSplay;
243
244 if (aSplay.start() != nullptr)
245 exit(EXIT_FAILURE);
246
247 if (aSplay.size() != 0)
248 exit(EXIT_FAILURE);
249
250 aSplay.insert (new intnode(5), compareint);
251
252 if (aSplay.start() == nullptr)
253 exit(EXIT_FAILURE);
254
255 if (aSplay.size() != 1)
256 exit(EXIT_FAILURE);
257
258 aSplay.destroy(destint);
259
260 if (aSplay.start() != nullptr)
261 exit(EXIT_FAILURE);
262
263 if (aSplay.size() != 0)
264 exit(EXIT_FAILURE);
265 }
266
267 /* TODO: also test the other Splay API */
268
269 return EXIT_SUCCESS;
270}
271
static void BeginWalk()
Definition: splay.cc:67
static int LastValue
Definition: splay.cc:55
static void VisitNode(intnode *const &)
Definition: splay.cc:96
static void VisitNodeRef(intnode const &)
Definition: splay.cc:102
static void VisitVoid(void *const &)
Definition: splay.cc:73
static void CheckNode(intnode const &)
Definition: splay.cc:80
static bool ExpectedFail
Definition: splay.cc:56
Definition: splay.h:50
SplayNode< V > const * start() const
Definition: splay.h:345
void insert(Value const &, SPLAYCMP *compare)
Definition: splay.h:318
SplayNode< V > const * finish() const
Definition: splay.h:355
size_t size() const
Definition: splay.h:377
void destroy(SPLAYFREE *=DefaultFree)
Definition: splay.h:365
Definition: splay.cc:26
intnode(int anInt)
Definition: splay.cc:31
intnode()
Definition: splay.cc:29
int i
Definition: splay.cc:33
static uint32 A
Definition: md4.c:43
static uint32 B
Definition: md4.c:43
#define xfree
static int compareintref(intnode const &a, intnode const &b)
Definition: splay.cc:121
static int compareint(intnode *const &a, intnode *const &b)
Definition: splay.cc:45
int main(int, char *[])
Definition: splay.cc:131
static void destintref(intnode &)
Definition: splay.cc:127
static int compareintvoid(void *const &a, void *const &n)
Definition: splay.cc:37
static void destint(intnode *&data)
Definition: splay.cc:115
static void destintvoid(void *&data)
Definition: splay.cc:108
Definition: parse.c:104
void * xcalloc(size_t n, size_t sz)
Definition: xalloc.cc:71

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors