source: tester.c @ 396505be

release-1.10release-1.8release-1.9
Last change on this file since 396505be was 396505be, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Use owl_global_get_homedir for ~ in owl_util_makepath It's strange that ~/.zephyr.subs and ~/.owl/startup are computed differently. We should use the same one. This also matches bash in that ~ prefers $HOME over passwd. If don't have a homedir at all, this defaults to / instead of keeping the ~, but that won't happen and at least it's consistent with existing behavior for zephyr dotfiles.
  • Property mode set to 100644
File size: 33.5 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 *quoted = g_string_new("");
230  owl_string_appendf_quoted(quoted, "%q foo %q%q %s %", "hello", "world is", "can't");
231  FAIL_UNLESS("owl_string_appendf",
232              !strcmp(quoted->str, "hello foo 'world is'\"can't\" %s %"));
233  g_string_free(quoted, 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  home = owl_global_get_homedir(&g);
278  s = owl_util_makepath("~");
279  FAIL_UNLESS("makepath ~", !strcmp(home, s));
280  g_free(s);
281
282  path = g_strconcat(home, "/foo/bar/baz", NULL);
283  s = owl_util_makepath("~///foo/bar//baz");
284  FAIL_UNLESS("makepath ~///foo/bar//baz", !strcmp(path, s));
285  g_free(s);
286  g_free(path);
287
288  errno = 0;
289  pw = getpwnam("root");
290  if (pw) {
291    home = pw->pw_dir;
292  } else {
293    /* Just make some noise so we notice. */
294    home = "<WHAT>";
295    fprintf(stderr, "getpwnam: %s", errno ? strerror(errno) : "No such user");
296  }
297
298  s = owl_util_makepath("~root");
299  FAIL_UNLESS("makepath ~root", !strcmp(home, s));
300  g_free(s);
301
302  path = g_strconcat(home, "/foo/bar/baz", NULL);
303  s = owl_util_makepath("~root///foo/bar//baz");
304  FAIL_UNLESS("makepath ~root///foo/bar//baz", !strcmp(path, s));
305  g_free(s);
306  g_free(path);
307
308  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
309  printf("# END testing owl_util (%d failures)\n", numfailed);
310  return(numfailed);
311}
312
313int owl_dict_regtest(void) {
314  owl_dict d;
315  GPtrArray *l;
316  int numfailed=0;
317  char *av = g_strdup("aval"), *bv = g_strdup("bval"), *cv = g_strdup("cval"),
318    *dv = g_strdup("dval");
319
320  printf("# BEGIN testing owl_dict\n");
321  owl_dict_create(&d);
322  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
323  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
324  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
325  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
326  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
327  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
328  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
329  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
330  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
331  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
332  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
333  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
334
335  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
336  l = owl_dict_get_keys(&d);
337  FAIL_UNLESS("get_keys result size", 3 == l->len);
338 
339  /* these assume the returned keys are sorted */
340  FAIL_UNLESS("get_keys result val", 0 == strcmp("a", l->pdata[0]));
341  FAIL_UNLESS("get_keys result val", 0 == strcmp("b", l->pdata[1]));
342  FAIL_UNLESS("get_keys result val", 0 == strcmp("c", l->pdata[2]));
343
344  owl_ptr_array_free(l, g_free);
345  owl_dict_cleanup(&d, NULL);
346
347  g_free(av);
348  g_free(bv);
349  g_free(cv);
350  g_free(dv);
351
352  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
353  printf("# END testing owl_dict (%d failures)\n", numfailed);
354  return(numfailed);
355}
356
357int owl_variable_regtest(void) {
358  owl_vardict vd;
359  owl_variable *var;
360  int numfailed=0;
361  char *value;
362  const void *v;
363
364  printf("# BEGIN testing owl_variable\n");
365  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
366
367  FAIL_UNLESS("get bool var", NULL != (var = owl_variable_get_var(&vd, "rxping")));
368  FAIL_UNLESS("get bool", 0 == owl_variable_get_bool(var));
369  FAIL_UNLESS("get bool (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
370  FAIL_UNLESS("get bool as string",
371              !strcmp((value = owl_variable_get_tostring(var)), "off"));
372  g_free(value);
373  FAIL_UNLESS("set bool 1", 0 == owl_variable_set_bool_on(var));
374  FAIL_UNLESS("get bool 2", 1 == owl_variable_get_bool(var));
375  FAIL_UNLESS("set bool 3", 0 == owl_variable_set_fromstring(var, "off", 0));
376  FAIL_UNLESS("get bool 4", 0 == owl_variable_get_bool(var));
377  FAIL_UNLESS("set bool 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
378  FAIL_UNLESS("get bool 6", 0 == owl_variable_get_bool(var));
379
380
381  FAIL_UNLESS("get string var", NULL != (var = owl_variable_get_var(&vd, "logpath")));
382  FAIL_UNLESS("get string", 0 == strcmp("~/zlog/people", owl_variable_get_string(var)));
383  FAIL_UNLESS("set string 7", 0 == owl_variable_set_string(var, "whee"));
384  FAIL_UNLESS("get string", !strcmp("whee", owl_variable_get_string(var)));
385
386  FAIL_UNLESS("get int var", NULL != (var = owl_variable_get_var(&vd, "typewinsize")));
387  FAIL_UNLESS("get int", 8 == owl_variable_get_int(var));
388  FAIL_UNLESS("get int (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
389  FAIL_UNLESS("get int as string",
390              !strcmp((value = owl_variable_get_tostring(var)), "8"));
391  g_free(value);
392  FAIL_UNLESS("set int 1", 0 == owl_variable_set_int(var, 12));
393  FAIL_UNLESS("get int 2", 12 == owl_variable_get_int(var));
394  FAIL_UNLESS("set int 1b", -1 == owl_variable_set_int(var, -3));
395  FAIL_UNLESS("get int 2b", 12 == owl_variable_get_int(var));
396  FAIL_UNLESS("set int 3", 0 == owl_variable_set_fromstring(var, "9", 0));
397  FAIL_UNLESS("get int 4", 9 == owl_variable_get_int(var));
398  FAIL_UNLESS("set int 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
399  FAIL_UNLESS("set int 6", -1 == owl_variable_set_fromstring(var, "", 0));
400  FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var));
401
402  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
403  FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar")));
404  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(var)));
405  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var)));
406  owl_variable_set_string(var, "new val");
407  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var)));
408
409  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
410  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
411  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var)));
412  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
413  owl_variable_set_int(var, 17);
414  FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var));
415
416  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
417  FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar")));
418  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(var)));
419  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
420  owl_variable_set_bool_off(var);
421  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
422
423  owl_variable_dict_newvar_string(&vd, "nullstringvar", "", "", NULL);
424  FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar")));
425  FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var)));
426  g_free(value);
427  var = owl_variable_get_var(&vd, "zsigproc");
428  FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var)));
429  g_free(value);
430
431  owl_variable_dict_cleanup(&vd);
432
433  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
434  printf("# END testing owl_variable (%d failures)\n", numfailed);
435  return(numfailed);
436}
437
438static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
439{
440  owl_filter *f;
441  int ok;
442  int failed = 0;
443  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
444    printf("not ok can't parse %s\n", filt);
445    failed = 1;
446    goto out;
447  }
448  ok = owl_filter_message_match(f, m);
449  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
450    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
451    failed = 1;
452  }
453 out:
454  owl_filter_delete(f);
455  if(!failed) {
456    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
457  }
458  return failed;
459}
460
461int owl_filter_regtest(void) {
462  int numfailed=0;
463  owl_message m;
464  owl_filter *f1, *f2, *f3, *f4, *f5;
465
466  owl_message_init(&m);
467  owl_message_set_type_zephyr(&m);
468  owl_message_set_direction_in(&m);
469  owl_message_set_class(&m, "owl");
470  owl_message_set_instance(&m, "tester");
471  owl_message_set_sender(&m, "owl-user");
472  owl_message_set_recipient(&m, "joe");
473  owl_message_set_attribute(&m, "foo", "bar");
474
475#define TEST_FILTER(f, e) do {                          \
476    numtests++;                                         \
477    numfailed += owl_filter_test_string(f, &m, e);      \
478      } while(0)
479
480  TEST_FILTER("true", 1);
481  TEST_FILTER("false", 0);
482  TEST_FILTER("( true )", 1);
483  TEST_FILTER("not false", 1);
484  TEST_FILTER("( true ) or ( false )", 1);
485  TEST_FILTER("true and false", 0);
486  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
487
488  TEST_FILTER("class owl", 1);
489  TEST_FILTER("class ^owl$", 1);
490  TEST_FILTER("instance test", 1);
491  TEST_FILTER("instance ^test$", 0);
492  TEST_FILTER("instance ^tester$", 1);
493
494  TEST_FILTER("foo bar", 1);
495  TEST_FILTER("class owl and instance tester", 1);
496  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
497
498  /* Order of operations and precedence */
499  TEST_FILTER("not true or false", 0);
500  TEST_FILTER("true or true and false", 0);
501  TEST_FILTER("true and true and false or true", 1);
502  TEST_FILTER("false and false or true", 1);
503  TEST_FILTER("true and false or false", 0);
504
505  f1 = owl_filter_new_fromstring("f1", "class owl");
506  owl_global_add_filter(&g, f1);
507  TEST_FILTER("filter f1", 1);
508  owl_global_remove_filter(&g, "f1");
509
510  /* Test recursion prevention */
511  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
512  owl_filter_delete(f2);
513
514  /* mutual recursion */
515  f3 = owl_filter_new_fromstring("f3", "filter f4");
516  owl_global_add_filter(&g, f3);
517  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
518  owl_global_remove_filter(&g, "f3");
519  owl_filter_delete(f4);
520
521  /* support referencing a filter several times */
522  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
523  owl_filter_delete(f5);
524
525  return 0;
526}
527
528int owl_editwin_regtest(void) {
529  int numfailed = 0;
530  const char *p;
531  owl_editwin *oe;
532  const char *autowrap_string = "we feel our owls should live "
533                                "closer to our ponies.";
534
535  printf("# BEGIN testing owl_editwin\n");
536
537  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
538
539  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
540
541  /* check paragraph fill */
542  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
543  owl_editwin_move_to_top(oe);
544  owl_editwin_fill_paragraph(oe);
545  p = owl_editwin_get_text(oe);
546  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
547                                                            "blah blah blah.\n"
548                                                            "\n"
549                                                            "blah"));
550
551  owl_editwin_unref(oe); oe = NULL;
552  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
553
554  /* check that lines ending with ". " correctly fill */
555  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
556  owl_editwin_move_to_top(oe);
557  owl_editwin_fill_paragraph(oe);
558  p = owl_editwin_get_text(oe);
559  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
560                                                            "blah blah blah. \n"
561                                                            "\n"
562                                                            "blah"));
563
564  owl_editwin_unref(oe); oe = NULL;
565
566  /* Test owl_editwin_move_to_beginning_of_line. */
567  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
568  owl_editwin_insert_string(oe, "\n");
569  owl_editwin_insert_string(oe, "12345678\n");
570  owl_editwin_insert_string(oe, "\n");
571  owl_editwin_insert_string(oe, "abcdefg\n");
572  owl_editwin_move_to_top(oe);
573  FAIL_UNLESS("already at beginning of line",
574              owl_editwin_move_to_beginning_of_line(oe) == 0);
575  owl_editwin_line_move(oe, 1);
576  owl_editwin_point_move(oe, 5);
577  FAIL_UNLESS("find beginning of line after empty first line",
578              owl_editwin_move_to_beginning_of_line(oe) == -5);
579  owl_editwin_line_move(oe, 1);
580  FAIL_UNLESS("find beginning empty middle line",
581              owl_editwin_move_to_beginning_of_line(oe) == 0);
582  owl_editwin_line_move(oe, 1);
583  owl_editwin_point_move(oe, 2);
584  FAIL_UNLESS("find beginning of line after empty middle line",
585              owl_editwin_move_to_beginning_of_line(oe) == -2);
586  owl_editwin_unref(oe); oe = NULL;
587
588  /* Test automatic line-wrapping. */
589  owl_global_set_edit_maxwrapcols(&g, 10);
590  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
591  for (p = autowrap_string; *p; p++) {
592    owl_input j;
593    j.ch = *p;
594    j.uch = *p; /* Assuming ASCII. */
595    owl_editwin_process_char(oe, j);
596  }
597  p = owl_editwin_get_text(oe);
598  FAIL_UNLESS("text was automatically wrapped",
599              p && !strcmp(p, "we feel\n"
600                           "our owls\n"
601                           "should\n"
602                           "live\n"
603                           "closer to\n"
604                           "our\n"
605                           "ponies."));
606  owl_editwin_unref(oe); oe = NULL;
607  owl_global_set_edit_maxwrapcols(&g, 70);
608
609  /* Test owl_editwin_current_column. */
610  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
611  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
612  owl_editwin_insert_string(oe, "abcdef");
613  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
614  owl_editwin_insert_string(oe, "\t");
615  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
616  owl_editwin_insert_string(oe, "123\n12\t3");
617  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
618  owl_editwin_move_to_beginning_of_line(oe);
619  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
620  owl_editwin_unref(oe); oe = NULL;
621
622  printf("# END testing owl_editwin (%d failures)\n", numfailed);
623
624  return numfailed;
625}
626
627int owl_fmtext_regtest(void) {
628  int numfailed = 0;
629  int start, end;
630  owl_fmtext fm1;
631  owl_fmtext fm2;
632  owl_regex re;
633  char *str;
634
635  printf("# BEGIN testing owl_fmtext\n");
636
637  owl_fmtext_init_null(&fm1);
638  owl_fmtext_init_null(&fm2);
639
640  /* Verify text gets correctly appended. */
641  owl_fmtext_append_normal(&fm1, "1234567898");
642  owl_fmtext_append_fmtext(&fm2, &fm1);
643  FAIL_UNLESS("string lengths correct",
644              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
645
646  /* Test owl_fmtext_num_lines. */
647  owl_fmtext_clear(&fm1);
648  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
649  owl_fmtext_append_normal(&fm1, "12345\n67898");
650  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
651  owl_fmtext_append_normal(&fm1, "\n");
652  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
653  owl_fmtext_append_bold(&fm1, "");
654  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
655
656  /* Test owl_fmtext_truncate_lines */
657  owl_fmtext_clear(&fm1);
658  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
659
660  owl_fmtext_clear(&fm2);
661  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
662  str = owl_fmtext_print_plain(&fm2);
663  FAIL_UNLESS("lines corrected truncated",
664              str && !strcmp(str, "1\n2\n3\n"));
665  g_free(str);
666
667  owl_fmtext_clear(&fm2);
668  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
669  str = owl_fmtext_print_plain(&fm2);
670  FAIL_UNLESS("lines corrected truncated",
671              str && !strcmp(str, "1\n2\n3\n4\n"));
672  g_free(str);
673
674  /* Test owl_fmtext_truncate_cols. */
675  owl_fmtext_clear(&fm1);
676  owl_fmtext_append_normal(&fm1, "123456789012345\n");
677  owl_fmtext_append_normal(&fm1, "123456789\n");
678  owl_fmtext_append_normal(&fm1, "1234567890\n");
679
680  owl_fmtext_clear(&fm2);
681  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
682  str = owl_fmtext_print_plain(&fm2);
683  FAIL_UNLESS("columns correctly truncated",
684              str && !strcmp(str, "567890"
685                                  "56789\n"
686                                  "567890"));
687  g_free(str);
688
689  owl_fmtext_clear(&fm1);
690  owl_fmtext_append_normal(&fm1, "12\t1234");
691  owl_fmtext_append_bold(&fm1, "56\n");
692  owl_fmtext_append_bold(&fm1, "12345678\t\n");
693
694  owl_fmtext_clear(&fm2);
695  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
696  str = owl_fmtext_print_plain(&fm2);
697  FAIL_UNLESS("columns correctly truncated",
698              str && !strcmp(str, "    123456"
699                                  "5678      "));
700  g_free(str);
701
702  /* Test owl_fmtext_expand_tabs. */
703  owl_fmtext_clear(&fm1);
704  owl_fmtext_append_normal(&fm1, "12\t1234");
705  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
706  owl_fmtext_clear(&fm2);
707  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
708  str = owl_fmtext_print_plain(&fm2);
709  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
710  FAIL_UNLESS("tabs corrected expanded",
711              str && !strcmp(str, "12      1234567 1\n"
712                                  "12345678        1"));
713  g_free(str);
714
715  owl_fmtext_clear(&fm2);
716  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
717  str = owl_fmtext_print_plain(&fm2);
718  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
719  FAIL_UNLESS("tabs corrected expanded",
720              str && !strcmp(str, "12     1234567 1\n"
721                                  "12345678       1"));
722  g_free(str);
723
724  /* Test owl_fmtext_search. */
725  owl_fmtext_clear(&fm1);
726  owl_fmtext_append_normal(&fm1, "123123123123");
727  owl_regex_create(&re, "12");
728  {
729    int count = 0, offset;
730    offset = owl_fmtext_search(&fm1, &re, 0);
731    while (offset >= 0) {
732      FAIL_UNLESS("search matches",
733                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
734      count++;
735      offset = owl_fmtext_search(&fm1, &re, offset+1);
736    }
737    FAIL_UNLESS("exactly four matches", count == 4);
738  }
739  owl_regex_cleanup(&re);
740
741  /* Test owl_fmtext_line_number. */
742  owl_fmtext_clear(&fm1);
743  owl_fmtext_append_normal(&fm1, "123\n456\n");
744  owl_fmtext_append_bold(&fm1, "");
745  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
746  FAIL_UNLESS("trailing formatting characters part of false line",
747              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
748  owl_regex_create_quoted(&re, "456");
749  FAIL_UNLESS("correctly find second line (line 1)",
750              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
751  owl_regex_cleanup(&re);
752
753  /* Test owl_fmtext_line_extents. */
754  owl_fmtext_clear(&fm1);
755  owl_fmtext_append_normal(&fm1, "123\n456\n789");
756  owl_fmtext_line_extents(&fm1, 1, &start, &end);
757  FAIL_UNLESS("line contents",
758              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
759  owl_fmtext_line_extents(&fm1, 2, &start, &end);
760  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
761
762  owl_fmtext_cleanup(&fm1);
763  owl_fmtext_cleanup(&fm2);
764
765  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
766
767  return numfailed;
768}
769
770static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
771  owl_filter *f = NULL;
772  char *filtstr = NULL;
773  int failed = 0;
774  f = owl_global_get_filter(&g, filtname);
775  if (f == NULL) {
776    printf("not ok filter missing: %s\n", filtname);
777    failed = 1;
778    goto out;
779  }
780
781  /* TODO: Come up with a better way to test this. */
782  filtstr = owl_filter_print(f);
783  if (strcmp(expected, filtstr)) {
784    printf("not ok filter incorrect: |%s| instead of |%s|\n",
785           filtstr, expected);
786    failed = 1;
787    goto out;
788  }
789
790 out:
791  g_free(filtstr);
792  return failed;
793}
794
795static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
796  char *filtname = NULL;
797  int failed = 0;
798
799  filtname = owl_function_classinstfilt(c, i, related);
800  if (filtname == NULL) {
801    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
802           related ? "related" : "not related");
803    failed = 1;
804    goto out;
805  }
806  if (owl_smartfilter_test_equals(filtname, expected)) {
807    failed = 1;
808    goto out;
809  }
810 out:
811  if (!failed) {
812    printf("ok %s\n", filtname);
813  }
814  if (filtname)
815    owl_global_remove_filter(&g, filtname);
816  g_free(filtname);
817  return failed;
818}
819
820static int owl_zuserfilt_test(const char *longuser, const char *expected) {
821  char *filtname = NULL;
822  int failed = 0;
823
824  filtname = owl_function_zuserfilt(longuser);
825  if (filtname == NULL) {
826    printf("not ok null filtname: %s\n", longuser);
827    failed = 1;
828    goto out;
829  }
830  if (owl_smartfilter_test_equals(filtname, expected)) {
831    failed = 1;
832    goto out;
833  }
834 out:
835  if (!failed) {
836    printf("ok %s\n", filtname);
837  }
838  if (filtname)
839    owl_global_remove_filter(&g, filtname);
840  g_free(filtname);
841  return failed;
842}
843
844
845int owl_smartfilter_regtest(void) {
846  int numfailed = 0;
847
848  printf("# BEGIN testing owl_smartfilter\n");
849
850  /* Check classinst making. */
851
852#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
853    numtests++;                                         \
854    numfailed += owl_classinstfilt_test(c, i, r, e);    \
855  } while (0)
856  TEST_CLASSINSTFILT("message", NULL, false,
857                     "class ^message$\n");
858  TEST_CLASSINSTFILT("message", NULL, true,
859                     "class ^(un)*message(\\.d)*$\n");
860  TEST_CLASSINSTFILT("message", "personal", false,
861                     "class ^message$ and instance ^personal$\n");
862  TEST_CLASSINSTFILT("message", "personal", true,
863                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
864
865  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
866                     "class ^message$ and instance '^evil\tinstance$'\n");
867  TEST_CLASSINSTFILT("message", "evil instance", false,
868                     "class ^message$ and instance '^evil instance$'\n");
869  TEST_CLASSINSTFILT("message", "evil'instance", false,
870                     "class ^message$ and instance \"^evil'instance$\"\n");
871  TEST_CLASSINSTFILT("message", "evil\"instance", false,
872                     "class ^message$ and instance '^evil\"instance$'\n");
873  TEST_CLASSINSTFILT("message", "evil$instance", false,
874                     "class ^message$ and instance ^evil\\$instance$\n");
875
876#define TEST_ZUSERFILT(l, e) do {                       \
877    numtests++;                                         \
878    numfailed += owl_zuserfilt_test(l, e);              \
879  } while (0)
880  TEST_ZUSERFILT("user",
881                 "( type ^zephyr$ and filter personal and "
882                 "( ( direction ^in$ and sender "
883                 "^user$"
884                 " ) or ( direction ^out$ and recipient "
885                 "^user$"
886                 " ) ) ) or ( ( class ^login$ ) and ( sender "
887                 "^user$"
888                 " ) )\n");
889  TEST_ZUSERFILT("very evil\t.user",
890                 "( type ^zephyr$ and filter personal and "
891                 "( ( direction ^in$ and sender "
892                 "'^very evil\t\\.user$'"
893                 " ) or ( direction ^out$ and recipient "
894                 "'^very evil\t\\.user$'"
895                 " ) ) ) or ( ( class ^login$ ) and ( sender "
896                 "'^very evil\t\\.user$'"
897                 " ) )\n");
898
899  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
900
901  return numfailed;
902}
903
904int owl_history_regtest(void)
905{
906  int numfailed = 0;
907  int i;
908  owl_history h;
909
910  printf("# BEGIN testing owl_history\n");
911  owl_history_init(&h);
912
913  /* Operations on empty history. */
914  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
915  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
916  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
917
918  /* Insert a few records. */
919  owl_history_store(&h, "a", false);
920  owl_history_store(&h, "b", false);
921  owl_history_store(&h, "c", false);
922  owl_history_store(&h, "d", true);
923
924  /* Walk up and down the history a bit. */
925  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
926  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
927  FAIL_UNLESS("touched", owl_history_is_touched(&h));
928  FAIL_UNLESS("next d", strcmp(owl_history_get_next(&h), "d") == 0);
929  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
930  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
931  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
932  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
933  FAIL_UNLESS("prev a", strcmp(owl_history_get_prev(&h), "a") == 0);
934  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
935
936  /* Now insert something. It should reset and blow away 'd'. */
937  owl_history_store(&h, "e", false);
938  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
939  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
940  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
941  FAIL_UNLESS("touched", owl_history_is_touched(&h));
942  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
943  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
944
945  /* Lines get de-duplicated on insert. */
946  owl_history_store(&h, "e", false);
947  owl_history_store(&h, "e", false);
948  owl_history_store(&h, "e", false);
949  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
950  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
951
952  /* But a partial is not deduplicated, as it'll go away soon. */
953  owl_history_store(&h, "e", true);
954  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
955  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
956  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
957  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
958
959  /* Reset moves to the front... */
960  owl_history_store(&h, "f", 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  owl_history_reset(&h);
964  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
965  /* ...and destroys any pending partial entry... */
966  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
967  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
968  /* ...but not non-partial ones. */
969  owl_history_reset(&h);
970  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
971
972  /* Finally, check we are bounded by OWL_HISTORYSIZE. */
973  for (i = 0; i < OWL_HISTORYSIZE; i++) {
974    char *string = g_strdup_printf("mango%d", i);
975    owl_history_store(&h, string, false);
976    g_free(string);
977  }
978  /* The OWL_HISTORYSIZE'th prev gets NULL. */
979  for (i = OWL_HISTORYSIZE - 2; i >= 0; i--) {
980    char *string = g_strdup_printf("mango%d", i);
981    FAIL_UNLESS("prev mango_N", strcmp(owl_history_get_prev(&h), string) == 0);
982    g_free(string);
983  }
984  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
985
986  owl_history_cleanup(&h);
987
988  printf("# END testing owl_history (%d failures)\n", numfailed);
989  return numfailed;
990}
Note: See TracBrowser for help on using the repository browser.