source: tester.c @ e21b921

Last change on this file since e21b921 was e21b921, checked in by Jason Gross <jgross@mit.edu>, 14 years ago
Add tests for enum variables.
  • Property mode set to 100644
File size: 34.6 KB
Line 
1#define OWL_PERL
2#define WINDOW FAKE_WINDOW
3#include "owl.h"
4#undef WINDOW
5
6#include <stdio.h>
7
8#undef instr
9#include <ncursesw/curses.h>
10
11owl_global g;
12
13int numtests;
14
15int owl_regtest(void);
16int owl_util_regtest(void);
17int owl_dict_regtest(void);
18int owl_variable_regtest(void);
19int owl_filter_regtest(void);
20int owl_obarray_regtest(void);
21int owl_editwin_regtest(void);
22int owl_fmtext_regtest(void);
23int owl_smartfilter_regtest(void);
24int owl_history_regtest(void);
25
26extern void owl_perl_xs_init(pTHX);
27
28int main(int argc, char **argv, char **env)
29{
30  FILE *rnull;
31  FILE *wnull;
32  char *perlerr;
33  int status = 0;
34  SCREEN *screen;
35
36  if (argc <= 1) {
37    fprintf(stderr, "Usage: %s --builtin|TEST.t|-le CODE\n", argv[0]);
38    return 1;
39  }
40
41  /* initialize a fake ncurses, detached from std{in,out} */
42  wnull = fopen("/dev/null", "w");
43  rnull = fopen("/dev/null", "r");
44  screen = newterm("xterm", wnull, rnull);
45  /* initialize global structures */
46  owl_global_init(&g);
47
48  perlerr = owl_perlconfig_initperl(NULL, &argc, &argv, &env);
49  if (perlerr) {
50    endwin();
51    fprintf(stderr, "Internal perl error: %s\n", perlerr);
52    status = 1;
53    goto out;
54  }
55
56  owl_global_complete_setup(&g);
57  owl_global_setup_default_filters(&g);
58
59  owl_view_create(owl_global_get_current_view(&g), "main",
60                  owl_global_get_filter(&g, "all"),
61                  owl_global_get_style_by_name(&g, "default"));
62
63  owl_function_firstmsg();
64
65  ENTER;
66  SAVETMPS;
67
68  if (strcmp(argv[1], "--builtin") == 0) {
69    status = owl_regtest();
70  } else if (strcmp(argv[1], "-le") == 0 && argc > 2) {
71    /*
72     * 'prove' runs its harness perl with '-le CODE' to get some
73     * information out.
74     */
75    moreswitches("l");
76    eval_pv(argv[2], true);
77  } else {
78    sv_setpv(get_sv("0", false), argv[1]);
79    sv_setpv(get_sv("main::test_prog", TRUE), argv[1]);
80
81    eval_pv("do $main::test_prog; die($@) if($@)", true);
82  }
83
84  status = 0;
85
86  FREETMPS;
87  LEAVE;
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  delscreen(screen);
95  fclose(rnull);
96  fclose(wnull);
97  return status;
98}
99
100int owl_regtest(void) {
101  numtests = 0;
102  int numfailures=0;
103  /*
104    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
105    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
106  */
107  numfailures += owl_util_regtest();
108  numfailures += owl_dict_regtest();
109  numfailures += owl_variable_regtest();
110  numfailures += owl_filter_regtest();
111  numfailures += owl_editwin_regtest();
112  numfailures += owl_fmtext_regtest();
113  numfailures += owl_smartfilter_regtest();
114  numfailures += owl_history_regtest();
115  if (numfailures) {
116      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
117  }
118  printf("1..%d\n", numtests);
119
120  return(numfailures);
121}
122
123#define FAIL_UNLESS(desc,pred) do { int __pred = (pred);                \
124    numtests++;                                                         \
125    printf("%s %d %s", (__pred) ? "ok" : (numfailed++, "not ok"), numtests, desc); \
126    if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0)
127
128
129int owl_util_regtest(void)
130{
131  int numfailed=0;
132  char *s, *path, *home;
133
134  printf("# BEGIN testing owl_util\n");
135
136#define CHECK_STR_AND_FREE(desc, expected, expr)         \
137    do {                                                 \
138      char *__value = (expr);                            \
139      FAIL_UNLESS((desc), !strcmp((expected), __value)); \
140      g_free(__value);                                 \
141    } while (0)
142
143  CHECK_STR_AND_FREE("owl_text_substitute 2", "fYZYZ",
144                     owl_text_substitute("foo", "o", "YZ"));
145  CHECK_STR_AND_FREE("owl_text_substitute 3", "foo",
146                     owl_text_substitute("fYZYZ", "YZ", "o"));
147  CHECK_STR_AND_FREE("owl_text_substitute 4", "/u/foo/meep",
148                     owl_text_substitute("~/meep", "~", "/u/foo"));
149
150  FAIL_UNLESS("skiptokens 1", 
151              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
152  FAIL_UNLESS("skiptokens 2", 
153              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
154
155  CHECK_STR_AND_FREE("expand_tabs 1", "        hi", owl_text_expand_tabs("\thi"));
156
157  CHECK_STR_AND_FREE("expand_tabs 2", "        hi\nword    tab",
158                     owl_text_expand_tabs("\thi\nword\ttab"));
159
160  CHECK_STR_AND_FREE("expand_tabs 3", "                2 tabs",
161                     owl_text_expand_tabs("\t\t2 tabs"));
162  CHECK_STR_AND_FREE("expand_tabs 4", "α       ααααααα!        ",
163                     owl_text_expand_tabs(\tααααααα!\t"));
164  CHECK_STR_AND_FREE("expand_tabs 5", "A      AAA!!        ",
165                     owl_text_expand_tabs("A\tAAA!!\t"));
166
167  FAIL_UNLESS("skiptokens 1",
168              !strcmp("world", skiptokens("hello world", 1)));
169
170  FAIL_UNLESS("skiptokens 2",
171              !strcmp("c d e", skiptokens("a   b c d e", 2)));
172
173  FAIL_UNLESS("skiptokens 3",
174              !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
175
176  FAIL_UNLESS("skiptokens 4",
177              !strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
178
179  FAIL_UNLESS("skiptokens 5",
180              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
181
182#define CHECK_QUOTING(desc, unquoted, quoted)           \
183  do {                                                  \
184      int __argc;                                       \
185      char *__quoted = owl_arg_quote(unquoted);         \
186      char **__argv;                                    \
187      FAIL_UNLESS(desc, !strcmp(quoted, __quoted));     \
188      __argv = owl_parseline(__quoted, &__argc);        \
189      FAIL_UNLESS(desc " - arg count", __argc == 1);    \
190      FAIL_UNLESS(desc " - null-terminated",            \
191                  __argv[__argc] == NULL);              \
192      FAIL_UNLESS(desc " - parsed",                     \
193                  !strcmp(__argv[0], unquoted));        \
194      g_strfreev(__argv);                               \
195      g_free(__quoted);                         \
196    } while (0)
197
198  CHECK_QUOTING("boring text", "mango", "mango");
199  CHECK_QUOTING("spaces", "mangos are tasty", "'mangos are tasty'");
200  CHECK_QUOTING("single quotes", "mango's", "\"mango's\"");
201  CHECK_QUOTING("double quotes", "he said \"mangos are tasty\"",
202                "'he said \"mangos are tasty\"'");
203  CHECK_QUOTING("both quotes",
204                "he said \"mango's are tasty even when you put in "
205                "a random apostrophe\"",
206                "\"he said \"'\"'\"mango's are tasty even when you put in "
207                "a random apostrophe\"'\"'\"\"");
208  CHECK_QUOTING("quote monster", "'\"\"'\"'''\"",
209                "\""
210                "'"
211                "\"'\"'\""
212                "\"'\"'\""
213                "'"
214                "\"'\"'\""
215                "'"
216                "'"
217                "'"
218                "\"'\"'\""
219                "\"");
220
221  GString *quoted = g_string_new("");
222  owl_string_appendf_quoted(quoted, "%q foo %q%q %s %", "hello", "world is", "can't");
223  FAIL_UNLESS("owl_string_appendf",
224              !strcmp(quoted->str, "hello foo 'world is'\"can't\" %s %"));
225  g_string_free(quoted, true);
226
227
228  s = owl_util_baseclass("barnowl");
229  FAIL_UNLESS("baseclass barnowl", !strcmp("barnowl", s));
230  g_free(s);
231  s = owl_util_baseclass("unbarnowl");
232  FAIL_UNLESS("baseclass unbarnowl", !strcmp("barnowl", s));
233  g_free(s);
234  s = owl_util_baseclass("unununbarnowl.d.d");
235  FAIL_UNLESS("baseclass unununbarnowl.d.d", !strcmp("barnowl", s));
236  g_free(s);
237  s = owl_util_baseclass("ununun.d.d");
238  FAIL_UNLESS("baseclass ununun.d.d", !strcmp("", s));
239  g_free(s);
240  s = owl_util_baseclass("d.d.d.d");
241  FAIL_UNLESS("baseclass d.d.d.d", !strcmp("d", s));
242  g_free(s);
243  s = owl_util_baseclass("n.d.d.d");
244  FAIL_UNLESS("baseclass n.d.d.d", !strcmp("n", s));
245  g_free(s);
246  s = owl_util_baseclass("ununun.");
247  FAIL_UNLESS("baseclass ununun.", !strcmp(".", s));
248  g_free(s);
249  s = owl_util_baseclass("unununu");
250  FAIL_UNLESS("baseclass unununu", !strcmp("u", s));
251  g_free(s);
252
253
254  s = owl_util_makepath("foo/bar");
255  FAIL_UNLESS("makepath foo/bar", !strcmp("foo/bar", s));
256  g_free(s);
257  s = owl_util_makepath("//foo///bar");
258  FAIL_UNLESS("makepath //foo///bar", !strcmp("/foo/bar", s));
259  g_free(s);
260  s = owl_util_makepath("foo/~//bar/");
261  FAIL_UNLESS("makepath foo/~//bar/", !strcmp("foo/~/bar/", s));
262  g_free(s);
263  s = owl_util_makepath("~thisuserhadreallybetternotexist/foobar/");
264  FAIL_UNLESS("makepath ~thisuserhadreallybetternotexist/foobar/",
265              !strcmp("~thisuserhadreallybetternotexist/foobar/", s));
266  g_free(s);
267
268  home = g_strdup(owl_global_get_homedir(&g));
269  s = owl_util_makepath("~");
270  FAIL_UNLESS("makepath ~", !strcmp(home, s));
271  g_free(s);
272
273  path = g_build_filename(home, "foo/bar/baz", NULL);
274  s = owl_util_makepath("~///foo/bar//baz");
275  FAIL_UNLESS("makepath ~///foo/bar//baz", !strcmp(path, s));
276  g_free(s);
277  g_free(path);
278  g_free(home);
279
280  home = owl_util_homedir_for_user("root");
281  if (home == NULL) {
282    /* Just make some noise so we notice. */
283    home = g_strdup("<WHAT>");
284    fprintf(stderr, "owl_util_homedir_for_user failed");
285  }
286
287  s = owl_util_makepath("~root");
288  FAIL_UNLESS("makepath ~root", !strcmp(home, s));
289  g_free(s);
290
291  path = g_build_filename(home, "foo/bar/baz", NULL);
292  s = owl_util_makepath("~root///foo/bar//baz");
293  FAIL_UNLESS("makepath ~root///foo/bar//baz", !strcmp(path, s));
294  g_free(s);
295  g_free(path);
296  g_free(home);
297
298  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
299  printf("# END testing owl_util (%d failures)\n", numfailed);
300  return(numfailed);
301}
302
303int owl_dict_regtest(void) {
304  owl_dict d;
305  GPtrArray *l;
306  int numfailed=0;
307  char *av = g_strdup("aval"), *bv = g_strdup("bval"), *cv = g_strdup("cval"),
308    *dv = g_strdup("dval");
309
310  printf("# BEGIN testing owl_dict\n");
311  owl_dict_create(&d);
312  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
313  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
314  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
315  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
316  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
317  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
318  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
319  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
320  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
321  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
322  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
323  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
324
325  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
326  l = owl_dict_get_keys(&d);
327  FAIL_UNLESS("get_keys result size", 3 == l->len);
328 
329  /* these assume the returned keys are sorted */
330  FAIL_UNLESS("get_keys result val", 0 == strcmp("a", l->pdata[0]));
331  FAIL_UNLESS("get_keys result val", 0 == strcmp("b", l->pdata[1]));
332  FAIL_UNLESS("get_keys result val", 0 == strcmp("c", l->pdata[2]));
333
334  owl_ptr_array_free(l, g_free);
335  owl_dict_cleanup(&d, NULL);
336
337  g_free(av);
338  g_free(bv);
339  g_free(cv);
340  g_free(dv);
341
342  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
343  printf("# END testing owl_dict (%d failures)\n", numfailed);
344  return(numfailed);
345}
346
347int owl_variable_regtest(void) {
348  owl_vardict vd;
349  owl_variable *var;
350  int numfailed=0;
351  char *value;
352  const void *v;
353
354  printf("# BEGIN testing owl_variable\n");
355  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
356
357  FAIL_UNLESS("get bool var", NULL != (var = owl_variable_get_var(&vd, "rxping")));
358  FAIL_UNLESS("get bool", 0 == owl_variable_get_bool(var));
359  FAIL_UNLESS("get bool (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
360  FAIL_UNLESS("get bool as string",
361              !strcmp((value = owl_variable_get_tostring(var)), "off"));
362  g_free(value);
363  FAIL_UNLESS("set bool 1", 0 == owl_variable_set_bool_on(var));
364  FAIL_UNLESS("get bool 2", 1 == owl_variable_get_bool(var));
365  FAIL_UNLESS("set bool 3", 0 == owl_variable_set_fromstring(var, "off", 0));
366  FAIL_UNLESS("get bool 4", 0 == owl_variable_get_bool(var));
367  FAIL_UNLESS("set bool 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
368  FAIL_UNLESS("get bool 6", 0 == owl_variable_get_bool(var));
369
370
371  FAIL_UNLESS("get string var", NULL != (var = owl_variable_get_var(&vd, "logpath")));
372  FAIL_UNLESS("get string", 0 == strcmp("~/zlog/people", owl_variable_get_string(var)));
373  FAIL_UNLESS("set string 7", 0 == owl_variable_set_string(var, "whee"));
374  FAIL_UNLESS("get string", !strcmp("whee", owl_variable_get_string(var)));
375
376  FAIL_UNLESS("get int var", NULL != (var = owl_variable_get_var(&vd, "typewinsize")));
377  FAIL_UNLESS("get int", 8 == owl_variable_get_int(var));
378  FAIL_UNLESS("get int (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
379  FAIL_UNLESS("get int as string",
380              !strcmp((value = owl_variable_get_tostring(var)), "8"));
381  g_free(value);
382  FAIL_UNLESS("set int 1", 0 == owl_variable_set_int(var, 12));
383  FAIL_UNLESS("get int 2", 12 == owl_variable_get_int(var));
384  FAIL_UNLESS("set int 1b", -1 == owl_variable_set_int(var, -3));
385  FAIL_UNLESS("get int 2b", 12 == owl_variable_get_int(var));
386  FAIL_UNLESS("set int 3", 0 == owl_variable_set_fromstring(var, "9", 0));
387  FAIL_UNLESS("get int 4", 9 == owl_variable_get_int(var));
388  FAIL_UNLESS("set int 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
389  FAIL_UNLESS("set int 6", -1 == owl_variable_set_fromstring(var, "", 0));
390  FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var));
391
392  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval", "<string>");
393  FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar")));
394  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(var)));
395  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var)));
396  owl_variable_set_string(var, "new val");
397  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var)));
398
399  owl_variable_dict_newvar_enum(&vd, "enumvar", "", "", 0, "foo,bar,baz");
400  FAIL_UNLESS("get new enum var", NULL != (var = owl_variable_get_var(&vd, "enumvar")));
401  FAIL_UNLESS("get new enum var", NULL != (v = owl_variable_get(var)));
402  FAIL_UNLESS("get new enum val tostring", !strcmp("foo", value = owl_variable_get_tostring(var)));
403  g_free(value);
404  FAIL_UNLESS("valid update enum var", 0 == owl_variable_set_int(var, 1));
405  FAIL_UNLESS("update int enum val tostring", !strcmp("bar", value = owl_variable_get_tostring(var)));
406  g_free(value);
407  FAIL_UNLESS("update int enum val", 1 == owl_variable_get_int(var));
408  FAIL_UNLESS("invalid update enum var", -1 == owl_variable_set_int(var, 5));
409  FAIL_UNLESS("no update int enum val", 1 == owl_variable_get_int(var));
410  FAIL_UNLESS("valid update enum var fromstring", 0 == owl_variable_set_fromstring(var, "baz", 0));
411  FAIL_UNLESS("update int enum val fromstring", 2 == owl_variable_get_int(var));
412  FAIL_UNLESS("invalid update enum var fromstring", -1 == owl_variable_set_fromstring(var, "xxx", 0));
413  FAIL_UNLESS("no update int enum val fromstring", 2 == owl_variable_get_int(var));
414
415  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47, 0);
416  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
417  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var)));
418  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
419  owl_variable_set_int(var, 17);
420  FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var));
421
422  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
423  FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar")));
424  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(var)));
425  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
426  owl_variable_set_bool_off(var);
427  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
428
429  owl_variable_dict_newvar_string(&vd, "nullstringvar", "", "", NULL, "<string>");
430  FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar")));
431  FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var)));
432  g_free(value);
433  var = owl_variable_get_var(&vd, "zsigproc");
434  FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var)));
435  g_free(value);
436
437  owl_variable_dict_cleanup(&vd);
438
439  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
440  printf("# END testing owl_variable (%d failures)\n", numfailed);
441  return(numfailed);
442}
443
444static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
445{
446  owl_filter *f;
447  int ok;
448  int failed = 0;
449  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
450    printf("not ok can't parse %s\n", filt);
451    failed = 1;
452    goto out;
453  }
454  ok = owl_filter_message_match(f, m);
455  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
456    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
457    failed = 1;
458  }
459 out:
460  owl_filter_delete(f);
461  if(!failed) {
462    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
463  }
464  return failed;
465}
466
467int owl_filter_regtest(void) {
468  int numfailed=0;
469  owl_message m;
470  owl_filter *f1, *f2, *f3, *f4, *f5;
471
472  owl_message_init(&m);
473  owl_message_set_type_zephyr(&m);
474  owl_message_set_direction_in(&m);
475  owl_message_set_class(&m, "owl");
476  owl_message_set_instance(&m, "tester");
477  owl_message_set_sender(&m, "owl-user");
478  owl_message_set_recipient(&m, "joe");
479  owl_message_set_attribute(&m, "foo", "bar");
480
481#define TEST_FILTER(f, e) do {                          \
482    numtests++;                                         \
483    numfailed += owl_filter_test_string(f, &m, e);      \
484      } while(0)
485
486  TEST_FILTER("true", 1);
487  TEST_FILTER("false", 0);
488  TEST_FILTER("( true )", 1);
489  TEST_FILTER("not false", 1);
490  TEST_FILTER("( true ) or ( false )", 1);
491  TEST_FILTER("true and false", 0);
492  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
493
494  TEST_FILTER("class owl", 1);
495  TEST_FILTER("class ^owl$", 1);
496  TEST_FILTER("instance test", 1);
497  TEST_FILTER("instance ^test$", 0);
498  TEST_FILTER("instance ^tester$", 1);
499
500  TEST_FILTER("foo bar", 1);
501  TEST_FILTER("class owl and instance tester", 1);
502  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
503
504  /* Order of operations and precedence */
505  TEST_FILTER("not true or false", 0);
506  TEST_FILTER("true or true and false", 0);
507  TEST_FILTER("true and true and false or true", 1);
508  TEST_FILTER("false and false or true", 1);
509  TEST_FILTER("true and false or false", 0);
510
511  f1 = owl_filter_new_fromstring("f1", "class owl");
512  owl_global_add_filter(&g, f1);
513  TEST_FILTER("filter f1", 1);
514  owl_global_remove_filter(&g, "f1");
515
516  /* Test recursion prevention */
517  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
518  owl_filter_delete(f2);
519
520  /* mutual recursion */
521  f3 = owl_filter_new_fromstring("f3", "filter f4");
522  owl_global_add_filter(&g, f3);
523  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
524  owl_global_remove_filter(&g, "f3");
525  owl_filter_delete(f4);
526
527  /* support referencing a filter several times */
528  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
529  owl_filter_delete(f5);
530
531  owl_message_cleanup(&m);
532
533  return 0;
534}
535
536int owl_editwin_regtest(void) {
537  int numfailed = 0;
538  const char *p;
539  owl_editwin *oe;
540  const char *autowrap_string = "we feel our owls should live "
541                                "closer to our ponies.";
542
543  printf("# BEGIN testing owl_editwin\n");
544
545  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
546
547  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
548
549  /* check paragraph fill */
550  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
551  owl_editwin_move_to_top(oe);
552  owl_editwin_fill_paragraph(oe);
553  p = owl_editwin_get_text(oe);
554  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
555                                                            "blah blah blah.\n"
556                                                            "\n"
557                                                            "blah"));
558
559  owl_editwin_unref(oe); oe = NULL;
560  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
561
562  /* check that lines ending with ". " correctly fill */
563  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
564  owl_editwin_move_to_top(oe);
565  owl_editwin_fill_paragraph(oe);
566  p = owl_editwin_get_text(oe);
567  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
568                                                            "blah blah blah. \n"
569                                                            "\n"
570                                                            "blah"));
571
572  owl_editwin_unref(oe); oe = NULL;
573
574  /* Test owl_editwin_move_to_beginning_of_line. */
575  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
576  owl_editwin_insert_string(oe, "\n");
577  owl_editwin_insert_string(oe, "12345678\n");
578  owl_editwin_insert_string(oe, "\n");
579  owl_editwin_insert_string(oe, "abcdefg\n");
580  owl_editwin_move_to_top(oe);
581  FAIL_UNLESS("already at beginning of line",
582              owl_editwin_move_to_beginning_of_line(oe) == 0);
583  owl_editwin_line_move(oe, 1);
584  owl_editwin_point_move(oe, 5);
585  FAIL_UNLESS("find beginning of line after empty first line",
586              owl_editwin_move_to_beginning_of_line(oe) == -5);
587  owl_editwin_line_move(oe, 1);
588  FAIL_UNLESS("find beginning empty middle line",
589              owl_editwin_move_to_beginning_of_line(oe) == 0);
590  owl_editwin_line_move(oe, 1);
591  owl_editwin_point_move(oe, 2);
592  FAIL_UNLESS("find beginning of line after empty middle line",
593              owl_editwin_move_to_beginning_of_line(oe) == -2);
594  owl_editwin_unref(oe); oe = NULL;
595
596  /* Test automatic line-wrapping. */
597  owl_global_set_edit_maxwrapcols(&g, 10);
598  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
599  for (p = autowrap_string; *p; p++) {
600    owl_input j;
601    j.ch = *p;
602    j.uch = *p; /* Assuming ASCII. */
603    owl_editwin_process_char(oe, j);
604  }
605  p = owl_editwin_get_text(oe);
606  FAIL_UNLESS("text was automatically wrapped",
607              p && !strcmp(p, "we feel\n"
608                           "our owls\n"
609                           "should\n"
610                           "live\n"
611                           "closer to\n"
612                           "our\n"
613                           "ponies."));
614  owl_editwin_unref(oe); oe = NULL;
615  owl_global_set_edit_maxwrapcols(&g, 70);
616
617  /* Test owl_editwin_current_column. */
618  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
619  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
620  owl_editwin_insert_string(oe, "abcdef");
621  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
622  owl_editwin_insert_string(oe, "\t");
623  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
624  owl_editwin_insert_string(oe, "123\n12\t3");
625  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
626  owl_editwin_move_to_beginning_of_line(oe);
627  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
628  owl_editwin_unref(oe); oe = NULL;
629
630  printf("# END testing owl_editwin (%d failures)\n", numfailed);
631
632  return numfailed;
633}
634
635int owl_fmtext_regtest(void) {
636  int numfailed = 0;
637  int start, end;
638  owl_fmtext fm1;
639  owl_fmtext fm2;
640  owl_regex re;
641  char *str;
642
643  printf("# BEGIN testing owl_fmtext\n");
644
645  owl_fmtext_init_null(&fm1);
646  owl_fmtext_init_null(&fm2);
647
648  /* Verify text gets correctly appended. */
649  owl_fmtext_append_normal(&fm1, "1234567898");
650  owl_fmtext_append_fmtext(&fm2, &fm1);
651  FAIL_UNLESS("string lengths correct",
652              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
653
654  /* Test owl_fmtext_num_lines. */
655  owl_fmtext_clear(&fm1);
656  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
657  owl_fmtext_append_normal(&fm1, "12345\n67898");
658  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
659  owl_fmtext_append_normal(&fm1, "\n");
660  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
661  owl_fmtext_append_bold(&fm1, "");
662  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
663
664  /* Test owl_fmtext_truncate_lines */
665  owl_fmtext_clear(&fm1);
666  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
667
668  owl_fmtext_clear(&fm2);
669  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
670  str = owl_fmtext_print_plain(&fm2);
671  FAIL_UNLESS("lines corrected truncated",
672              str && !strcmp(str, "1\n2\n3\n"));
673  g_free(str);
674
675  owl_fmtext_clear(&fm2);
676  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
677  str = owl_fmtext_print_plain(&fm2);
678  FAIL_UNLESS("lines corrected truncated",
679              str && !strcmp(str, "1\n2\n3\n4\n"));
680  g_free(str);
681
682  /* Test owl_fmtext_truncate_cols. */
683  owl_fmtext_clear(&fm1);
684  owl_fmtext_append_normal(&fm1, "123456789012345\n");
685  owl_fmtext_append_normal(&fm1, "123456789\n");
686  owl_fmtext_append_normal(&fm1, "1234567890\n");
687
688  owl_fmtext_clear(&fm2);
689  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
690  str = owl_fmtext_print_plain(&fm2);
691  FAIL_UNLESS("columns correctly truncated",
692              str && !strcmp(str, "567890"
693                                  "56789\n"
694                                  "567890"));
695  g_free(str);
696
697  owl_fmtext_clear(&fm1);
698  owl_fmtext_append_normal(&fm1, "12\t1234");
699  owl_fmtext_append_bold(&fm1, "56\n");
700  owl_fmtext_append_bold(&fm1, "12345678\t\n");
701
702  owl_fmtext_clear(&fm2);
703  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
704  str = owl_fmtext_print_plain(&fm2);
705  FAIL_UNLESS("columns correctly truncated",
706              str && !strcmp(str, "    123456"
707                                  "5678      "));
708  g_free(str);
709
710  /* Test owl_fmtext_expand_tabs. */
711  owl_fmtext_clear(&fm1);
712  owl_fmtext_append_normal(&fm1, "12\t1234");
713  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
714  owl_fmtext_clear(&fm2);
715  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
716  str = owl_fmtext_print_plain(&fm2);
717  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
718  FAIL_UNLESS("tabs corrected expanded",
719              str && !strcmp(str, "12      1234567 1\n"
720                                  "12345678        1"));
721  g_free(str);
722
723  owl_fmtext_clear(&fm2);
724  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
725  str = owl_fmtext_print_plain(&fm2);
726  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
727  FAIL_UNLESS("tabs corrected expanded",
728              str && !strcmp(str, "12     1234567 1\n"
729                                  "12345678       1"));
730  g_free(str);
731
732  /* Test owl_fmtext_search. */
733  owl_fmtext_clear(&fm1);
734  owl_fmtext_append_normal(&fm1, "123123123123");
735  owl_regex_create(&re, "12");
736  {
737    int count = 0, offset;
738    offset = owl_fmtext_search(&fm1, &re, 0);
739    while (offset >= 0) {
740      FAIL_UNLESS("search matches",
741                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
742      count++;
743      offset = owl_fmtext_search(&fm1, &re, offset+1);
744    }
745    FAIL_UNLESS("exactly four matches", count == 4);
746  }
747  owl_regex_cleanup(&re);
748
749  /* Test owl_fmtext_line_number. */
750  owl_fmtext_clear(&fm1);
751  owl_fmtext_append_normal(&fm1, "123\n456\n");
752  owl_fmtext_append_bold(&fm1, "");
753  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
754  FAIL_UNLESS("trailing formatting characters part of false line",
755              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
756  owl_regex_create_quoted(&re, "456");
757  FAIL_UNLESS("correctly find second line (line 1)",
758              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
759  owl_regex_cleanup(&re);
760
761  /* Test owl_fmtext_line_extents. */
762  owl_fmtext_clear(&fm1);
763  owl_fmtext_append_normal(&fm1, "123\n456\n789");
764  owl_fmtext_line_extents(&fm1, 1, &start, &end);
765  FAIL_UNLESS("line contents",
766              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
767  owl_fmtext_line_extents(&fm1, 2, &start, &end);
768  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
769
770  owl_fmtext_cleanup(&fm1);
771  owl_fmtext_cleanup(&fm2);
772
773  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
774
775  return numfailed;
776}
777
778static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
779  owl_filter *f = NULL;
780  char *filtstr = NULL;
781  int failed = 0;
782  f = owl_global_get_filter(&g, filtname);
783  if (f == NULL) {
784    printf("not ok filter missing: %s\n", filtname);
785    failed = 1;
786    goto out;
787  }
788
789  /* TODO: Come up with a better way to test this. */
790  filtstr = owl_filter_print(f);
791  if (strcmp(expected, filtstr)) {
792    printf("not ok filter incorrect: |%s| instead of |%s|\n",
793           filtstr, expected);
794    failed = 1;
795    goto out;
796  }
797
798 out:
799  g_free(filtstr);
800  return failed;
801}
802
803static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
804  char *filtname = NULL;
805  int failed = 0;
806
807  filtname = owl_function_classinstfilt(c, i, related);
808  if (filtname == NULL) {
809    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
810           related ? "related" : "not related");
811    failed = 1;
812    goto out;
813  }
814  if (owl_smartfilter_test_equals(filtname, expected)) {
815    failed = 1;
816    goto out;
817  }
818 out:
819  if (!failed) {
820    printf("ok %s\n", filtname);
821  }
822  if (filtname)
823    owl_global_remove_filter(&g, filtname);
824  g_free(filtname);
825  return failed;
826}
827
828static int owl_zuserfilt_test(const char *longuser, const char *expected) {
829  char *filtname = NULL;
830  int failed = 0;
831
832  filtname = owl_function_zuserfilt(longuser);
833  if (filtname == NULL) {
834    printf("not ok null filtname: %s\n", longuser);
835    failed = 1;
836    goto out;
837  }
838  if (owl_smartfilter_test_equals(filtname, expected)) {
839    failed = 1;
840    goto out;
841  }
842 out:
843  if (!failed) {
844    printf("ok %s\n", filtname);
845  }
846  if (filtname)
847    owl_global_remove_filter(&g, filtname);
848  g_free(filtname);
849  return failed;
850}
851
852
853int owl_smartfilter_regtest(void) {
854  int numfailed = 0;
855
856  printf("# BEGIN testing owl_smartfilter\n");
857
858  /* Check classinst making. */
859
860#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
861    numtests++;                                         \
862    numfailed += owl_classinstfilt_test(c, i, r, e);    \
863  } while (0)
864  TEST_CLASSINSTFILT("message", NULL, false,
865                     "class ^message$\n");
866  TEST_CLASSINSTFILT("message", NULL, true,
867                     "class ^(un)*message(\\.d)*$\n");
868  TEST_CLASSINSTFILT("message", "personal", false,
869                     "class ^message$ and instance ^personal$\n");
870  TEST_CLASSINSTFILT("message", "personal", true,
871                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
872
873  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
874                     "class ^message$ and instance '^evil\tinstance$'\n");
875  TEST_CLASSINSTFILT("message", "evil instance", false,
876                     "class ^message$ and instance '^evil instance$'\n");
877  TEST_CLASSINSTFILT("message", "evil'instance", false,
878                     "class ^message$ and instance \"^evil'instance$\"\n");
879  TEST_CLASSINSTFILT("message", "evil\"instance", false,
880                     "class ^message$ and instance '^evil\"instance$'\n");
881  TEST_CLASSINSTFILT("message", "evil$instance", false,
882                     "class ^message$ and instance ^evil\\$instance$\n");
883
884#define TEST_ZUSERFILT(l, e) do {                       \
885    numtests++;                                         \
886    numfailed += owl_zuserfilt_test(l, e);              \
887  } while (0)
888  TEST_ZUSERFILT("user",
889                 "( type ^zephyr$ and filter personal and "
890                 "( ( direction ^in$ and sender "
891                 "^user$"
892                 " ) or ( direction ^out$ and recipient "
893                 "^user$"
894                 " ) ) ) or ( ( class ^login$ ) and ( sender "
895                 "^user$"
896                 " ) )\n");
897  TEST_ZUSERFILT("very evil\t.user",
898                 "( type ^zephyr$ and filter personal and "
899                 "( ( direction ^in$ and sender "
900                 "'^very evil\t\\.user$'"
901                 " ) or ( direction ^out$ and recipient "
902                 "'^very evil\t\\.user$'"
903                 " ) ) ) or ( ( class ^login$ ) and ( sender "
904                 "'^very evil\t\\.user$'"
905                 " ) )\n");
906
907  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
908
909  return numfailed;
910}
911
912int owl_history_regtest(void)
913{
914  int numfailed = 0;
915  int i;
916  owl_history h;
917
918  printf("# BEGIN testing owl_history\n");
919  owl_history_init(&h);
920
921  /* Operations on empty history. */
922  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
923  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
924  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
925
926  /* Insert a few records. */
927  owl_history_store(&h, "a", false);
928  owl_history_store(&h, "b", false);
929  owl_history_store(&h, "c", false);
930  owl_history_store(&h, "d", true);
931
932  /* Walk up and down the history a bit. */
933  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
934  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
935  FAIL_UNLESS("touched", owl_history_is_touched(&h));
936  FAIL_UNLESS("next d", strcmp(owl_history_get_next(&h), "d") == 0);
937  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
938  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
939  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
940  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
941  FAIL_UNLESS("prev a", strcmp(owl_history_get_prev(&h), "a") == 0);
942  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
943
944  /* Now insert something. It should reset and blow away 'd'. */
945  owl_history_store(&h, "e", false);
946  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
947  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
948  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
949  FAIL_UNLESS("touched", owl_history_is_touched(&h));
950  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
951  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
952
953  /* Lines get de-duplicated on insert. */
954  owl_history_store(&h, "e", false);
955  owl_history_store(&h, "e", false);
956  owl_history_store(&h, "e", false);
957  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
958  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
959
960  /* But a partial is not deduplicated, as it'll go away soon. */
961  owl_history_store(&h, "e", true);
962  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
963  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
964  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
965  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
966
967  /* Reset moves to the front... */
968  owl_history_store(&h, "f", true);
969  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
970  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
971  owl_history_reset(&h);
972  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
973  /* ...and destroys any pending partial entry... */
974  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
975  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
976  /* ...but not non-partial ones. */
977  owl_history_reset(&h);
978  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
979
980  /* Finally, check we are bounded by OWL_HISTORYSIZE. */
981  for (i = 0; i < OWL_HISTORYSIZE; i++) {
982    char *string = g_strdup_printf("mango%d", i);
983    owl_history_store(&h, string, false);
984    g_free(string);
985  }
986  /* The OWL_HISTORYSIZE'th prev gets NULL. */
987  for (i = OWL_HISTORYSIZE - 2; i >= 0; i--) {
988    char *string = g_strdup_printf("mango%d", i);
989    FAIL_UNLESS("prev mango_N", strcmp(owl_history_get_prev(&h), string) == 0);
990    g_free(string);
991  }
992  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
993
994  owl_history_cleanup(&h);
995
996  printf("# END testing owl_history (%d failures)\n", numfailed);
997  return numfailed;
998}
Note: See TracBrowser for help on using the repository browser.