]>
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
30 intnode (int anInt
) : i (anInt
) {}
36 compareintvoid(void * const &a
, void * const &n
)
38 intnode
*A
= (intnode
*)a
;
39 intnode
*B
= (intnode
*)n
;
44 compareint(intnode
* const &a
, intnode
* const &b
)
53 static void BeginWalk();
55 static bool ExpectedFail
;
56 static void WalkVoid(void *const &, void *);
57 static void WalkNode(intnode
*const &, void *);
58 static void WalkNodeRef(intnode
const &, void *);
59 static void CheckNode(intnode
const &);
62 int SplayCheck::LastValue (0);
63 bool SplayCheck::ExpectedFail (false);
66 SplayCheck::BeginWalk()
72 SplayCheck::WalkVoid(void *const &node
, void *state
)
74 intnode
*A
= (intnode
*)node
;
79 SplayCheck::CheckNode(intnode
const &A
)
81 if (LastValue
> A
.i
) {
95 SplayCheck::WalkNode (intnode
*const &a
, void *state
)
101 SplayCheck::WalkNodeRef (intnode
const &a
, void *state
)
107 destintvoid(void * &data
)
109 intnode
*i
= (intnode
*)data
;
114 destint(intnode
* &data
)
120 compareintref(intnode
const &a
, intnode
const &b
)
126 destintref (intnode
&)
130 main(int argc
, char *argv
[])
135 /* test void * splay containers */
136 splayNode
*top
= NULL
;
137 squid_srandom(time(NULL
));
139 for (i
= 0; i
< 100; ++i
) {
140 I
= (intnode
*)xcalloc(sizeof(intnode
), 1);
141 I
->i
= squid_random();
143 top
= top
->insert(I
, compareintvoid
);
145 top
= new splayNode(static_cast<void*>(new intnode(101)));
148 SplayCheck::BeginWalk();
149 top
->walk(SplayCheck::WalkVoid
, NULL
);
151 SplayCheck::BeginWalk();
152 top
->walk(SplayCheck::WalkVoid
, NULL
);
153 top
->destroy(destintvoid
);
156 /* test typesafe splay containers */
159 SplayNode
<intnode
*> *safeTop
= new SplayNode
<intnode
*>(new intnode(101));
161 for ( int i
= 0; i
< 100; ++i
) {
164 I
->i
= squid_random();
165 safeTop
= safeTop
->insert(I
, compareint
);
168 SplayCheck::BeginWalk();
169 safeTop
->walk(SplayCheck::WalkNode
, NULL
);
171 safeTop
->destroy(destint
);
175 SplayNode
<intnode
> *safeTop
= new SplayNode
<intnode
>(101);
177 for (int i
= 0; i
< 100; ++i
) {
179 I
.i
= squid_random();
180 safeTop
= safeTop
->insert(I
, compareintref
);
183 SplayCheck::BeginWalk();
184 safeTop
->walk(SplayCheck::WalkNodeRef
, NULL
);
186 safeTop
->destroy(destintref
);
189 /* check the check routine */
191 SplayCheck::BeginWalk();
194 /* check we don't segfault on NULL splay calls */
195 SplayCheck::WalkNodeRef(I
, NULL
);
197 SplayCheck::ExpectedFail
= true;
198 SplayCheck::WalkNodeRef(I
, NULL
);
202 /* check for begin() */
203 Splay
<intnode
> *safeTop
= new Splay
<intnode
>();
205 if (safeTop
->start() != NULL
)
208 if (safeTop
->finish() != NULL
)
211 for (int i
= 0; i
< 100; ++i
) {
213 I
.i
= squid_random();
215 if (I
.i
> 50 && I
.i
< 10000000)
216 safeTop
->insert(I
, compareintref
);
222 safeTop
->insert (I
, compareintref
);
224 safeTop
->insert (I
, compareintref
);
227 if (!safeTop
->start())
230 if (safeTop
->start()->data
.i
!= 50)
233 if (!safeTop
->finish())
236 if (safeTop
->finish()->data
.i
!= 10000000)
239 safeTop
->destroy(destintref
);
243 Splay
<intnode
*> aSplay
;
245 if (aSplay
.start() != NULL
)
248 if (aSplay
.size() != 0)
251 aSplay
.insert (new intnode(5), compareint
);
253 if (aSplay
.start() == NULL
)
256 if (aSplay
.size() != 1)
259 aSplay
.destroy(destint
);
261 if (aSplay
.start() != NULL
)
264 if (aSplay
.size() != 0)
268 /* TODO: also test the other Splay API */