4 * (c) 2006, Pavel Charvat <pchar@ucw.cz>
8 #include "ucw/mempool.h"
12 #define TRACE(x...) do{log(L_DEBUG, x);}while(0)
14 #define TRACE(x...) do{}while(0)
17 /* TEST1 - multiple searches */
19 #define KMP_PREFIX(x) kmp1_##x
20 #define KMP_WANT_CLEANUP
22 #define KMPS_PREFIX(x) kmp1s1_##x
23 #define KMPS_KMP_PREFIX(x) kmp1_##x
24 #define KMPS_WANT_BEST
25 #define KMPS_EXIT(kmp,src,s) TRACE("Best match has %d characters", s->best->len)
26 #include "ucw/kmp-search.h"
27 #define KMPS_PREFIX(x) kmp1s2_##x
28 #define KMPS_KMP_PREFIX(x) kmp1_##x
29 #define KMPS_VARS uns count;
30 #define KMPS_INIT(kmp,src,s) s->u.count = 0
31 #define KMPS_FOUND(kmp,src,s) s->u.count++
32 #include "ucw/kmp-search.h"
37 TRACE("Running test1");
38 struct kmp1_struct kmp;
40 kmp1_add(&kmp, "ahoj");
41 kmp1_add(&kmp, "hoj");
42 kmp1_add(&kmp, "aho");
44 struct kmp1s1_search s1;
45 kmp1s1_search(&kmp, &s1, "asjlahslhalahosjkjhojsas");
46 ASSERT(s1.best->len == 3);
47 struct kmp1s2_search s2;
48 kmp1s2_search(&kmp, &s2, "asjlahslhalahojsjkjhojsas");
49 ASSERT(s2.u.count == 4);
53 /* TEST2 - various tracing */
55 #define KMP_PREFIX(x) kmp2_##x
59 #define KMP_STATE_VARS char *str; uns id;
60 #define KMP_ADD_EXTRA_ARGS uns id
61 #define KMP_VARS char *start;
62 #define KMP_ADD_INIT(kmp,src) kmp->u.start = src
63 #define KMP_ADD_NEW(kmp,src,s) do{ TRACE("Inserting string %s with id %d", kmp->u.start, id); \
64 s->u.str = kmp->u.start; s->u.id = id; }while(0)
65 #define KMP_ADD_DUP(kmp,src,s) TRACE("String %s already inserted", kmp->u.start)
66 #define KMP_WANT_CLEANUP
67 #define KMP_WANT_SEARCH
68 #define KMPS_ADD_CONTROLS
69 #define KMPS_MERGE_CONTROLS
70 #define KMPS_FOUND(kmp,src,s) TRACE("String %s with id %d found", s->out->u.str, s->out->u.id)
71 #define KMPS_STEP(kmp,src,s) TRACE("Got to state %p after reading %d", s->s, s->c)
77 TRACE("Running test2");
78 struct kmp2_struct kmp;
80 kmp2_add(&kmp, "ahoj", 1);
81 kmp2_add(&kmp, "ahoj", 2);
82 kmp2_add(&kmp, "hoj", 3);
83 kmp2_add(&kmp, "aho", 4);
84 kmp2_add(&kmp, "aba", 5);
85 kmp2_add(&kmp, "aba", 5);
86 kmp2_add(&kmp, "pěl", 5);
88 kmp2_run(&kmp, "Šíleně žluťoučký kůň úpěl ďábelské ódy labababaks sdahojdhsaladsjhla");
92 /* TEST3 - random tests */
94 #define KMP_PREFIX(x) kmp3_##x
95 #define KMP_STATE_VARS uns index;
96 #define KMP_ADD_EXTRA_ARGS uns index
97 #define KMP_VARS char *start;
98 #define KMP_ADD_INIT(kmp,src) kmp->u.start = src
99 #define KMP_ADD_NEW(kmp,src,s) s->u.index = index
100 #define KMP_ADD_DUP(kmp,src,s) *(kmp->u.start) = 0
101 #define KMP_WANT_CLEANUP
102 #define KMP_WANT_SEARCH
103 #define KMPS_VARS uns sum, *cnt;
104 #define KMPS_FOUND(kmp,src,s) do{ ASSERT(s->u.cnt[s->out->u.index]); s->u.cnt[s->out->u.index]--; s->u.sum--; }while(0)
110 TRACE("Running test3");
111 struct mempool *pool = mp_new(1024);
112 for (uns testn = 0; testn < 100; testn++)
115 uns n = random_max(100);
117 struct kmp3_struct kmp;
119 for (uns i = 0; i < n; i++)
121 uns m = random_max(10);
122 s[i] = mp_alloc(pool, m + 1);
123 for (uns j = 0; j < m; j++)
124 s[i][j] = 'a' + random_max(3);
126 kmp3_add(&kmp, s[i], i);
129 for (uns i = 0; i < 10; i++)
131 uns m = random_max(100);
133 for (uns j = 0; j < m; j++)
134 b[j] = 'a' + random_max(4);
137 struct kmp3_search search;
140 for (uns j = 0; j < n; j++)
144 for (uns k = 0; k < m; k++)
145 if (!strncmp(b + k, s[j], strlen(s[j])))
146 cnt[j]++, search.u.sum++;
148 kmp3_search(&kmp, &search, b);
149 ASSERT(search.u.sum == 0);
156 /* TEST4 - user-defined character type */
162 kmp4_eq(struct kmp4_struct *kmp UNUSED, byte *a, byte *b)
164 return (a == b) || (a && b && *a == *b);
168 kmp4_hash(struct kmp4_struct *kmp UNUSED, struct kmp4_state *s, byte *c)
170 return (c ? (*c << 16) : 0) + (uns)(uintptr_t)s;
173 #define KMP_PREFIX(x) kmp4_##x
174 #define KMP_CHAR byte *
175 #define KMP_CONTROL_CHAR NULL
176 #define KMP_GET_CHAR(kmp,src,c) ({ c = src++; !!*c; })
177 #define KMP_GIVE_HASHFN
179 #define KMP_WANT_CLEANUP
180 #define KMP_WANT_SEARCH
181 #define KMPS_FOUND(kmp,src,s) TRACE("found")
182 #define KMPS_ADD_CONTROLS
183 #define KMPS_MERGE_CONTROLS
189 TRACE("Running test4");
190 struct kmp4_struct kmp;
192 kmp4_add(&kmp, "ahoj");
194 kmp4_run(&kmp, "djdhaskjdahoahaahojojshdaksjahdahojskj");