source: tester.c @ e89ec48

release-1.10release-1.9
Last change on this file since e89ec48 was a74a044, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Free the owl_message created for testing It's safe to do so now.
  • Property mode set to 100644
File size: 33.4 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");
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_int(&vd, "intvar", "", "", 47);
400  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
401  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var)));
402  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
403  owl_variable_set_int(var, 17);
404  FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var));
405
406  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
407  FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar")));
408  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(var)));
409  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
410  owl_variable_set_bool_off(var);
411  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
412
413  owl_variable_dict_newvar_string(&vd, "nullstringvar", "", "", NULL);
414  FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar")));
415  FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var)));
416  g_free(value);
417  var = owl_variable_get_var(&vd, "zsigproc");
418  FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var)));
419  g_free(value);
420
421  owl_variable_dict_cleanup(&vd);
422
423  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
424  printf("# END testing owl_variable (%d failures)\n", numfailed);
425  return(numfailed);
426}
427
428static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
429{
430  owl_filter *f;
431  int ok;
432  int failed = 0;
433  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
434    printf("not ok can't parse %s\n", filt);
435    failed = 1;
436    goto out;
437  }
438  ok = owl_filter_message_match(f, m);
439  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
440    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
441    failed = 1;
442  }
443 out:
444  owl_filter_delete(f);
445  if(!failed) {
446    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
447  }
448  return failed;
449}
450
451int owl_filter_regtest(void) {
452  int numfailed=0;
453  owl_message m;
454  owl_filter *f1, *f2, *f3, *f4, *f5;
455
456  owl_message_init(&m);
457  owl_message_set_type_zephyr(&m);
458  owl_message_set_direction_in(&m);
459  owl_message_set_class(&m, "owl");
460  owl_message_set_instance(&m, "tester");
461  owl_message_set_sender(&m, "owl-user");
462  owl_message_set_recipient(&m, "joe");
463  owl_message_set_attribute(&m, "foo", "bar");
464
465#define TEST_FILTER(f, e) do {                          \
466    numtests++;                                         \
467    numfailed += owl_filter_test_string(f, &m, e);      \
468      } while(0)
469
470  TEST_FILTER("true", 1);
471  TEST_FILTER("false", 0);
472  TEST_FILTER("( true )", 1);
473  TEST_FILTER("not false", 1);
474  TEST_FILTER("( true ) or ( false )", 1);
475  TEST_FILTER("true and false", 0);
476  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
477
478  TEST_FILTER("class owl", 1);
479  TEST_FILTER("class ^owl$", 1);
480  TEST_FILTER("instance test", 1);
481  TEST_FILTER("instance ^test$", 0);
482  TEST_FILTER("instance ^tester$", 1);
483
484  TEST_FILTER("foo bar", 1);
485  TEST_FILTER("class owl and instance tester", 1);
486  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
487
488  /* Order of operations and precedence */
489  TEST_FILTER("not true or false", 0);
490  TEST_FILTER("true or true and false", 0);
491  TEST_FILTER("true and true and false or true", 1);
492  TEST_FILTER("false and false or true", 1);
493  TEST_FILTER("true and false or false", 0);
494
495  f1 = owl_filter_new_fromstring("f1", "class owl");
496  owl_global_add_filter(&g, f1);
497  TEST_FILTER("filter f1", 1);
498  owl_global_remove_filter(&g, "f1");
499
500  /* Test recursion prevention */
501  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
502  owl_filter_delete(f2);
503
504  /* mutual recursion */
505  f3 = owl_filter_new_fromstring("f3", "filter f4");
506  owl_global_add_filter(&g, f3);
507  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
508  owl_global_remove_filter(&g, "f3");
509  owl_filter_delete(f4);
510
511  /* support referencing a filter several times */
512  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
513  owl_filter_delete(f5);
514
515  owl_message_cleanup(&m);
516
517  return 0;
518}
519
520int owl_editwin_regtest(void) {
521  int numfailed = 0;
522  const char *p;
523  owl_editwin *oe;
524  const char *autowrap_string = "we feel our owls should live "
525                                "closer to our ponies.";
526
527  printf("# BEGIN testing owl_editwin\n");
528
529  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
530
531  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
532
533  /* check paragraph fill */
534  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
535  owl_editwin_move_to_top(oe);
536  owl_editwin_fill_paragraph(oe);
537  p = owl_editwin_get_text(oe);
538  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
539                                                            "blah blah blah.\n"
540                                                            "\n"
541                                                            "blah"));
542
543  owl_editwin_unref(oe); oe = NULL;
544  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
545
546  /* check that lines ending with ". " correctly fill */
547  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
548  owl_editwin_move_to_top(oe);
549  owl_editwin_fill_paragraph(oe);
550  p = owl_editwin_get_text(oe);
551  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
552                                                            "blah blah blah. \n"
553                                                            "\n"
554                                                            "blah"));
555
556  owl_editwin_unref(oe); oe = NULL;
557
558  /* Test owl_editwin_move_to_beginning_of_line. */
559  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
560  owl_editwin_insert_string(oe, "\n");
561  owl_editwin_insert_string(oe, "12345678\n");
562  owl_editwin_insert_string(oe, "\n");
563  owl_editwin_insert_string(oe, "abcdefg\n");
564  owl_editwin_move_to_top(oe);
565  FAIL_UNLESS("already at beginning of line",
566              owl_editwin_move_to_beginning_of_line(oe) == 0);
567  owl_editwin_line_move(oe, 1);
568  owl_editwin_point_move(oe, 5);
569  FAIL_UNLESS("find beginning of line after empty first line",
570              owl_editwin_move_to_beginning_of_line(oe) == -5);
571  owl_editwin_line_move(oe, 1);
572  FAIL_UNLESS("find beginning empty middle line",
573              owl_editwin_move_to_beginning_of_line(oe) == 0);
574  owl_editwin_line_move(oe, 1);
575  owl_editwin_point_move(oe, 2);
576  FAIL_UNLESS("find beginning of line after empty middle line",
577              owl_editwin_move_to_beginning_of_line(oe) == -2);
578  owl_editwin_unref(oe); oe = NULL;
579
580  /* Test automatic line-wrapping. */
581  owl_global_set_edit_maxwrapcols(&g, 10);
582  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
583  for (p = autowrap_string; *p; p++) {
584    owl_input j;
585    j.ch = *p;
586    j.uch = *p; /* Assuming ASCII. */
587    owl_editwin_process_char(oe, j);
588  }
589  p = owl_editwin_get_text(oe);
590  FAIL_UNLESS("text was automatically wrapped",
591              p && !strcmp(p, "we feel\n"
592                           "our owls\n"
593                           "should\n"
594                           "live\n"
595                           "closer to\n"
596                           "our\n"
597                           "ponies."));
598  owl_editwin_unref(oe); oe = NULL;
599  owl_global_set_edit_maxwrapcols(&g, 70);
600
601  /* Test owl_editwin_current_column. */
602  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
603  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
604  owl_editwin_insert_string(oe, "abcdef");
605  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
606  owl_editwin_insert_string(oe, "\t");
607  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
608  owl_editwin_insert_string(oe, "123\n12\t3");
609  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
610  owl_editwin_move_to_beginning_of_line(oe);
611  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
612  owl_editwin_unref(oe); oe = NULL;
613
614  printf("# END testing owl_editwin (%d failures)\n", numfailed);
615
616  return numfailed;
617}
618
619int owl_fmtext_regtest(void) {
620  int numfailed = 0;
621  int start, end;
622  owl_fmtext fm1;
623  owl_fmtext fm2;
624  owl_regex re;
625  char *str;
626
627  printf("# BEGIN testing owl_fmtext\n");
628
629  owl_fmtext_init_null(&fm1);
630  owl_fmtext_init_null(&fm2);
631
632  /* Verify text gets correctly appended. */
633  owl_fmtext_append_normal(&fm1, "1234567898");
634  owl_fmtext_append_fmtext(&fm2, &fm1);
635  FAIL_UNLESS("string lengths correct",
636              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
637
638  /* Test owl_fmtext_num_lines. */
639  owl_fmtext_clear(&fm1);
640  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
641  owl_fmtext_append_normal(&fm1, "12345\n67898");
642  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
643  owl_fmtext_append_normal(&fm1, "\n");
644  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
645  owl_fmtext_append_bold(&fm1, "");
646  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
647
648  /* Test owl_fmtext_truncate_lines */
649  owl_fmtext_clear(&fm1);
650  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
651
652  owl_fmtext_clear(&fm2);
653  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
654  str = owl_fmtext_print_plain(&fm2);
655  FAIL_UNLESS("lines corrected truncated",
656              str && !strcmp(str, "1\n2\n3\n"));
657  g_free(str);
658
659  owl_fmtext_clear(&fm2);
660  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
661  str = owl_fmtext_print_plain(&fm2);
662  FAIL_UNLESS("lines corrected truncated",
663              str && !strcmp(str, "1\n2\n3\n4\n"));
664  g_free(str);
665
666  /* Test owl_fmtext_truncate_cols. */
667  owl_fmtext_clear(&fm1);
668  owl_fmtext_append_normal(&fm1, "123456789012345\n");
669  owl_fmtext_append_normal(&fm1, "123456789\n");
670  owl_fmtext_append_normal(&fm1, "1234567890\n");
671
672  owl_fmtext_clear(&fm2);
673  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
674  str = owl_fmtext_print_plain(&fm2);
675  FAIL_UNLESS("columns correctly truncated",
676              str && !strcmp(str, "567890"
677                                  "56789\n"
678                                  "567890"));
679  g_free(str);
680
681  owl_fmtext_clear(&fm1);
682  owl_fmtext_append_normal(&fm1, "12\t1234");
683  owl_fmtext_append_bold(&fm1, "56\n");
684  owl_fmtext_append_bold(&fm1, "12345678\t\n");
685
686  owl_fmtext_clear(&fm2);
687  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
688  str = owl_fmtext_print_plain(&fm2);
689  FAIL_UNLESS("columns correctly truncated",
690              str && !strcmp(str, "    123456"
691                                  "5678      "));
692  g_free(str);
693
694  /* Test owl_fmtext_expand_tabs. */
695  owl_fmtext_clear(&fm1);
696  owl_fmtext_append_normal(&fm1, "12\t1234");
697  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
698  owl_fmtext_clear(&fm2);
699  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
700  str = owl_fmtext_print_plain(&fm2);
701  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
702  FAIL_UNLESS("tabs corrected expanded",
703              str && !strcmp(str, "12      1234567 1\n"
704                                  "12345678        1"));
705  g_free(str);
706
707  owl_fmtext_clear(&fm2);
708  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
709  str = owl_fmtext_print_plain(&fm2);
710  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
711  FAIL_UNLESS("tabs corrected expanded",
712              str && !strcmp(str, "12     1234567 1\n"
713                                  "12345678       1"));
714  g_free(str);
715
716  /* Test owl_fmtext_search. */
717  owl_fmtext_clear(&fm1);
718  owl_fmtext_append_normal(&fm1, "123123123123");
719  owl_regex_create(&re, "12");
720  {
721    int count = 0, offset;
722    offset = owl_fmtext_search(&fm1, &re, 0);
723    while (offset >= 0) {
724      FAIL_UNLESS("search matches",
725                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
726      count++;
727      offset = owl_fmtext_search(&fm1, &re, offset+1);
728    }
729    FAIL_UNLESS("exactly four matches", count == 4);
730  }
731  owl_regex_cleanup(&re);
732
733  /* Test owl_fmtext_line_number. */
734  owl_fmtext_clear(&fm1);
735  owl_fmtext_append_normal(&fm1, "123\n456\n");
736  owl_fmtext_append_bold(&fm1, "");
737  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
738  FAIL_UNLESS("trailing formatting characters part of false line",
739              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
740  owl_regex_create_quoted(&re, "456");
741  FAIL_UNLESS("correctly find second line (line 1)",
742              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
743  owl_regex_cleanup(&re);
744
745  /* Test owl_fmtext_line_extents. */
746  owl_fmtext_clear(&fm1);
747  owl_fmtext_append_normal(&fm1, "123\n456\n789");
748  owl_fmtext_line_extents(&fm1, 1, &start, &end);
749  FAIL_UNLESS("line contents",
750              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
751  owl_fmtext_line_extents(&fm1, 2, &start, &end);
752  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
753
754  owl_fmtext_cleanup(&fm1);
755  owl_fmtext_cleanup(&fm2);
756
757  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
758
759  return numfailed;
760}
761
762static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
763  owl_filter *f = NULL;
764  char *filtstr = NULL;
765  int failed = 0;
766  f = owl_global_get_filter(&g, filtname);
767  if (f == NULL) {
768    printf("not ok filter missing: %s\n", filtname);
769    failed = 1;
770    goto out;
771  }
772
773  /* TODO: Come up with a better way to test this. */
774  filtstr = owl_filter_print(f);
775  if (strcmp(expected, filtstr)) {
776    printf("not ok filter incorrect: |%s| instead of |%s|\n",
777           filtstr, expected);
778    failed = 1;
779    goto out;
780  }
781
782 out:
783  g_free(filtstr);
784  return failed;
785}
786
787static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
788  char *filtname = NULL;
789  int failed = 0;
790
791  filtname = owl_function_classinstfilt(c, i, related);
792  if (filtname == NULL) {
793    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
794           related ? "related" : "not related");
795    failed = 1;
796    goto out;
797  }
798  if (owl_smartfilter_test_equals(filtname, expected)) {
799    failed = 1;
800    goto out;
801  }
802 out:
803  if (!failed) {
804    printf("ok %s\n", filtname);
805  }
806  if (filtname)
807    owl_global_remove_filter(&g, filtname);
808  g_free(filtname);
809  return failed;
810}
811
812static int owl_zuserfilt_test(const char *longuser, const char *expected) {
813  char *filtname = NULL;
814  int failed = 0;
815
816  filtname = owl_function_zuserfilt(longuser);
817  if (filtname == NULL) {
818    printf("not ok null filtname: %s\n", longuser);
819    failed = 1;
820    goto out;
821  }
822  if (owl_smartfilter_test_equals(filtname, expected)) {
823    failed = 1;
824    goto out;
825  }
826 out:
827  if (!failed) {
828    printf("ok %s\n", filtname);
829  }
830  if (filtname)
831    owl_global_remove_filter(&g, filtname);
832  g_free(filtname);
833  return failed;
834}
835
836
837int owl_smartfilter_regtest(void) {
838  int numfailed = 0;
839
840  printf("# BEGIN testing owl_smartfilter\n");
841
842  /* Check classinst making. */
843
844#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
845    numtests++;                                         \
846    numfailed += owl_classinstfilt_test(c, i, r, e);    \
847  } while (0)
848  TEST_CLASSINSTFILT("message", NULL, false,
849                     "class ^message$\n");
850  TEST_CLASSINSTFILT("message", NULL, true,
851                     "class ^(un)*message(\\.d)*$\n");
852  TEST_CLASSINSTFILT("message", "personal", false,
853                     "class ^message$ and instance ^personal$\n");
854  TEST_CLASSINSTFILT("message", "personal", true,
855                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
856
857  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
858                     "class ^message$ and instance '^evil\tinstance$'\n");
859  TEST_CLASSINSTFILT("message", "evil instance", false,
860                     "class ^message$ and instance '^evil instance$'\n");
861  TEST_CLASSINSTFILT("message", "evil'instance", false,
862                     "class ^message$ and instance \"^evil'instance$\"\n");
863  TEST_CLASSINSTFILT("message", "evil\"instance", false,
864                     "class ^message$ and instance '^evil\"instance$'\n");
865  TEST_CLASSINSTFILT("message", "evil$instance", false,
866                     "class ^message$ and instance ^evil\\$instance$\n");
867
868#define TEST_ZUSERFILT(l, e) do {                       \
869    numtests++;                                         \
870    numfailed += owl_zuserfilt_test(l, e);              \
871  } while (0)
872  TEST_ZUSERFILT("user",
873                 "( type ^zephyr$ and filter personal and "
874                 "( ( direction ^in$ and sender "
875                 "^user$"
876                 " ) or ( direction ^out$ and recipient "
877                 "^user$"
878                 " ) ) ) or ( ( class ^login$ ) and ( sender "
879                 "^user$"
880                 " ) )\n");
881  TEST_ZUSERFILT("very evil\t.user",
882                 "( type ^zephyr$ and filter personal and "
883                 "( ( direction ^in$ and sender "
884                 "'^very evil\t\\.user$'"
885                 " ) or ( direction ^out$ and recipient "
886                 "'^very evil\t\\.user$'"
887                 " ) ) ) or ( ( class ^login$ ) and ( sender "
888                 "'^very evil\t\\.user$'"
889                 " ) )\n");
890
891  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
892
893  return numfailed;
894}
895
896int owl_history_regtest(void)
897{
898  int numfailed = 0;
899  int i;
900  owl_history h;
901
902  printf("# BEGIN testing owl_history\n");
903  owl_history_init(&h);
904
905  /* Operations on empty history. */
906  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
907  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
908  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
909
910  /* Insert a few records. */
911  owl_history_store(&h, "a", false);
912  owl_history_store(&h, "b", false);
913  owl_history_store(&h, "c", false);
914  owl_history_store(&h, "d", true);
915
916  /* Walk up and down the history a bit. */
917  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
918  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
919  FAIL_UNLESS("touched", owl_history_is_touched(&h));
920  FAIL_UNLESS("next d", strcmp(owl_history_get_next(&h), "d") == 0);
921  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
922  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
923  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
924  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
925  FAIL_UNLESS("prev a", strcmp(owl_history_get_prev(&h), "a") == 0);
926  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
927
928  /* Now insert something. It should reset and blow away 'd'. */
929  owl_history_store(&h, "e", false);
930  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
931  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
932  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
933  FAIL_UNLESS("touched", owl_history_is_touched(&h));
934  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
935  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
936
937  /* Lines get de-duplicated on insert. */
938  owl_history_store(&h, "e", false);
939  owl_history_store(&h, "e", false);
940  owl_history_store(&h, "e", false);
941  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
942  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
943
944  /* But a partial is not deduplicated, as it'll go away soon. */
945  owl_history_store(&h, "e", true);
946  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
947  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
948  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
949  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
950
951  /* Reset moves to the front... */
952  owl_history_store(&h, "f", true);
953  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
954  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
955  owl_history_reset(&h);
956  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
957  /* ...and destroys any pending partial entry... */
958  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
959  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
960  /* ...but not non-partial ones. */
961  owl_history_reset(&h);
962  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
963
964  /* Finally, check we are bounded by OWL_HISTORYSIZE. */
965  for (i = 0; i < OWL_HISTORYSIZE; i++) {
966    char *string = g_strdup_printf("mango%d", i);
967    owl_history_store(&h, string, false);
968    g_free(string);
969  }
970  /* The OWL_HISTORYSIZE'th prev gets NULL. */
971  for (i = OWL_HISTORYSIZE - 2; i >= 0; i--) {
972    char *string = g_strdup_printf("mango%d", i);
973    FAIL_UNLESS("prev mango_N", strcmp(owl_history_get_prev(&h), string) == 0);
974    g_free(string);
975  }
976  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
977
978  owl_history_cleanup(&h);
979
980  printf("# END testing owl_history (%d failures)\n", numfailed);
981  return numfailed;
982}
Note: See TracBrowser for help on using the repository browser.