2 * UCW Library -- Byte-based trie -- Testing utility
4 * (c) 2008 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include "lib/getopt.h"
19 #define TRIE_PREFIX(x) basic_##x
20 #define TRIE_NODE_TYPE char
21 #define TRIE_WANT_CLEANUP
23 #define TRIE_WANT_DELETE
24 #define TRIE_WANT_FIND
25 #define TRIE_WANT_AUDIT
37 if (!basic_find("str1") || !basic_find("str2") || basic_find("x") || basic_find("str123"))
41 if (basic_find("str1") || !basic_find("str2"))
47 #define TRIE_PREFIX(x) dynamic_##x
48 #define TRIE_NODE_TYPE char
51 #define TRIE_WANT_CLEANUP
53 #define TRIE_WANT_FIND
54 #define TRIE_WANT_AUDIT
63 struct dynamic_trie trie1, trie2;
66 dynamic_add(&trie1, "str1");
67 dynamic_add(&trie2, "str2");
68 if (!dynamic_find(&trie1, "str1") || dynamic_find(&trie1, "str2") || !dynamic_find(&trie2, "str2"))
70 dynamic_audit(&trie1);
71 dynamic_audit(&trie2);
72 dynamic_cleanup(&trie1);
73 dynamic_cleanup(&trie2);
77 #define TRIE_PREFIX(x) random_##x
78 #define TRIE_NODE_TYPE char
79 #define TRIE_LEN_TYPE u16
81 #define TRIE_WANT_CLEANUP
82 #define TRIE_WANT_FIND
84 #define TRIE_WANT_REMOVE
85 #define TRIE_WANT_AUDIT
91 #define MAX_STRINGS 200
94 static char *str[MAX_STRINGS];
99 uns l = random_max(11);
100 char *s = xmalloc(l + 1);
101 for (uns i = 0; i < l; i++)
102 s[i] = random_max('z' - 'a') + 'a';
108 gen_unique_string(void)
113 for (uns i = 0; i < count; i++)
114 if (!strcmp(s, str[i]))
125 if (count == MAX_STRINGS)
127 char *s = gen_unique_string();
139 uns i = random_max(count);
140 DBG("remove '%s'", str[i]);
141 random_remove(str[i]);
144 str[i] = str[--count];
150 if (!count || !random_max(4))
152 char *s = gen_unique_string();
153 DBG("negative find '%s'", s);
160 uns i = random_max(count);
161 DBG("positive find '%s'", str[i]);
162 if (random_find(str[i]) != str[i])
172 for (uns i = 0; i < count; i++)
183 for (uns i = 0; i < 10000; i++)
185 int r = random_max(1000);
188 else if ((r -= 150) < 0)
190 else if ((r -= 300) < 0)
192 else if ((r -= 1) < 0)
198 int main(int argc, char **argv)
201 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 || optind + 1 != argc)
202 die("Invalid usage, see the source code");
205 char *test = argv[optind];
206 if (!strcmp(test, "basic"))
208 else if (!strcmp(test, "dynamic"))
210 else if (!strcmp(test, "random"))
213 die("Unknown test case");