source: tester.c @ 89ce405

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 89ce405 was 3f6555d, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Punt obarray and friends in favor of g_intern_string It's somewhat faster (about 1.5x by completely unrigorous tests), and it's less code for us to maintain. Signed-off-by: David Benjamin <davidben@mit.edu>
  • Property mode set to 100644
File size: 13.0 KB
Line 
1#include "owl.h"
2#include <unistd.h>
3#include <stdlib.h>
4
5owl_global g;
6
7int numtests;
8
9int owl_util_regtest(void);
10int owl_dict_regtest(void);
11int owl_variable_regtest(void);
12int owl_filter_regtest(void);
13int owl_obarray_regtest(void);
14int owl_editwin_regtest(void);
15
16int main(int argc, char **argv, char **env)
17{
18  /* initialize a fake ncurses, detached from std{in,out} */
19  FILE *rnull = fopen("/dev/null", "r");
20  FILE *wnull = fopen("/dev/null", "w");
21  newterm("xterm", wnull, rnull);
22  /* initialize global structures */
23  owl_global_init(&g);
24
25  numtests = 0;
26  int numfailures=0;
27  /*
28    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
29    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
30  */
31  numfailures += owl_util_regtest();
32  numfailures += owl_dict_regtest();
33  numfailures += owl_variable_regtest();
34  numfailures += owl_filter_regtest();
35  numfailures += owl_editwin_regtest();
36  if (numfailures) {
37      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
38  }
39  printf("1..%d\n", numtests);
40
41  /* probably not necessary, but tear down the screen */
42  endwin();
43  fclose(rnull);
44  fclose(wnull);
45
46  return(numfailures);
47}
48
49#define FAIL_UNLESS(desc,pred) do { int __pred = (pred);                \
50    numtests++;                                                         \
51    printf("%s %s", (__pred)?"ok":(numfailed++,"not ok"), desc);        \
52    if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0)
53
54
55int owl_util_regtest(void)
56{
57  int numfailed=0;
58
59  printf("# BEGIN testing owl_util\n");
60
61  FAIL_UNLESS("owl_util_substitute 1",
62              !strcmp("foo", owl_text_substitute("foo", "", "Y")));
63  FAIL_UNLESS("owl_text_substitute 2",
64              !strcmp("fYZYZ", owl_text_substitute("foo", "o", "YZ")));
65  FAIL_UNLESS("owl_text_substitute 3",
66              !strcmp("foo", owl_text_substitute("fYZYZ", "YZ", "o")));
67  FAIL_UNLESS("owl_text_substitute 4",
68              !strcmp("/u/foo/meep", owl_text_substitute("~/meep", "~", "/u/foo")));
69
70  FAIL_UNLESS("skiptokens 1", 
71              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
72  FAIL_UNLESS("skiptokens 2", 
73              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
74
75  FAIL_UNLESS("expand_tabs 1",
76              !strcmp("        hi", owl_text_expand_tabs("\thi")));
77
78  FAIL_UNLESS("expand_tabs 2",
79              !strcmp("        hi\nword    tab", owl_text_expand_tabs("\thi\nword\ttab")));
80
81  FAIL_UNLESS("expand_tabs 3",
82              !strcmp("                2 tabs", owl_text_expand_tabs("\t\t2 tabs")));
83  FAIL_UNLESS("expand_tabs 4",
84              !strcmp("α       ααααααα!        ", owl_text_expand_tabs(\tααααααα!\t")));
85  FAIL_UNLESS("expand_tabs 5",
86              !strcmp("A      AAA!!        ", owl_text_expand_tabs("A\tAAA!!\t")));
87
88  FAIL_UNLESS("skiptokens 1",
89              !strcmp("world", skiptokens("hello world", 1)));
90
91  FAIL_UNLESS("skiptokens 2",
92              !strcmp("c d e", skiptokens("a   b c d e", 2)));
93
94  FAIL_UNLESS("skiptokens 3",
95              !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
96
97  FAIL_UNLESS("skiptokens 4",
98              !strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
99
100  FAIL_UNLESS("skiptokens 5",
101              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
102
103  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
104  printf("# END testing owl_util (%d failures)\n", numfailed);
105  return(numfailed);
106}
107
108int owl_dict_regtest(void) {
109  owl_dict d;
110  owl_list l;
111  int numfailed=0;
112  char *av="aval", *bv="bval", *cv="cval", *dv="dval";
113
114  printf("# BEGIN testing owl_dict\n");
115  FAIL_UNLESS("create", 0==owl_dict_create(&d));
116  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
117  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
118  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
119  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
120  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
121  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
122  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
123  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
124  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
125  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
126  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
127  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
128
129  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
130  FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l));
131  FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l));
132 
133  /* these assume the returned keys are sorted */
134  FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0)));
135  FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1)));
136  FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2)));
137
138  owl_list_cleanup(&l, owl_free);
139  owl_dict_cleanup(&d, NULL);
140
141  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
142  printf("# END testing owl_dict (%d failures)\n", numfailed);
143  return(numfailed);
144}
145
146int owl_variable_regtest(void) {
147  owl_vardict vd;
148  int numfailed=0;
149  char buf[1024];
150  const void *v;
151
152  printf("# BEGIN testing owl_variable\n");
153  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
154
155  FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping"));
156  FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
157  FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"rxping", buf, 1024));
158  FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off"));
159  FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
160  FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
161  FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0));
162  FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping"));
163  FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0));
164  FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping"));
165
166
167  FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
168  FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
169  FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
170
171  FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
172  FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
173  FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024));
174  FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8"));
175  FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
176  FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
177  FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
178  FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
179  FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0));
180  FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
181  FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0));
182  FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0));
183  FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
184
185  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
186  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar", OWL_VARIABLE_STRING)));
187  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar")));
188  owl_variable_set_string(&vd, "stringvar", "new val");
189  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar")));
190
191  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
192  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar", OWL_VARIABLE_INT)));
193  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar"));
194  owl_variable_set_int(&vd, "intvar", 17);
195  FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar"));
196
197  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
198  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar", OWL_VARIABLE_BOOL)));
199  FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar"));
200  owl_variable_set_bool_off(&vd, "boolvar");
201  FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar"));
202
203  owl_variable_dict_cleanup(&vd);
204
205  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
206  printf("# END testing owl_variable (%d failures)\n", numfailed);
207  return(numfailed);
208}
209
210static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
211{
212  owl_filter *f;
213  int ok;
214  int failed = 0;
215  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
216    printf("not ok can't parse %s\n", filt);
217    failed = 1;
218    goto out;
219  }
220  ok = owl_filter_message_match(f, m);
221  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
222    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
223    failed = 1;
224  }
225 out:
226  owl_filter_delete(f);
227  if(!failed) {
228    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
229  }
230  return failed;
231}
232
233int owl_filter_regtest(void) {
234  int numfailed=0;
235  owl_message m;
236  owl_filter *f1, *f2, *f3, *f4, *f5;
237
238  owl_dict_create(&g.filters);
239  g.filterlist = NULL;
240  owl_message_init(&m);
241  owl_message_set_type_zephyr(&m);
242  owl_message_set_direction_in(&m);
243  owl_message_set_class(&m, "owl");
244  owl_message_set_instance(&m, "tester");
245  owl_message_set_sender(&m, "owl-user");
246  owl_message_set_recipient(&m, "joe");
247  owl_message_set_attribute(&m, "foo", "bar");
248
249#define TEST_FILTER(f, e) do {                          \
250    numtests++;                                         \
251    numfailed += owl_filter_test_string(f, &m, e);      \
252      } while(0)
253
254  TEST_FILTER("true", 1);
255  TEST_FILTER("false", 0);
256  TEST_FILTER("( true )", 1);
257  TEST_FILTER("not false", 1);
258  TEST_FILTER("( true ) or ( false )", 1);
259  TEST_FILTER("true and false", 0);
260  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
261
262  TEST_FILTER("class owl", 1);
263  TEST_FILTER("class ^owl$", 1);
264  TEST_FILTER("instance test", 1);
265  TEST_FILTER("instance ^test$", 0);
266  TEST_FILTER("instance ^tester$", 1);
267
268  TEST_FILTER("foo bar", 1);
269  TEST_FILTER("class owl and instance tester", 1);
270  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
271
272  /* Order of operations and precedence */
273  TEST_FILTER("not true or false", 0);
274  TEST_FILTER("true or true and false", 0);
275  TEST_FILTER("true and true and false or true", 1);
276  TEST_FILTER("false and false or true", 1);
277  TEST_FILTER("true and false or false", 0);
278
279  f1 = owl_filter_new_fromstring("f1", "class owl");
280  owl_global_add_filter(&g, f1);
281  TEST_FILTER("filter f1", 1);
282  owl_global_remove_filter(&g, "f1");
283
284  /* Test recursion prevention */
285  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
286  owl_filter_delete(f2);
287
288  /* mutual recursion */
289  f3 = owl_filter_new_fromstring("f3", "filter f4");
290  owl_global_add_filter(&g, f3);
291  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
292  owl_global_remove_filter(&g, "f3");
293  owl_filter_delete(f4);
294
295  /* support referencing a filter several times */
296  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
297  owl_filter_delete(f5);
298
299  return 0;
300}
301
302int owl_editwin_regtest(void) {
303  int numfailed = 0;
304  const char *p;
305
306  printf("# BEGIN testing owl_editwin\n");
307
308  owl_editwin *oe;
309  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
310
311  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
312
313  /* check paragraph fill */
314  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
315  owl_editwin_move_to_top(oe);
316  owl_editwin_fill_paragraph(oe);
317  p = owl_editwin_get_text(oe);
318  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
319                                                            "blah blah blah.\n"
320                                                            "\n"
321                                                            "blah"));
322
323  owl_editwin_delete(oe); oe = NULL;
324  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
325
326  /* check that lines ending with ". " correctly fill */
327  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
328  owl_editwin_move_to_top(oe);
329  owl_editwin_fill_paragraph(oe);
330  p = owl_editwin_get_text(oe);
331  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
332                                                            "blah blah blah. \n"
333                                                            "\n"
334                                                            "blah"));
335
336  owl_editwin_delete(oe); oe = NULL;
337
338  printf("# END testing owl_editwin (%d failures)\n", numfailed);
339
340  return numfailed;
341}
Note: See TracBrowser for help on using the repository browser.