source: tester.c @ f034ac0

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