source: tester.c @ 7feba19

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