46 testitem_new (
const char* str)
48 TestItem
self = malloc (
sizeof *
self);
50 self->key = strdup (str);
55 testitem_delete (TestItem
self)
64 cmp_fn (
const void*,
const void*);
67 key_fn (
const PSplaynode);
70 delete_fn (PSplaynode);
76 fcmp_fn (
const void*,
const void*);
79 fkey_fn (
const PSplaynode);
82 fdelete_fn (PSplaynode);
89 testitem_print_node (PSplaynode node,
const enum psplay_order_enum which,
int level,
void* data)
92 static char* sp =
" ";
95 if (which == PSPLAY_PREORDER)
96 fprintf (fh,
"%s ...\n", sp);
102 case PSPLAY_PREORDER:
103 fprintf (fh,
"%s%p '%s'\n", sp+40-level, node, ((TestItem)node)->key);
104 if (node->left) fprintf (fh,
"%sleft %p '%s'\n", sp+40-level, node->left, ((TestItem)node->left)->key);
107 if (node->right) fprintf (fh,
"%sright %p '%s'\n", sp+40-level, node->right, ((TestItem)node->right)->key);
109 case PSPLAY_POSTORDER:
117 testitem_dump (PSplay
self, FILE* dest)
119 fprintf (dest,
"root %p '%s'\n", self->tree, self->tree?((TestItem)self->tree)->key:
"EMPTY");
120 psplay_walk (
self, NULL, testitem_print_node, 0, dest);
121 fprintf (dest,
"\n");
125 testitem_graphvizprint_node (PSplaynode node,
const enum psplay_order_enum which,
int level,
void* data)
131 case PSPLAY_PREORDER:
133 fprintf (fh,
"\t\"%p:%s\":sw -> \"%p:%s\":ne;\n",
135 ((TestItem)node)->key,
137 ((TestItem)node->left)->key);
141 fprintf (fh,
"\t\"%p:%s\":se -> \"%p:%s\":nw;\n",
143 ((TestItem)node)->key,
145 ((TestItem)node->right)->key);
147 case PSPLAY_POSTORDER:
157 testitem_graphvizdump (PSplay
self, FILE* dest)
160 if (!cnt) cnt = (
time(NULL) % 1000) * 100;
163 sprintf(cmd,
"dot -Tps >/var/tmp/dbg%d.ps; gv /var/tmp/dbg%d.ps",cnt,cnt);
164 FILE * graph = popen(cmd,
"w");
166 fprintf(graph,
"digraph \"%s\" { center=true; size=\"6,6\"; node [color=lightblue2, style=filled];",
"psplay");
169 fprintf(graph,
"\t\"root\":s -> \"%p:%s\":n;\n",
170 self->tree, self->tree?((TestItem)self->tree)->key:
"EMPTY");
172 psplay_walk (
self, NULL, testitem_graphvizprint_node, 0, graph);
186 psplay_init (&splay_tree, cmp_fn, key_fn, delete_fn);
188 psplay_dump (&splay_tree, stdout);
194 TEST (
"basic_insert_dump")
199 psplay_init (&splay_tree, cmp_fn, key_fn, delete_fn);
201 int end = atoi (argv[2]);
205 for (
int i = 1; i <= end; ++i)
207 sprintf (key,
"%d", i);
208 ECHO (
"insert %s", key);
209 psplay_insert (&splay_tree, (PSplaynode)testitem_new (key), 100);
212 psplay_dump (&splay_tree, stderr);
215 for (
int i = 1; i <= end; ++i)
217 sprintf (key,
"%d", i);
218 ECHO (
"insert %s", key);
220 psplay_dump (&splay_tree, stderr);
222 for (
int i = end; i; --i)
224 sprintf (key,
"%d", i);
225 ECHO (
"insert %s", key);
227 psplay_dump (&splay_tree, stderr);
239 psplay_init (&splay_tree, cmp_fn, key_fn, delete_fn);
241 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"foo"), 100);
242 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"bar"), 100);
243 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"baz"), 100);
244 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"test"), 100);
245 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"pap"), 100);
246 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"qux"), 100);
248 testitem_graphvizdump (&splay_tree, stdout);
249 psplay_dump (&splay_tree, stdout);
252 TestItem f = (TestItem)
psplay_find (&splay_tree,
"baz", 100);
254 printf (
"found %p (%.4s)\n", &f->node, f->key);
255 psplay_dump (&splay_tree, stdout);
257 f = (TestItem)
psplay_find (&splay_tree,
"test", 100);
259 printf (
"found %p (%.4s)\n", &f->node, f->key);
260 psplay_dump (&splay_tree, stdout);
262 f = (TestItem)
psplay_find (&splay_tree,
"test", 100);
264 printf (
"found %p (%.4s)\n", &f->node, f->key);
265 psplay_dump (&splay_tree, stdout);
267 f = (TestItem)
psplay_find (&splay_tree,
"foo", 100);
269 printf (
"found %p (%.4s)\n", &f->node, f->key);
270 psplay_dump (&splay_tree, stdout);
274 psplay_dump (&splay_tree, stdout);
277 psplay_dump (&splay_tree, stdout);
280 psplay_dump (&splay_tree, stdout);
282 printf (
"destroying\n");
284 psplay_dump (&splay_tree, stdout);
287 psplay_dump (&splay_tree, stdout);
290 psplay_dump (&splay_tree, stdout);
293 psplay_dump (&splay_tree, stdout);
298 TEST (
"basic_insert_splay")
303 psplay_init (&splay_tree, cmp_fn, key_fn, delete_fn);
305 int end = atoi (argv[2]);
309 for (
int i = 1; i <= end; ++i)
311 sprintf (key,
"%d", i);
312 ECHO (
"insert %s", key);
313 psplay_insert (&splay_tree, (PSplaynode)testitem_new (key), 100);
316 for (
int i = end/2; i <= end; ++i)
318 psplay_dump (&splay_tree, stderr);
319 sprintf (key,
"%d", i);
327 TEST (
"basic_rand_insert_dump")
332 psplay_init (&splay_tree, cmp_fn, key_fn, delete_fn);
334 int end = atoi (argv[2]);
338 for (
int i = 1; i <= end; ++i)
340 sprintf (key,
"%d", i );
341 psplay_insert (&splay_tree, (PSplaynode)testitem_new (key), 100);
344 testitem_graphvizdump (&splay_tree, stdout);
357 psplay_init (&splay_tree, fcmp_fn, fkey_fn, fdelete_fn);
359 int end = atoi (argv[2]);
363 for (
int i = 1; i <= end; ++i)
365 sprintf (key,
"%d", i);
366 psplay_insert (&splay_tree, (PSplaynode)testitem_new (key), 100);
399 TEST (
"insert_fastcheck")
415 cmp_fn (
const void* a,
const void* b)
419 return strcmp (a, b);
423 key_fn (
const PSplaynode node)
426 CHECK (((TestItem)node)->key);
428 return ((TestItem)node)->key;
432 delete_fn (PSplaynode node)
435 testitem_delete ((TestItem) node);
442 fcmp_fn (
const void* a,
const void* b)
444 return strcmp (a, b);
448 fkey_fn (
const PSplaynode node)
450 return ((TestItem)node)->key;
454 fdelete_fn (PSplaynode node)
456 testitem_delete ((TestItem) node);
PSplay psplay_destroy(PSplay self)
Destroy a splay tree Frees all elements and associated resources of a splay tree. ...
Common functions for handling of time values.
Helpers and support macros for defining test executables in C.
PSplay psplay_init(PSplay self, psplay_cmp_fn cmp, psplay_key_fn key, psplay_delete_fn del)
Initialize a splay tree.
PSplaynode psplay_find(PSplay self, const void *key, int splayfactor)
Find a element in a splay tree.
PSplaynode psplay_insert(PSplay self, PSplaynode node, int splayfactor)
Insert a element into a splay tree.
void psplay_delete(PSplay self)
Delete a splay tree Frees all elements and associated resources of a splay tree and then itseld...
Probabilistic splay tree.
PSplaynode psplay_remove(PSplay self, PSplaynode node)
Remove a node from a splay tree.
int psplay_walk(PSplay self, PSplaynode node, psplay_action_fn action, int level, void *data)
Start a tree traversal.
PSplaynode psplay_remove_key(PSplay self, void *key)
Remove a node by key from a splay tree.
PSplaynode psplaynode_init(PSplaynode self)
Initialise a splay tree node The user has to place this nodes within his datastructure and must Initi...
void(* psplay_delete_fn)(PSplaynode node)
Destructor for user defined data Called when an element got removed from a splay tree.