source: tester.c @ 1bdbcb1

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