2 * Copyright (C) 1996-2018 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 *)
74 intnode
*A
= (intnode
*)node
;
79 SplayCheck::CheckNode(intnode
const &A
)
81 if (LastValue
> A
.i
) {
95 SplayCheck::WalkNode (intnode
*const &a
, void *)
101 SplayCheck::WalkNodeRef (intnode
const &a
, void *)
107 destintvoid(void * &data
)
109 intnode
*i
= (intnode
*)data
;
114 destint(intnode
* &data
)
120 compareintref(intnode
const &a
, intnode
const &b
)
126 destintref (intnode
&)
132 std::mt19937 generator
;
133 xuniform_int_distribution
<int> distribution
;
134 auto nextRandom
= std::bind (distribution
, generator
);
137 /* test void * splay containers */
138 splayNode
*top
= NULL
;
140 for (int i
= 0; i
< 100; ++i
) {
141 intnode
*I
= (intnode
*)xcalloc(sizeof(intnode
), 1);
144 top
= top
->insert(I
, compareintvoid
);
146 top
= new splayNode(static_cast<void*>(new intnode(101)));
149 SplayCheck::BeginWalk();
150 top
->walk(SplayCheck::WalkVoid
, NULL
);
152 SplayCheck::BeginWalk();
153 top
->walk(SplayCheck::WalkVoid
, NULL
);
154 top
->destroy(destintvoid
);
157 /* test typesafe splay containers */
160 SplayNode
<intnode
*> *safeTop
= new SplayNode
<intnode
*>(new intnode(101));
162 for ( int i
= 0; i
< 100; ++i
) {
166 safeTop
= safeTop
->insert(I
, compareint
);
169 SplayCheck::BeginWalk();
170 safeTop
->walk(SplayCheck::WalkNode
, NULL
);
172 safeTop
->destroy(destint
);
176 SplayNode
<intnode
> *safeTop
= new SplayNode
<intnode
>(101);
178 for (int i
= 0; i
< 100; ++i
) {
181 safeTop
= safeTop
->insert(I
, compareintref
);
184 SplayCheck::BeginWalk();
185 safeTop
->walk(SplayCheck::WalkNodeRef
, NULL
);
187 safeTop
->destroy(destintref
);
190 /* check the check routine */
192 SplayCheck::BeginWalk();
195 /* check we don't segfault on NULL splay calls */
196 SplayCheck::WalkNodeRef(I
, NULL
);
198 SplayCheck::ExpectedFail
= true;
199 SplayCheck::WalkNodeRef(I
, NULL
);
203 /* check for begin() */
204 Splay
<intnode
> *safeTop
= new Splay
<intnode
>();
206 if (safeTop
->start() != NULL
)
209 if (safeTop
->finish() != NULL
)
212 for (int i
= 0; i
< 100; ++i
) {
216 if (I
.i
> 50 && I
.i
< 10000000)
217 safeTop
->insert(I
, compareintref
);
223 safeTop
->insert (I
, compareintref
);
225 safeTop
->insert (I
, compareintref
);
228 if (!safeTop
->start())
231 if (safeTop
->start()->data
.i
!= 50)
234 if (!safeTop
->finish())
237 if (safeTop
->finish()->data
.i
!= 10000000)
240 safeTop
->destroy(destintref
);
244 Splay
<intnode
*> aSplay
;
246 if (aSplay
.start() != NULL
)
249 if (aSplay
.size() != 0)
252 aSplay
.insert (new intnode(5), compareint
);
254 if (aSplay
.start() == NULL
)
257 if (aSplay
.size() != 1)
260 aSplay
.destroy(destint
);
262 if (aSplay
.start() != NULL
)
265 if (aSplay
.size() != 0)
269 /* TODO: also test the other Splay API */