source: tester.c @ 0e8a0fc

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 0e8a0fc was dd6af02, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 15 years ago
Revert 7b4d90e... and just fix #51 owl_fmtext_truncate_cols once again expects that fmtext ends in a newline. Revert documentation changes and remove test that now breaks while testing out-of-spec text. In owl_viewwin_init_fmtext, check that fmtext ends in a newline and append one if necessary.
  • Property mode set to 100644
File size: 11.6 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);
14
15int main(int argc, char **argv, char **env)
16{
17  owl_errqueue_init(owl_global_get_errqueue(&g));
18  owl_obarray_init(&(g.obarray));
19
20  numtests = 0;
21  int numfailures=0;
22  /*
23    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
24    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
25  */
26  numfailures += owl_util_regtest();
27  numfailures += owl_dict_regtest();
28  numfailures += owl_variable_regtest();
29  numfailures += owl_filter_regtest();
30  numfailures += owl_obarray_regtest();
31  if (numfailures) {
32      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
33  }
34  printf("1..%d\n", numtests);
35  return(numfailures);
36}
37
38#define FAIL_UNLESS(desc,pred) do { int __pred = (pred);                \
39    numtests++;                                                         \
40    printf("%s %s", (__pred)?"ok":(numfailed++,"not ok"), desc);        \
41    if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0)
42
43
44int owl_util_regtest(void)
45{
46  int numfailed=0;
47
48  printf("# BEGIN testing owl_util\n");
49
50  FAIL_UNLESS("owl_util_substitute 1",
51              !strcmp("foo", owl_text_substitute("foo", "", "Y")));
52  FAIL_UNLESS("owl_text_substitute 2",
53              !strcmp("fYZYZ", owl_text_substitute("foo", "o", "YZ")));
54  FAIL_UNLESS("owl_text_substitute 3",
55              !strcmp("foo", owl_text_substitute("fYZYZ", "YZ", "o")));
56  FAIL_UNLESS("owl_text_substitute 4",
57              !strcmp("/u/foo/meep", owl_text_substitute("~/meep", "~", "/u/foo")));
58
59  FAIL_UNLESS("skiptokens 1", 
60              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
61  FAIL_UNLESS("skiptokens 2", 
62              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
63
64  FAIL_UNLESS("expand_tabs 1",
65              !strcmp("        hi", owl_text_expand_tabs("\thi")));
66
67  FAIL_UNLESS("expand_tabs 2",
68              !strcmp("        hi\nword    tab", owl_text_expand_tabs("\thi\nword\ttab")));
69
70  FAIL_UNLESS("expand_tabs 3",
71              !strcmp("                2 tabs", owl_text_expand_tabs("\t\t2 tabs")));
72
73  FAIL_UNLESS("skiptokens 1",
74              !strcmp("world", skiptokens("hello world", 1)));
75
76  FAIL_UNLESS("skiptokens 2",
77              !strcmp("c d e", skiptokens("a   b c d e", 2)));
78
79  FAIL_UNLESS("skiptokens 3",
80              !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
81
82  FAIL_UNLESS("skiptokens 4",
83              !strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
84
85  FAIL_UNLESS("skiptokens 5",
86              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
87
88  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
89  printf("# END testing owl_util (%d failures)\n", numfailed);
90  return(numfailed);
91}
92
93int owl_dict_regtest(void) {
94  owl_dict d;
95  owl_list l;
96  int numfailed=0;
97  char *av="aval", *bv="bval", *cv="cval", *dv="dval";
98
99  printf("# BEGIN testing owl_dict\n");
100  FAIL_UNLESS("create", 0==owl_dict_create(&d));
101  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_free));
102  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_free));
103  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_free));
104  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_free));
105  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
106  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
107  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
108  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
109  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
110  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
111  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
112  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
113
114  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
115  FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l));
116  FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l));
117 
118  /* these assume the returned keys are sorted */
119  FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0)));
120  FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1)));
121  FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2)));
122
123  owl_list_free_all(&l, owl_free);
124  owl_dict_free_all(&d, NULL);
125
126  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
127  printf("# END testing owl_dict (%d failures)\n", numfailed);
128  return(numfailed);
129}
130
131int owl_variable_regtest(void) {
132  owl_vardict vd;
133  int numfailed=0;
134  char buf[1024];
135  const void *v;
136
137  printf("# BEGIN testing owl_variable\n");
138  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
139
140  FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping"));
141  FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
142  FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"rxping", buf, 1024));
143  FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off"));
144  FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
145  FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
146  FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0));
147  FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping"));
148  FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0));
149  FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping"));
150
151
152  FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
153  FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
154  FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
155
156  FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
157  FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
158  FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024));
159  FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8"));
160  FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
161  FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
162  FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
163  FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
164  FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0));
165  FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
166  FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0));
167  FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0));
168  FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
169
170  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
171  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar", OWL_VARIABLE_STRING)));
172  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar")));
173  owl_variable_set_string(&vd, "stringvar", "new val");
174  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar")));
175
176  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
177  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar", OWL_VARIABLE_INT)));
178  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar"));
179  owl_variable_set_int(&vd, "intvar", 17);
180  FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar"));
181
182  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
183  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar", OWL_VARIABLE_BOOL)));
184  FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar"));
185  owl_variable_set_bool_off(&vd, "boolvar");
186  FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar"));
187
188  owl_variable_dict_free(&vd);
189
190  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
191  printf("# END testing owl_variable (%d failures)\n", numfailed);
192  return(numfailed);
193}
194
195int owl_filter_test_string(const char * filt, const owl_message *m, int shouldmatch) /* noproto */ {
196  owl_filter f;
197  int ok;
198  int failed = 0;
199  if(owl_filter_init_fromstring(&f, "test-filter", filt)) {
200    printf("not ok can't parse %s\n", filt);
201    failed = 1;
202    goto out;
203  }
204  ok = owl_filter_message_match(&f, m);
205  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
206    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
207    failed = 1;
208  }
209 out:
210  owl_filter_free(&f);
211  if(!failed) {
212    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
213  }
214  return failed;
215}
216
217int owl_filter_regtest(void) {
218  int numfailed=0;
219  owl_message m;
220  owl_filter f1, f2, f3, f4, f5;
221
222  owl_list_create(&(g.filterlist));
223  owl_message_init(&m);
224  owl_message_set_type_zephyr(&m);
225  owl_message_set_direction_in(&m);
226  owl_message_set_class(&m, "owl");
227  owl_message_set_instance(&m, "tester");
228  owl_message_set_sender(&m, "owl-user");
229  owl_message_set_recipient(&m, "joe");
230  owl_message_set_attribute(&m, "foo", "bar");
231
232#define TEST_FILTER(f, e) do {                          \
233    numtests++;                                         \
234    numfailed += owl_filter_test_string(f, &m, e);      \
235      } while(0)
236
237  TEST_FILTER("true", 1);
238  TEST_FILTER("false", 0);
239  TEST_FILTER("( true )", 1);
240  TEST_FILTER("not false", 1);
241  TEST_FILTER("( true ) or ( false )", 1);
242  TEST_FILTER("true and false", 0);
243  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
244
245  TEST_FILTER("class owl", 1);
246  TEST_FILTER("class ^owl$", 1);
247  TEST_FILTER("instance test", 1);
248  TEST_FILTER("instance ^test$", 0);
249  TEST_FILTER("instance ^tester$", 1);
250
251  TEST_FILTER("foo bar", 1);
252  TEST_FILTER("class owl and instance tester", 1);
253  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
254
255  /* Order of operations and precedence */
256  TEST_FILTER("not true or false", 0);
257  TEST_FILTER("true or true and false", 0);
258  TEST_FILTER("true and true and false or true", 1);
259  TEST_FILTER("false and false or true", 1);
260  TEST_FILTER("true and false or false", 0);
261
262  owl_filter_init_fromstring(&f1, "f1", "class owl");
263  owl_global_add_filter(&g, &f1);
264  TEST_FILTER("filter f1", 1);
265
266  /* Test recursion prevention */
267  FAIL_UNLESS("self reference", owl_filter_init_fromstring(&f2, "test", "filter test"));
268
269  /* mutual recursion */
270  owl_filter_init_fromstring(&f3, "f3", "filter f4");
271  owl_global_add_filter(&g, &f3);
272  FAIL_UNLESS("mutual recursion",   owl_filter_init_fromstring(&f4, "f4", "filter f3"));
273
274  /* support referencing a filter several times */
275  FAIL_UNLESS("DAG", !owl_filter_init_fromstring(&f5, "dag", "filter f1 or filter f1"));
276
277  return 0;
278}
279
280
281int owl_obarray_regtest(void) {
282  int numfailed = 0;
283  const char *p,*p2;
284
285  owl_obarray oa;
286  owl_obarray_init(&oa);
287
288  printf("# BEGIN testing owl_obarray\n");
289
290  p = owl_obarray_insert(&oa, "test");
291  FAIL_UNLESS("returned string is equal", p && !strcmp(p, "test"));
292  p2 = owl_obarray_insert(&oa, "test");
293  FAIL_UNLESS("returned string is equal", p2 && !strcmp(p2, "test"));
294  FAIL_UNLESS("returned the same string", p2 && p == p2);
295
296  p = owl_obarray_insert(&oa, "test2");
297  FAIL_UNLESS("returned string is equal", p && !strcmp(p, "test2"));
298  p2 = owl_obarray_find(&oa, "test2");
299  FAIL_UNLESS("returned the same string", p2 && !strcmp(p2, "test2"));
300
301  p = owl_obarray_find(&oa, "nothere");
302  FAIL_UNLESS("Didn't find a string that isn't there", p == NULL);
303
304  printf("# END testing owl_obarray (%d failures)\n", numfailed);
305
306  return numfailed;
307}
Note: See TracBrowser for help on using the repository browser.