]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/splay.cc
2 * $Id: splay.cc,v 1.8 2006/04/25 10:40:29 serassio Exp $
4 * based on ftp://ftp.cs.cmu.edu/user/sleator/splaying/top-down-splay.c
5 * http://bobo.link.cs.cmu.edu/cgi-bin/splay/splay-cgi.pl
21 #define assert(X) {if (!(X)) exit (1);}
36 intnode (int anInt
) : i (anInt
) {}
42 compareintvoid(void * const &a
, void * const &n
)
44 intnode
*A
= (intnode
*)a
;
45 intnode
*B
= (intnode
*)n
;
50 compareint(intnode
* const &a
, intnode
* const &b
)
59 static void BeginWalk();
61 static bool ExpectedFail
;
62 static void WalkVoid(void *const &, void *);
63 static void WalkNode(intnode
*const &, void *);
64 static void WalkNodeRef(intnode
const &, void *);
65 static void CheckNode(intnode
const &);
68 int SplayCheck::LastValue (0);
69 bool SplayCheck::ExpectedFail (false);
72 SplayCheck::BeginWalk()
78 SplayCheck::WalkVoid(void *const &node
, void *state
)
80 intnode
*A
= (intnode
*)node
;
85 SplayCheck::CheckNode(intnode
const &A
)
87 if (LastValue
> A
.i
) {
101 SplayCheck::WalkNode (intnode
*const &a
, void *state
)
107 SplayCheck::WalkNodeRef (intnode
const &a
, void *state
)
113 destintvoid(void * &data
)
115 intnode
*i
= (intnode
*)data
;
120 destint(intnode
* &data
)
126 compareintref(intnode
const &a
, intnode
const &b
)
132 destintref (intnode
&)
136 main(int argc
, char *argv
[])
141 /* test void * splay containers */
142 splayNode
*top
= NULL
;
143 squid_srandom(time(NULL
));
145 for (i
= 0; i
< 100; i
++) {
146 I
= (intnode
*)xcalloc(sizeof(intnode
), 1);
147 I
->i
= squid_random();
148 top
= top
->insert(I
, compareintvoid
);
151 SplayCheck::BeginWalk();
152 top
->walk(SplayCheck::WalkVoid
, NULL
);
154 SplayCheck::BeginWalk();
155 top
->walk(SplayCheck::WalkVoid
, NULL
);
156 top
->destroy(destintvoid
);
157 /* check we don't segfault on NULL splay calls */
159 top
->splay((void *)NULL
, compareintvoid
);
162 /* test typesafe splay containers */
165 SplayNode
<intnode
*> *safeTop
= NULL
;
167 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
++)
190 I
.i
= squid_random();
191 safeTop
= safeTop
->insert(I
, compareintref
);
194 SplayCheck::BeginWalk();
195 safeTop
->walk(SplayCheck::WalkNodeRef
, NULL
);
197 safeTop
->destroy(destintref
);
198 /* check we don't segfault on NULL splay calls */
200 safeTop
->splay(intnode(), compareintref
);
201 SplayCheck::BeginWalk();
202 safeTop
->walk(SplayCheck::WalkNodeRef
, NULL
);
204 /* check the check routine */
205 SplayCheck::BeginWalk();
208 /* check we don't segfault on NULL splay calls */
209 SplayCheck::WalkNodeRef(I
, NULL
);
211 SplayCheck::ExpectedFail
= true;
212 SplayCheck::WalkNodeRef(I
, NULL
);
215 /* check for begin() */
216 SplayNode
<intnode
> *safeTop
= NULL
;
218 if (safeTop
->start() != NULL
)
221 if (safeTop
->finish() != NULL
)
224 for (int i
= 0; i
< 100; i
++) {
226 I
.i
= squid_random();
228 if (I
.i
> 50 && I
.i
< 10000000)
229 safeTop
= safeTop
->insert(I
, compareintref
);
235 safeTop
= safeTop
->insert (I
, compareintref
);
237 safeTop
= safeTop
->insert (I
, compareintref
);
240 if (!safeTop
->start())
243 if (safeTop
->start()->data
.i
!= 50)
246 if (!safeTop
->finish())
249 if (safeTop
->finish()->data
.i
!= 10000000)
252 safeTop
->destroy(destintref
);
256 Splay
<intnode
*> aSplay
;
258 if (aSplay
.start() != NULL
)
261 if (aSplay
.size() != 0)
264 aSplay
.insert (new intnode(5), compareint
);
266 if (aSplay
.start() == NULL
)
269 if (aSplay
.size() != 1)
272 aSplay
.destroy(destint
);
274 if (aSplay
.start() != NULL
)
277 if (aSplay
.size() != 0)