]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/splay.cc
2 * Copyright (C) 1996-2014 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.
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
22 #define assert(X) {if (!(X)) exit (1);}
37 intnode (int anInt
) : i (anInt
) {}
43 compareintvoid(void * const &a
, void * const &n
)
45 intnode
*A
= (intnode
*)a
;
46 intnode
*B
= (intnode
*)n
;
51 compareint(intnode
* const &a
, intnode
* const &b
)
60 static void BeginWalk();
62 static bool ExpectedFail
;
63 static void WalkVoid(void *const &, void *);
64 static void WalkNode(intnode
*const &, void *);
65 static void WalkNodeRef(intnode
const &, void *);
66 static void CheckNode(intnode
const &);
69 int SplayCheck::LastValue (0);
70 bool SplayCheck::ExpectedFail (false);
73 SplayCheck::BeginWalk()
79 SplayCheck::WalkVoid(void *const &node
, void *state
)
81 intnode
*A
= (intnode
*)node
;
86 SplayCheck::CheckNode(intnode
const &A
)
88 if (LastValue
> A
.i
) {
102 SplayCheck::WalkNode (intnode
*const &a
, void *state
)
108 SplayCheck::WalkNodeRef (intnode
const &a
, void *state
)
114 destintvoid(void * &data
)
116 intnode
*i
= (intnode
*)data
;
121 destint(intnode
* &data
)
127 compareintref(intnode
const &a
, intnode
const &b
)
133 destintref (intnode
&)
137 main(int argc
, char *argv
[])
142 /* test void * splay containers */
143 splayNode
*top
= NULL
;
144 squid_srandom(time(NULL
));
146 for (i
= 0; i
< 100; ++i
) {
147 I
= (intnode
*)xcalloc(sizeof(intnode
), 1);
148 I
->i
= squid_random();
149 top
= top
->insert(I
, compareintvoid
);
152 SplayCheck::BeginWalk();
153 top
->walk(SplayCheck::WalkVoid
, NULL
);
155 SplayCheck::BeginWalk();
156 top
->walk(SplayCheck::WalkVoid
, NULL
);
157 top
->destroy(destintvoid
);
158 /* check we don't segfault on NULL splay calls */
160 top
->splay((void *)NULL
, compareintvoid
);
163 /* test typesafe splay containers */
166 SplayNode
<intnode
*> *safeTop
= NULL
;
168 for ( int i
= 0; i
< 100; ++i
) {
171 I
->i
= squid_random();
172 safeTop
= safeTop
->insert(I
, compareint
);
175 SplayCheck::BeginWalk();
176 safeTop
->walk(SplayCheck::WalkNode
, NULL
);
178 safeTop
->destroy(destint
);
179 /* check we don't segfault on NULL splay calls */
181 safeTop
->splay((intnode
*)NULL
, compareint
);
185 SplayNode
<intnode
> *safeTop
= NULL
;
187 for (int i
= 0; i
< 100; ++i
) {
189 I
.i
= squid_random();
190 safeTop
= safeTop
->insert(I
, compareintref
);
193 SplayCheck::BeginWalk();
194 safeTop
->walk(SplayCheck::WalkNodeRef
, NULL
);
196 safeTop
->destroy(destintref
);
197 /* check we don't segfault on NULL splay calls */
199 safeTop
->splay(intnode(), compareintref
);
200 SplayCheck::BeginWalk();
201 safeTop
->walk(SplayCheck::WalkNodeRef
, NULL
);
204 /* check the check routine */
206 SplayCheck::BeginWalk();
209 /* check we don't segfault on NULL splay calls */
210 SplayCheck::WalkNodeRef(I
, NULL
);
212 SplayCheck::ExpectedFail
= true;
213 SplayCheck::WalkNodeRef(I
, NULL
);
217 /* check for begin() */
218 SplayNode
<intnode
> *safeTop
= NULL
;
220 if (safeTop
->start() != NULL
)
223 if (safeTop
->finish() != NULL
)
226 for (int i
= 0; i
< 100; ++i
) {
228 I
.i
= squid_random();
230 if (I
.i
> 50 && I
.i
< 10000000)
231 safeTop
= safeTop
->insert(I
, compareintref
);
237 safeTop
= safeTop
->insert (I
, compareintref
);
239 safeTop
= safeTop
->insert (I
, compareintref
);
242 if (!safeTop
->start())
245 if (safeTop
->start()->data
.i
!= 50)
248 if (!safeTop
->finish())
251 if (safeTop
->finish()->data
.i
!= 10000000)
254 safeTop
->destroy(destintref
);
258 Splay
<intnode
*> aSplay
;
260 if (aSplay
.start() != NULL
)
263 if (aSplay
.size() != 0)
266 aSplay
.insert (new intnode(5), compareint
);
268 if (aSplay
.start() == NULL
)
271 if (aSplay
.size() != 1)
274 aSplay
.destroy(destint
);
276 if (aSplay
.start() != NULL
)
279 if (aSplay
.size() != 0)