Debian Codegraph @

libmowgli-2/src/examples/patriciatest/patriciatest.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * patriciatest.c: Testing of the patricia tree.
4 *
5 * Copyright (c) 2008 Jilles Tjoelker
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <mowgli.h>
35
36 int errors = 0;
37
38 void
39 str_canon(char *key)
40 {
41 return;
42 }
43
44 void
45 statscb(const char *line, void *data)
46 {
47 printf("%s\n", line);
48 }
49
50 /* assumes data is key */
51 static void
52 check_all_retrievable(mowgli_patricia_t *dtree)
53 {
54 mowgli_patricia_iteration_state_t state;
55 void *elem, *elem2;
56 unsigned int n1 = 0, n2;
57
58 mowgli_patricia_stats(dtree, statscb, NULL);
59 printf("Checking consistency...");
60 fflush(stdout);
61 n2 = mowgli_patricia_size(dtree);
62 MOWGLI_PATRICIA_FOREACH(elem, &state, dtree)
63 {
64 elem2 = mowgli_patricia_retrieve(dtree, (const char *) elem);
65
66 if (elem2 == NULL)
67 {
68 errors++;
69 printf("failed to find element %s\n",
70 (const char *) elem);
71 }
72 else if (strcmp(elem2, elem))
73 {
74 printf("element %s != %s\n",
75 (const char *) elem,
76 (const char *) elem2);
77 errors++;
78 }
79 else
80 {
81 printf(".");
82 }
83
84 fflush(stdout);
85 n1++;
86
87 if (n1 > n2 * 2)
88 break;
89 }
90
91 if (n1 != n2)
92 {
93 errors++;
94 printf("number of iterated elements %u != size %u\n", n1, n2);
95 }
96
97 printf("\n");
98 fflush(stdout);
99 }
100
101 void
102 test_patricia(void)
103 {
104 mowgli_patricia_t *dtree;
105 mowgli_patricia_iteration_state_t state;
106 void *elem;
107
108 dtree = mowgli_patricia_create(str_canon);
109 #define ADD(x) printf("Adding %s\n", x); mowgli_patricia_add(dtree, x, x); check_all_retrievable(dtree)
110 ADD("\1\1");
111 ADD("alias");
112 ADD("\377");
113 ADD("\377\377\377");
114 ADD("foo");
115 ADD("bar");
116 ADD("baz");
117 ADD("splork");
118 ADD("rabbit");
119 ADD("\1");
120 ADD("meow");
121 ADD("hi");
122 ADD("konnichiwa");
123 ADD("absolutely");
124 ADD("cat");
125 ADD("dog");
126 ADD("woof");
127 ADD("moon");
128 ADD("new");
129 ADD("delete");
130
131 MOWGLI_PATRICIA_FOREACH(elem, &state, dtree)
132 {
133 printf("element -> %s\n", (const char *) elem);
134 }
135 printf("End of elements\n");
136 mowgli_patricia_stats(dtree, statscb, NULL);
137
138 check_all_retrievable(dtree);
139
140 #define TESTRETRIEVE(x) elem = mowgli_patricia_retrieve(dtree, x); printf("element %s: %s\n", x, elem ? (errors++, "YES") : "NO")
141 TESTRETRIEVE("meows");
142 TESTRETRIEVE("meo");
143 TESTRETRIEVE("deletes");
144 TESTRETRIEVE("z");
145 TESTRETRIEVE("0");
146
147 #define TESTDELETE(x) mowgli_patricia_delete(dtree, x); elem = mowgli_patricia_retrieve(dtree, x); printf("deleting %s: %s\n", x, elem ? (errors++, "STILL PRESENT") : "GONE"); check_all_retrievable(dtree)
148 TESTDELETE("YYY");
149 TESTDELETE("foo");
150 TESTDELETE("splork");
151 ADD("spork");
152 ADD("foo");
153 TESTDELETE("absolutely");
154 TESTDELETE("cat");
155 ADD("absolutely");
156 TESTDELETE("dog");
157
158 mowgli_patricia_destroy(dtree, NULL, NULL);
159 }
160
161 int
162 main(int argc, char *argv[])
163 {
164 test_patricia();
165
166 return errors == 0 ? 0 : 1;
167 }