source: tester.c @ 04b16f8

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