]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/splay.cc
86f80edd988e958df975094bfcab9da11f7f7bf4
2 * Copyright (C) 1996-2025 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
31 intnode (int anInt
) : i (anInt
) {}
37 compareintvoid(void *const &a
, void *const &n
)
39 intnode
*A
= (intnode
*)a
;
40 intnode
*B
= (intnode
*)n
;
45 compareint(intnode
*const &a
, intnode
*const &b
)
54 static void BeginWalk();
56 static bool ExpectedFail
;
57 static void VisitVoid(void *const &);
58 static void VisitNode(intnode
*const &);
59 static void VisitNodeRef(intnode
const &);
60 static void CheckNode(intnode
const &);
63 int SplayCheck::LastValue (0);
64 bool SplayCheck::ExpectedFail (false);
67 SplayCheck::BeginWalk()
73 SplayCheck::VisitVoid(void *const &node
)
75 intnode
*A
= (intnode
*)node
;
80 SplayCheck::CheckNode(intnode
const &A
)
82 if (LastValue
> A
.i
) {
96 SplayCheck::VisitNode(intnode
*const &a
)
102 SplayCheck::VisitNodeRef(intnode
const &a
)
108 destintvoid(void *&data
)
110 intnode
*i
= (intnode
*)data
;
115 destint(intnode
*&data
)
121 compareintref(intnode
const &a
, intnode
const &b
)
127 destintref(intnode
&)
133 std::mt19937 generator
;
134 std::uniform_int_distribution
<int> distribution
;
135 auto nextRandom
= std::bind (distribution
, generator
);
138 /* test void * splay containers */
139 const auto top
= new Splay
<void *>();
141 for (int i
= 0; i
< 100; ++i
) {
142 intnode
*I
= (intnode
*)xcalloc(sizeof(intnode
), 1);
144 top
->insert(I
, compareintvoid
);
147 SplayCheck::BeginWalk();
148 top
->visit(SplayCheck::VisitVoid
);
150 SplayCheck::BeginWalk();
151 top
->visit(SplayCheck::VisitVoid
);
152 top
->destroy(destintvoid
);
155 /* test typesafe splay containers */
158 const auto safeTop
= new Splay
<intnode
*>();
160 for ( int i
= 0; i
< 100; ++i
) {
164 safeTop
->insert(I
, compareint
);
167 SplayCheck::BeginWalk();
168 safeTop
->visit(SplayCheck::VisitNode
);
170 safeTop
->destroy(destint
);
174 const auto safeTop
= new Splay
<intnode
>();
176 for (int i
= 0; i
< 100; ++i
) {
179 safeTop
->insert(I
, compareintref
);
182 SplayCheck::BeginWalk();
183 safeTop
->visit(SplayCheck::VisitNodeRef
);
185 safeTop
->destroy(destintref
);
188 /* check the check routine */
190 SplayCheck::BeginWalk();
193 /* check we don't segfault on NULL splay calls */
194 SplayCheck::VisitNodeRef(I
);
196 SplayCheck::ExpectedFail
= true;
197 SplayCheck::VisitNodeRef(I
);
201 /* check for begin() */
202 Splay
<intnode
> *safeTop
= new Splay
<intnode
>();
204 if (safeTop
->start() != nullptr)
207 if (safeTop
->finish() != nullptr)
210 for (int i
= 0; i
< 100; ++i
) {
214 if (I
.i
> 50 && I
.i
< 10000000)
215 safeTop
->insert(I
, compareintref
);
221 safeTop
->insert (I
, compareintref
);
223 safeTop
->insert (I
, compareintref
);
226 if (!safeTop
->start())
229 if (safeTop
->start()->data
.i
!= 50)
232 if (!safeTop
->finish())
235 if (safeTop
->finish()->data
.i
!= 10000000)
238 safeTop
->destroy(destintref
);
242 Splay
<intnode
*> aSplay
;
244 if (aSplay
.start() != nullptr)
247 if (aSplay
.size() != 0)
250 aSplay
.insert (new intnode(5), compareint
);
252 if (aSplay
.start() == nullptr)
255 if (aSplay
.size() != 1)
258 aSplay
.destroy(destint
);
260 if (aSplay
.start() != nullptr)
263 if (aSplay
.size() != 0)
267 /* TODO: also test the other Splay API */