1 | #include "owl.h" |
---|
2 | #include <unistd.h> |
---|
3 | #include <stdlib.h> |
---|
4 | |
---|
5 | owl_global g; |
---|
6 | |
---|
7 | int numtests; |
---|
8 | |
---|
9 | int owl_regtest(void); |
---|
10 | int owl_util_regtest(void); |
---|
11 | int owl_dict_regtest(void); |
---|
12 | int owl_variable_regtest(void); |
---|
13 | int owl_filter_regtest(void); |
---|
14 | int owl_obarray_regtest(void); |
---|
15 | int owl_editwin_regtest(void); |
---|
16 | |
---|
17 | int main(int argc, char **argv, char **env) |
---|
18 | { |
---|
19 | /* initialize a fake ncurses, detached from std{in,out} */ |
---|
20 | FILE *rnull = fopen("/dev/null", "r"); |
---|
21 | FILE *wnull = fopen("/dev/null", "w"); |
---|
22 | char *perlerr; |
---|
23 | int status = 0; |
---|
24 | newterm("xterm", wnull, rnull); |
---|
25 | /* initialize global structures */ |
---|
26 | owl_global_init(&g); |
---|
27 | |
---|
28 | perlerr = owl_perlconfig_initperl(NULL, &argc, &argv, &env); |
---|
29 | if (perlerr) { |
---|
30 | endwin(); |
---|
31 | fprintf(stderr, "Internal perl error: %s\n", perlerr); |
---|
32 | status = 1; |
---|
33 | goto out; |
---|
34 | } |
---|
35 | owl_global_complete_setup(&g); |
---|
36 | |
---|
37 | status = owl_regtest(); |
---|
38 | |
---|
39 | out: |
---|
40 | /* probably not necessary, but tear down the screen */ |
---|
41 | endwin(); |
---|
42 | fclose(rnull); |
---|
43 | fclose(wnull); |
---|
44 | return status; |
---|
45 | } |
---|
46 | |
---|
47 | int owl_regtest(void) { |
---|
48 | numtests = 0; |
---|
49 | int numfailures=0; |
---|
50 | /* |
---|
51 | printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS |
---|
52 | +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS); |
---|
53 | */ |
---|
54 | numfailures += owl_util_regtest(); |
---|
55 | numfailures += owl_dict_regtest(); |
---|
56 | numfailures += owl_variable_regtest(); |
---|
57 | numfailures += owl_filter_regtest(); |
---|
58 | numfailures += owl_obarray_regtest(); |
---|
59 | numfailures += owl_editwin_regtest(); |
---|
60 | if (numfailures) { |
---|
61 | fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures); |
---|
62 | } |
---|
63 | printf("1..%d\n", numtests); |
---|
64 | |
---|
65 | return(numfailures); |
---|
66 | } |
---|
67 | |
---|
68 | #define FAIL_UNLESS(desc,pred) do { int __pred = (pred); \ |
---|
69 | numtests++; \ |
---|
70 | printf("%s %s", (__pred)?"ok":(numfailed++,"not ok"), desc); \ |
---|
71 | if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0) |
---|
72 | |
---|
73 | |
---|
74 | int owl_util_regtest(void) |
---|
75 | { |
---|
76 | int numfailed=0; |
---|
77 | |
---|
78 | printf("# BEGIN testing owl_util\n"); |
---|
79 | |
---|
80 | FAIL_UNLESS("owl_util_substitute 1", |
---|
81 | !strcmp("foo", owl_text_substitute("foo", "", "Y"))); |
---|
82 | FAIL_UNLESS("owl_text_substitute 2", |
---|
83 | !strcmp("fYZYZ", owl_text_substitute("foo", "o", "YZ"))); |
---|
84 | FAIL_UNLESS("owl_text_substitute 3", |
---|
85 | !strcmp("foo", owl_text_substitute("fYZYZ", "YZ", "o"))); |
---|
86 | FAIL_UNLESS("owl_text_substitute 4", |
---|
87 | !strcmp("/u/foo/meep", owl_text_substitute("~/meep", "~", "/u/foo"))); |
---|
88 | |
---|
89 | FAIL_UNLESS("skiptokens 1", |
---|
90 | !strcmp("bar quux", skiptokens("foo bar quux", 1))); |
---|
91 | FAIL_UNLESS("skiptokens 2", |
---|
92 | !strcmp("meep", skiptokens("foo 'bar quux' meep", 2))); |
---|
93 | |
---|
94 | FAIL_UNLESS("expand_tabs 1", |
---|
95 | !strcmp(" hi", owl_text_expand_tabs("\thi"))); |
---|
96 | |
---|
97 | FAIL_UNLESS("expand_tabs 2", |
---|
98 | !strcmp(" hi\nword tab", owl_text_expand_tabs("\thi\nword\ttab"))); |
---|
99 | |
---|
100 | FAIL_UNLESS("expand_tabs 3", |
---|
101 | !strcmp(" 2 tabs", owl_text_expand_tabs("\t\t2 tabs"))); |
---|
102 | FAIL_UNLESS("expand_tabs 4", |
---|
103 | !strcmp("α ααααααα! ", owl_text_expand_tabs("α\tααααααα!\t"))); |
---|
104 | FAIL_UNLESS("expand_tabs 5", |
---|
105 | !strcmp("A AAA!! ", owl_text_expand_tabs("A\tAAA!!\t"))); |
---|
106 | |
---|
107 | FAIL_UNLESS("skiptokens 1", |
---|
108 | !strcmp("world", skiptokens("hello world", 1))); |
---|
109 | |
---|
110 | FAIL_UNLESS("skiptokens 2", |
---|
111 | !strcmp("c d e", skiptokens("a b c d e", 2))); |
---|
112 | |
---|
113 | FAIL_UNLESS("skiptokens 3", |
---|
114 | !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1))); |
---|
115 | |
---|
116 | FAIL_UNLESS("skiptokens 4", |
---|
117 | !strcmp("c d e", skiptokens("a \"b\" c d e", 2))); |
---|
118 | |
---|
119 | FAIL_UNLESS("skiptokens 5", |
---|
120 | !strcmp("c d e", skiptokens("a \"'\" c d e", 2))); |
---|
121 | |
---|
122 | /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */ |
---|
123 | printf("# END testing owl_util (%d failures)\n", numfailed); |
---|
124 | return(numfailed); |
---|
125 | } |
---|
126 | |
---|
127 | int owl_dict_regtest(void) { |
---|
128 | owl_dict d; |
---|
129 | owl_list l; |
---|
130 | int numfailed=0; |
---|
131 | char *av="aval", *bv="bval", *cv="cval", *dv="dval"; |
---|
132 | |
---|
133 | printf("# BEGIN testing owl_dict\n"); |
---|
134 | FAIL_UNLESS("create", 0==owl_dict_create(&d)); |
---|
135 | FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete)); |
---|
136 | FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete)); |
---|
137 | FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete)); |
---|
138 | FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete)); |
---|
139 | FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0)); |
---|
140 | FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a")); |
---|
141 | FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b")); |
---|
142 | FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c")); |
---|
143 | FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d")); |
---|
144 | FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e")); |
---|
145 | FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d")); |
---|
146 | FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d")); |
---|
147 | |
---|
148 | FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d)); |
---|
149 | FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l)); |
---|
150 | FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l)); |
---|
151 | |
---|
152 | /* these assume the returned keys are sorted */ |
---|
153 | FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0))); |
---|
154 | FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1))); |
---|
155 | FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2))); |
---|
156 | |
---|
157 | owl_list_cleanup(&l, owl_free); |
---|
158 | owl_dict_cleanup(&d, NULL); |
---|
159 | |
---|
160 | /* if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */ |
---|
161 | printf("# END testing owl_dict (%d failures)\n", numfailed); |
---|
162 | return(numfailed); |
---|
163 | } |
---|
164 | |
---|
165 | int owl_variable_regtest(void) { |
---|
166 | owl_vardict vd; |
---|
167 | int numfailed=0; |
---|
168 | char buf[1024]; |
---|
169 | const void *v; |
---|
170 | |
---|
171 | printf("# BEGIN testing owl_variable\n"); |
---|
172 | FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd)); |
---|
173 | |
---|
174 | FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping")); |
---|
175 | FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble")); |
---|
176 | FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"rxping", buf, 1024)); |
---|
177 | FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off")); |
---|
178 | FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping")); |
---|
179 | FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping")); |
---|
180 | FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0)); |
---|
181 | FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping")); |
---|
182 | FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0)); |
---|
183 | FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping")); |
---|
184 | |
---|
185 | |
---|
186 | FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath"))); |
---|
187 | FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee")); |
---|
188 | FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath"))); |
---|
189 | |
---|
190 | FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize")); |
---|
191 | FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble")); |
---|
192 | FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024)); |
---|
193 | FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8")); |
---|
194 | FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12)); |
---|
195 | FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize")); |
---|
196 | FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3)); |
---|
197 | FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize")); |
---|
198 | FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0)); |
---|
199 | FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize")); |
---|
200 | FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0)); |
---|
201 | FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0)); |
---|
202 | FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize")); |
---|
203 | |
---|
204 | owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval"); |
---|
205 | FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar", OWL_VARIABLE_STRING))); |
---|
206 | FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar"))); |
---|
207 | owl_variable_set_string(&vd, "stringvar", "new val"); |
---|
208 | FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar"))); |
---|
209 | |
---|
210 | owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47); |
---|
211 | FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar", OWL_VARIABLE_INT))); |
---|
212 | FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar")); |
---|
213 | owl_variable_set_int(&vd, "intvar", 17); |
---|
214 | FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar")); |
---|
215 | |
---|
216 | owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1); |
---|
217 | FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar", OWL_VARIABLE_BOOL))); |
---|
218 | FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar")); |
---|
219 | owl_variable_set_bool_off(&vd, "boolvar"); |
---|
220 | FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar")); |
---|
221 | |
---|
222 | owl_variable_dict_cleanup(&vd); |
---|
223 | |
---|
224 | /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */ |
---|
225 | printf("# END testing owl_variable (%d failures)\n", numfailed); |
---|
226 | return(numfailed); |
---|
227 | } |
---|
228 | |
---|
229 | static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch) |
---|
230 | { |
---|
231 | owl_filter *f; |
---|
232 | int ok; |
---|
233 | int failed = 0; |
---|
234 | if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) { |
---|
235 | printf("not ok can't parse %s\n", filt); |
---|
236 | failed = 1; |
---|
237 | goto out; |
---|
238 | } |
---|
239 | ok = owl_filter_message_match(f, m); |
---|
240 | if((shouldmatch && !ok) || (!shouldmatch && ok)) { |
---|
241 | printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch); |
---|
242 | failed = 1; |
---|
243 | } |
---|
244 | out: |
---|
245 | owl_filter_delete(f); |
---|
246 | if(!failed) { |
---|
247 | printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt); |
---|
248 | } |
---|
249 | return failed; |
---|
250 | } |
---|
251 | |
---|
252 | int owl_filter_regtest(void) { |
---|
253 | int numfailed=0; |
---|
254 | owl_message m; |
---|
255 | owl_filter *f1, *f2, *f3, *f4, *f5; |
---|
256 | |
---|
257 | owl_dict_create(&g.filters); |
---|
258 | g.filterlist = NULL; |
---|
259 | owl_message_init(&m); |
---|
260 | owl_message_set_type_zephyr(&m); |
---|
261 | owl_message_set_direction_in(&m); |
---|
262 | owl_message_set_class(&m, "owl"); |
---|
263 | owl_message_set_instance(&m, "tester"); |
---|
264 | owl_message_set_sender(&m, "owl-user"); |
---|
265 | owl_message_set_recipient(&m, "joe"); |
---|
266 | owl_message_set_attribute(&m, "foo", "bar"); |
---|
267 | |
---|
268 | #define TEST_FILTER(f, e) do { \ |
---|
269 | numtests++; \ |
---|
270 | numfailed += owl_filter_test_string(f, &m, e); \ |
---|
271 | } while(0) |
---|
272 | |
---|
273 | TEST_FILTER("true", 1); |
---|
274 | TEST_FILTER("false", 0); |
---|
275 | TEST_FILTER("( true )", 1); |
---|
276 | TEST_FILTER("not false", 1); |
---|
277 | TEST_FILTER("( true ) or ( false )", 1); |
---|
278 | TEST_FILTER("true and false", 0); |
---|
279 | TEST_FILTER("( true or true ) or ( ( false ) )", 1); |
---|
280 | |
---|
281 | TEST_FILTER("class owl", 1); |
---|
282 | TEST_FILTER("class ^owl$", 1); |
---|
283 | TEST_FILTER("instance test", 1); |
---|
284 | TEST_FILTER("instance ^test$", 0); |
---|
285 | TEST_FILTER("instance ^tester$", 1); |
---|
286 | |
---|
287 | TEST_FILTER("foo bar", 1); |
---|
288 | TEST_FILTER("class owl and instance tester", 1); |
---|
289 | TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1); |
---|
290 | |
---|
291 | /* Order of operations and precedence */ |
---|
292 | TEST_FILTER("not true or false", 0); |
---|
293 | TEST_FILTER("true or true and false", 0); |
---|
294 | TEST_FILTER("true and true and false or true", 1); |
---|
295 | TEST_FILTER("false and false or true", 1); |
---|
296 | TEST_FILTER("true and false or false", 0); |
---|
297 | |
---|
298 | f1 = owl_filter_new_fromstring("f1", "class owl"); |
---|
299 | owl_global_add_filter(&g, f1); |
---|
300 | TEST_FILTER("filter f1", 1); |
---|
301 | owl_global_remove_filter(&g, "f1"); |
---|
302 | |
---|
303 | /* Test recursion prevention */ |
---|
304 | FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL); |
---|
305 | owl_filter_delete(f2); |
---|
306 | |
---|
307 | /* mutual recursion */ |
---|
308 | f3 = owl_filter_new_fromstring("f3", "filter f4"); |
---|
309 | owl_global_add_filter(&g, f3); |
---|
310 | FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL); |
---|
311 | owl_global_remove_filter(&g, "f3"); |
---|
312 | owl_filter_delete(f4); |
---|
313 | |
---|
314 | /* support referencing a filter several times */ |
---|
315 | FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL); |
---|
316 | owl_filter_delete(f5); |
---|
317 | |
---|
318 | return 0; |
---|
319 | } |
---|
320 | |
---|
321 | |
---|
322 | int owl_obarray_regtest(void) { |
---|
323 | int numfailed = 0; |
---|
324 | const char *p,*p2; |
---|
325 | |
---|
326 | owl_obarray oa; |
---|
327 | owl_obarray_init(&oa); |
---|
328 | |
---|
329 | printf("# BEGIN testing owl_obarray\n"); |
---|
330 | |
---|
331 | p = owl_obarray_insert(&oa, "test"); |
---|
332 | FAIL_UNLESS("returned string is equal", p && !strcmp(p, "test")); |
---|
333 | p2 = owl_obarray_insert(&oa, "test"); |
---|
334 | FAIL_UNLESS("returned string is equal", p2 && !strcmp(p2, "test")); |
---|
335 | FAIL_UNLESS("returned the same string", p2 && p == p2); |
---|
336 | |
---|
337 | p = owl_obarray_insert(&oa, "test2"); |
---|
338 | FAIL_UNLESS("returned string is equal", p && !strcmp(p, "test2")); |
---|
339 | p2 = owl_obarray_find(&oa, "test2"); |
---|
340 | FAIL_UNLESS("returned the same string", p2 && !strcmp(p2, "test2")); |
---|
341 | |
---|
342 | p = owl_obarray_find(&oa, "nothere"); |
---|
343 | FAIL_UNLESS("Didn't find a string that isn't there", p == NULL); |
---|
344 | |
---|
345 | printf("# END testing owl_obarray (%d failures)\n", numfailed); |
---|
346 | |
---|
347 | return numfailed; |
---|
348 | } |
---|
349 | |
---|
350 | int owl_editwin_regtest(void) { |
---|
351 | int numfailed = 0; |
---|
352 | const char *p; |
---|
353 | |
---|
354 | printf("# BEGIN testing owl_editwin\n"); |
---|
355 | |
---|
356 | owl_editwin *oe; |
---|
357 | oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL); |
---|
358 | |
---|
359 | /* TODO: make the strings a little more lenient w.r.t trailing whitespace */ |
---|
360 | |
---|
361 | /* check paragraph fill */ |
---|
362 | owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah"); |
---|
363 | owl_editwin_move_to_top(oe); |
---|
364 | owl_editwin_fill_paragraph(oe); |
---|
365 | p = owl_editwin_get_text(oe); |
---|
366 | FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n" |
---|
367 | "blah blah blah.\n" |
---|
368 | "\n" |
---|
369 | "blah")); |
---|
370 | |
---|
371 | owl_editwin_delete(oe); oe = NULL; |
---|
372 | oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL); |
---|
373 | |
---|
374 | /* check that lines ending with ". " correctly fill */ |
---|
375 | owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah"); |
---|
376 | owl_editwin_move_to_top(oe); |
---|
377 | owl_editwin_fill_paragraph(oe); |
---|
378 | p = owl_editwin_get_text(oe); |
---|
379 | FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n" |
---|
380 | "blah blah blah. \n" |
---|
381 | "\n" |
---|
382 | "blah")); |
---|
383 | |
---|
384 | owl_editwin_delete(oe); oe = NULL; |
---|
385 | |
---|
386 | printf("# END testing owl_editwin (%d failures)\n", numfailed); |
---|
387 | |
---|
388 | return numfailed; |
---|
389 | } |
---|