source: tester.c @ 22e02cd

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