source: tester.c @ dec60b4

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