37 testitem_new (
const char* str)
39 TestItem
self = malloc (
sizeof *
self);
41 self->key = strdup (str);
46 testitem_delete (TestItem
self)
55 cmp_fn (
const void*,
const void*);
58 key_fn (
const PSplaynode);
61 delete_fn (PSplaynode);
67 fcmp_fn (
const void*,
const void*);
70 fkey_fn (
const PSplaynode);
73 fdelete_fn (PSplaynode);
80 testitem_print_node (PSplaynode node,
const enum psplay_order_enum which,
int level,
void* data)
83 static char* sp =
" ";
86 if (which == PSPLAY_PREORDER)
87 fprintf (fh,
"%s ...\n", sp);
94 fprintf (fh,
"%s%p '%s'\n", sp+40-level, node, ((TestItem)node)->key);
95 if (node->left) fprintf (fh,
"%sleft %p '%s'\n", sp+40-level, node->left, ((TestItem)node->left)->key);
98 if (node->right) fprintf (fh,
"%sright %p '%s'\n", sp+40-level, node->right, ((TestItem)node->right)->key);
100 case PSPLAY_POSTORDER:
108 testitem_dump (PSplay
self, FILE* dest)
110 fprintf (dest,
"root %p '%s'\n", self->tree, self->tree?((TestItem)self->tree)->key:
"EMPTY");
111 psplay_walk (
self, NULL, testitem_print_node, 0, dest);
112 fprintf (dest,
"\n");
116 testitem_graphvizprint_node (PSplaynode node,
const enum psplay_order_enum which,
int level,
void* data)
122 case PSPLAY_PREORDER:
124 fprintf (fh,
"\t\"%p:%s\":sw -> \"%p:%s\":ne;\n",
126 ((TestItem)node)->key,
128 ((TestItem)node->left)->key);
132 fprintf (fh,
"\t\"%p:%s\":se -> \"%p:%s\":nw;\n",
134 ((TestItem)node)->key,
136 ((TestItem)node->right)->key);
138 case PSPLAY_POSTORDER:
148 testitem_graphvizdump (PSplay
self, FILE* dest)
151 if (!cnt) cnt = (
time(NULL) % 1000) * 100;
154 sprintf(cmd,
"dot -Tps >/var/tmp/dbg%d.ps; gv /var/tmp/dbg%d.ps",cnt,cnt);
155 FILE * graph = popen(cmd,
"w");
157 fprintf(graph,
"digraph \"%s\" { center=true; size=\"6,6\"; node [color=lightblue2, style=filled];",
"psplay");
160 fprintf(graph,
"\t\"root\":s -> \"%p:%s\":n;\n",
161 self->tree, self->tree?((TestItem)self->tree)->key:
"EMPTY");
163 psplay_walk (
self, NULL, testitem_graphvizprint_node, 0, graph);
177 psplay_init (&splay_tree, cmp_fn, key_fn, delete_fn);
179 psplay_dump (&splay_tree, stdout);
185 TEST (
"basic_insert_dump")
190 psplay_init (&splay_tree, cmp_fn, key_fn, delete_fn);
192 int end = atoi (argv[2]);
196 for (
int i = 1; i <= end; ++i)
198 sprintf (key,
"%d", i);
199 ECHO (
"insert %s", key);
200 psplay_insert (&splay_tree, (PSplaynode)testitem_new (key), 100);
203 psplay_dump (&splay_tree, stderr);
206 for (
int i = 1; i <= end; ++i)
208 sprintf (key,
"%d", i);
209 ECHO (
"insert %s", key);
211 psplay_dump (&splay_tree, stderr);
213 for (
int i = end; i; --i)
215 sprintf (key,
"%d", i);
216 ECHO (
"insert %s", key);
218 psplay_dump (&splay_tree, stderr);
230 psplay_init (&splay_tree, cmp_fn, key_fn, delete_fn);
232 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"foo"), 100);
233 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"bar"), 100);
234 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"baz"), 100);
235 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"test"), 100);
236 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"pap"), 100);
237 psplay_insert (&splay_tree, (PSplaynode)testitem_new (
"qux"), 100);
239 testitem_graphvizdump (&splay_tree, stdout);
240 psplay_dump (&splay_tree, stdout);
243 TestItem f = (TestItem)
psplay_find (&splay_tree,
"baz", 100);
245 printf (
"found %p (%.4s)\n", &f->node, f->key);
246 psplay_dump (&splay_tree, stdout);
248 f = (TestItem)
psplay_find (&splay_tree,
"test", 100);
250 printf (
"found %p (%.4s)\n", &f->node, f->key);
251 psplay_dump (&splay_tree, stdout);
253 f = (TestItem)
psplay_find (&splay_tree,
"test", 100);
255 printf (
"found %p (%.4s)\n", &f->node, f->key);
256 psplay_dump (&splay_tree, stdout);
258 f = (TestItem)
psplay_find (&splay_tree,
"foo", 100);
260 printf (
"found %p (%.4s)\n", &f->node, f->key);
261 psplay_dump (&splay_tree, stdout);
265 psplay_dump (&splay_tree, stdout);
268 psplay_dump (&splay_tree, stdout);
271 psplay_dump (&splay_tree, stdout);
273 printf (
"destroying\n");
275 psplay_dump (&splay_tree, stdout);
278 psplay_dump (&splay_tree, stdout);
281 psplay_dump (&splay_tree, stdout);
284 psplay_dump (&splay_tree, stdout);
289 TEST (
"basic_insert_splay")
294 psplay_init (&splay_tree, cmp_fn, key_fn, delete_fn);
296 int end = atoi (argv[2]);
300 for (
int i = 1; i <= end; ++i)
302 sprintf (key,
"%d", i);
303 ECHO (
"insert %s", key);
304 psplay_insert (&splay_tree, (PSplaynode)testitem_new (key), 100);
307 for (
int i = end/2; i <= end; ++i)
309 psplay_dump (&splay_tree, stderr);
310 sprintf (key,
"%d", i);
318 TEST (
"basic_rand_insert_dump")
323 psplay_init (&splay_tree, cmp_fn, key_fn, delete_fn);
325 int end = atoi (argv[2]);
329 for (
int i = 1; i <= end; ++i)
331 sprintf (key,
"%d", i );
332 psplay_insert (&splay_tree, (PSplaynode)testitem_new (key), 100);
335 testitem_graphvizdump (&splay_tree, stdout);
348 psplay_init (&splay_tree, fcmp_fn, fkey_fn, fdelete_fn);
350 int end = atoi (argv[2]);
354 for (
int i = 1; i <= end; ++i)
356 sprintf (key,
"%d", i);
357 psplay_insert (&splay_tree, (PSplaynode)testitem_new (key), 100);
390 TEST (
"insert_fastcheck")
406 cmp_fn (
const void* a,
const void* b)
410 return strcmp (a, b);
414 key_fn (
const PSplaynode node)
417 CHECK (((TestItem)node)->key);
419 return ((TestItem)node)->key;
423 delete_fn (PSplaynode node)
426 testitem_delete ((TestItem) node);
433 fcmp_fn (
const void* a,
const void* b)
435 return strcmp (a, b);
439 fkey_fn (
const PSplaynode node)
441 return ((TestItem)node)->key;
445 fdelete_fn (PSplaynode node)
447 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.