]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/splay.cc
2 * based on ftp://ftp.cs.cmu.edu/user/sleator/splaying/top-down-splay.c
3 * http://bobo.link.cs.cmu.edu/cgi-bin/splay/splay-cgi.pl
14 #define assert(X) {if (!(X)) exit (1);}
29 intnode (int anInt
) : i (anInt
) {}
35 compareintvoid(void * const &a
, void * const &n
)
37 intnode
*A
= (intnode
*)a
;
38 intnode
*B
= (intnode
*)n
;
43 compareint(intnode
* const &a
, intnode
* const &b
)
52 static void BeginWalk();
54 static bool ExpectedFail
;
55 static void WalkVoid(void *const &, void *);
56 static void WalkNode(intnode
*const &, void *);
57 static void WalkNodeRef(intnode
const &, void *);
58 static void CheckNode(intnode
const &);
61 int SplayCheck::LastValue (0);
62 bool SplayCheck::ExpectedFail (false);
65 SplayCheck::BeginWalk()
71 SplayCheck::WalkVoid(void *const &node
, void *state
)
73 intnode
*A
= (intnode
*)node
;
78 SplayCheck::CheckNode(intnode
const &A
)
80 if (LastValue
> A
.i
) {
94 SplayCheck::WalkNode (intnode
*const &a
, void *state
)
100 SplayCheck::WalkNodeRef (intnode
const &a
, void *state
)
106 destintvoid(void * &data
)
108 intnode
*i
= (intnode
*)data
;
113 destint(intnode
* &data
)
119 compareintref(intnode
const &a
, intnode
const &b
)
125 destintref (intnode
&)
129 main(int argc
, char *argv
[])
134 /* test void * splay containers */
135 splayNode
*top
= NULL
;
136 squid_srandom(time(NULL
));
138 for (i
= 0; i
< 100; ++i
) {
139 I
= (intnode
*)xcalloc(sizeof(intnode
), 1);
140 I
->i
= squid_random();
141 top
= top
->insert(I
, compareintvoid
);
144 SplayCheck::BeginWalk();
145 top
->walk(SplayCheck::WalkVoid
, NULL
);
147 SplayCheck::BeginWalk();
148 top
->walk(SplayCheck::WalkVoid
, NULL
);
149 top
->destroy(destintvoid
);
150 /* check we don't segfault on NULL splay calls */
152 top
->splay((void *)NULL
, compareintvoid
);
155 /* test typesafe splay containers */
158 SplayNode
<intnode
*> *safeTop
= NULL
;
160 for ( int i
= 0; i
< 100; ++i
) {
163 I
->i
= squid_random();
164 safeTop
= safeTop
->insert(I
, compareint
);
167 SplayCheck::BeginWalk();
168 safeTop
->walk(SplayCheck::WalkNode
, NULL
);
170 safeTop
->destroy(destint
);
171 /* check we don't segfault on NULL splay calls */
173 safeTop
->splay((intnode
*)NULL
, compareint
);
177 SplayNode
<intnode
> *safeTop
= NULL
;
179 for (int i
= 0; i
< 100; ++i
) {
181 I
.i
= squid_random();
182 safeTop
= safeTop
->insert(I
, compareintref
);
185 SplayCheck::BeginWalk();
186 safeTop
->walk(SplayCheck::WalkNodeRef
, NULL
);
188 safeTop
->destroy(destintref
);
189 /* check we don't segfault on NULL splay calls */
191 safeTop
->splay(intnode(), compareintref
);
192 SplayCheck::BeginWalk();
193 safeTop
->walk(SplayCheck::WalkNodeRef
, NULL
);
196 /* check the check routine */
198 SplayCheck::BeginWalk();
201 /* check we don't segfault on NULL splay calls */
202 SplayCheck::WalkNodeRef(I
, NULL
);
204 SplayCheck::ExpectedFail
= true;
205 SplayCheck::WalkNodeRef(I
, NULL
);
209 /* check for begin() */
210 SplayNode
<intnode
> *safeTop
= NULL
;
212 if (safeTop
->start() != NULL
)
215 if (safeTop
->finish() != NULL
)
218 for (int i
= 0; i
< 100; ++i
) {
220 I
.i
= squid_random();
222 if (I
.i
> 50 && I
.i
< 10000000)
223 safeTop
= safeTop
->insert(I
, compareintref
);
229 safeTop
= safeTop
->insert (I
, compareintref
);
231 safeTop
= safeTop
->insert (I
, compareintref
);
234 if (!safeTop
->start())
237 if (safeTop
->start()->data
.i
!= 50)
240 if (!safeTop
->finish())
243 if (safeTop
->finish()->data
.i
!= 10000000)
246 safeTop
->destroy(destintref
);
250 Splay
<intnode
*> aSplay
;
252 if (aSplay
.start() != NULL
)
255 if (aSplay
.size() != 0)
258 aSplay
.insert (new intnode(5), compareint
);
260 if (aSplay
.start() == NULL
)
263 if (aSplay
.size() != 1)
266 aSplay
.destroy(destint
);
268 if (aSplay
.start() != NULL
)
271 if (aSplay
.size() != 0)