Debian Codegraph @

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

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * patriciatest2.c: More testing of the patricia tree.
4 *
5 * Copyright (c) 2008-2010 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 #define TESTSIZE 10000
37
38 int errors = 0;
39
40 void
41 str_canon(char *key)
42 {
43 return;
44 }
45
46 void
47 statscb(const char *line, void *data)
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 n2 = mowgli_patricia_size(dtree);
60 MOWGLI_PATRICIA_FOREACH(elem, &state, dtree)
61 {
62 elem2 = mowgli_patricia_retrieve(dtree, (const char *) elem);
63
64 if (elem2 == NULL)
65 {
66 errors++;
67 printf("failed to find element %s\n",
68 (const char *) elem);
69 }
70 else if (strcmp(elem2, elem))
71 {
72 printf("element %s != %s\n",
73 (const char *) elem,
74 (const char *) elem2);
75 errors++;
76 }
77
78 n1++;
79
80 if (n1 > n2 * 2)
81 break;
82 }
83
84 if (n1 != n2)
85 {
86 errors++;
87 printf("number of iterated elements %u != size %u\n", n1, n2);
88 }
89 }
90
91 void
92 test_patricia(void)
93 {
94 mowgli_patricia_t *dtree;
95 int i, j;
96 char buf[100], *strings[TESTSIZE];
97
98 srandom(12346);
99
100 for (i = 0; i < TESTSIZE; i++)
101 {
102 for (j = 0; j < 40; j++)
103 buf[j] = 'a' + random() % 26;
104
105 buf[20 + random() % 20] = '\0';
106 strings[i] = strdup(buf);
107 }
108
109 dtree = mowgli_patricia_create(str_canon);
110
111 for (i = 0; i < TESTSIZE; i++)
112 {
113 mowgli_patricia_add(dtree, strings[i], strings[i]);
114 check_all_retrievable(dtree);
115 }
116
117 check_all_retrievable(dtree);
118
119 for (i = 0; i < TESTSIZE / 2; i++)
120 {
121 mowgli_patricia_delete(dtree, strings[i]);
122
123 if (mowgli_patricia_retrieve(dtree, strings[i]))
124 {
125 printf("still retrievable after delete: %s\n",
126 strings[i]);
127 errors++;
128 }
129
130 check_all_retrievable(dtree);
131 }
132
133 for (i = 0; i < TESTSIZE / 2; i++)
134 {
135 mowgli_patricia_add(dtree, strings[i], strings[i]);
136 check_all_retrievable(dtree);
137 }
138
139 for (i = 0; i < TESTSIZE; i++)
140 {
141 mowgli_patricia_delete(dtree, strings[i]);
142
143 if (mowgli_patricia_retrieve(dtree, strings[i]))
144 {
145 printf("still retrievable after delete: %s\n",
146 strings[i]);
147 errors++;
148 }
149
150 check_all_retrievable(dtree);
151 }
152
153 mowgli_patricia_destroy(dtree, NULL, NULL);
154
155 for (i = 0; i < TESTSIZE; i++)
156 free(strings[i]);
157 }
158
159 int
160 main(int argc, char *argv[])
161 {
162 test_patricia();
163
164 return errors == 0 ? 0 : 1;
165 }