source: tester.c @ 98d7757

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