source: tester.c @ b8f7962

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