source: tester.c @ 5aa33fd

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