source: tester.c @ a999d9e

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