source: tester.c @ 4e37d56

release-1.8release-1.9
Last change on this file since 4e37d56 was 4e37d56, checked in by Anders Kaseorg <andersk@mit.edu>, 10 years ago
owl_dict_regtest: Copy and free dict values Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 27.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);
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  if (numfailures) {
115      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
116  }
117  printf("1..%d\n", numtests);
118
119  return(numfailures);
120}
121
122#define FAIL_UNLESS(desc,pred) do { int __pred = (pred);                \
123    numtests++;                                                         \
124    printf("%s %s", (__pred)?"ok":(numfailed++,"not ok"), desc);        \
125    if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0)
126
127
128int owl_util_regtest(void)
129{
130  int numfailed=0;
131
132  printf("# BEGIN testing owl_util\n");
133
134#define CHECK_STR_AND_FREE(desc, expected, expr)         \
135    do {                                                 \
136      char *__value = (expr);                            \
137      FAIL_UNLESS((desc), !strcmp((expected), __value)); \
138      g_free(__value);                                 \
139    } while (0)
140
141  CHECK_STR_AND_FREE("owl_text_substitute 2", "fYZYZ",
142                     owl_text_substitute("foo", "o", "YZ"));
143  CHECK_STR_AND_FREE("owl_text_substitute 3", "foo",
144                     owl_text_substitute("fYZYZ", "YZ", "o"));
145  CHECK_STR_AND_FREE("owl_text_substitute 4", "/u/foo/meep",
146                     owl_text_substitute("~/meep", "~", "/u/foo"));
147
148  FAIL_UNLESS("skiptokens 1", 
149              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
150  FAIL_UNLESS("skiptokens 2", 
151              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
152
153  CHECK_STR_AND_FREE("expand_tabs 1", "        hi", owl_text_expand_tabs("\thi"));
154
155  CHECK_STR_AND_FREE("expand_tabs 2", "        hi\nword    tab",
156                     owl_text_expand_tabs("\thi\nword\ttab"));
157
158  CHECK_STR_AND_FREE("expand_tabs 3", "                2 tabs",
159                     owl_text_expand_tabs("\t\t2 tabs"));
160  CHECK_STR_AND_FREE("expand_tabs 4", "α       ααααααα!        ",
161                     owl_text_expand_tabs(\tααααααα!\t"));
162  CHECK_STR_AND_FREE("expand_tabs 5", "A      AAA!!        ",
163                     owl_text_expand_tabs("A\tAAA!!\t"));
164
165  FAIL_UNLESS("skiptokens 1",
166              !strcmp("world", skiptokens("hello world", 1)));
167
168  FAIL_UNLESS("skiptokens 2",
169              !strcmp("c d e", skiptokens("a   b c d e", 2)));
170
171  FAIL_UNLESS("skiptokens 3",
172              !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
173
174  FAIL_UNLESS("skiptokens 4",
175              !strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
176
177  FAIL_UNLESS("skiptokens 5",
178              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
179
180#define CHECK_QUOTING(desc, unquoted, quoted)           \
181  do {                                                  \
182      int __argc;                                       \
183      char *__quoted = owl_arg_quote(unquoted);         \
184      char **__argv;                                    \
185      FAIL_UNLESS(desc, !strcmp(quoted, __quoted));     \
186      __argv = owl_parseline(__quoted, &__argc);        \
187      FAIL_UNLESS(desc " - arg count", __argc == 1);    \
188      FAIL_UNLESS(desc " - null-terminated",            \
189                  __argv[__argc] == NULL);              \
190      FAIL_UNLESS(desc " - parsed",                     \
191                  !strcmp(__argv[0], unquoted));        \
192      g_strfreev(__argv);                               \
193      g_free(__quoted);                         \
194    } while (0)
195
196  CHECK_QUOTING("boring text", "mango", "mango");
197  CHECK_QUOTING("spaces", "mangos are tasty", "'mangos are tasty'");
198  CHECK_QUOTING("single quotes", "mango's", "\"mango's\"");
199  CHECK_QUOTING("double quotes", "he said \"mangos are tasty\"",
200                "'he said \"mangos are tasty\"'");
201  CHECK_QUOTING("both quotes",
202                "he said \"mango's are tasty even when you put in "
203                "a random apostrophe\"",
204                "\"he said \"'\"'\"mango's are tasty even when you put in "
205                "a random apostrophe\"'\"'\"\"");
206  CHECK_QUOTING("quote monster", "'\"\"'\"'''\"",
207                "\""
208                "'"
209                "\"'\"'\""
210                "\"'\"'\""
211                "'"
212                "\"'\"'\""
213                "'"
214                "'"
215                "'"
216                "\"'\"'\""
217                "\"");
218
219  GString *g = g_string_new("");
220  owl_string_appendf_quoted(g, "%q foo %q%q %s %", "hello", "world is", "can't");
221  FAIL_UNLESS("owl_string_appendf",
222              !strcmp(g->str, "hello foo 'world is'\"can't\" %s %"));
223  g_string_free(g, true);
224
225  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
226  printf("# END testing owl_util (%d failures)\n", numfailed);
227  return(numfailed);
228}
229
230int owl_dict_regtest(void) {
231  owl_dict d;
232  owl_list l;
233  int numfailed=0;
234  char *av = g_strdup("aval"), *bv = g_strdup("bval"), *cv = g_strdup("cval"),
235    *dv = g_strdup("dval");
236
237  printf("# BEGIN testing owl_dict\n");
238  owl_dict_create(&d);
239  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
240  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
241  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
242  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
243  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
244  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
245  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
246  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
247  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
248  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
249  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
250  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
251
252  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
253  owl_list_create(&l);
254  owl_dict_get_keys(&d, &l);
255  FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l));
256 
257  /* these assume the returned keys are sorted */
258  FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0)));
259  FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1)));
260  FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2)));
261
262  owl_list_cleanup(&l, g_free);
263  owl_dict_cleanup(&d, NULL);
264
265  g_free(av);
266  g_free(bv);
267  g_free(cv);
268  g_free(dv);
269
270  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
271  printf("# END testing owl_dict (%d failures)\n", numfailed);
272  return(numfailed);
273}
274
275int owl_variable_regtest(void) {
276  owl_vardict vd;
277  int numfailed=0;
278  char *value;
279  const void *v;
280
281  printf("# BEGIN testing owl_variable\n");
282  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
283
284  FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping"));
285  FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
286  FAIL_UNLESS("get bool as string",
287              !strcmp((value = owl_variable_get_tostring(&vd,"rxping")), "off"));
288  g_free(value);
289  FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
290  FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
291  FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0));
292  FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping"));
293  FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0));
294  FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping"));
295
296
297  FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
298  FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
299  FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
300
301  FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
302  FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
303  FAIL_UNLESS("get int as string",
304              !strcmp((value = owl_variable_get_tostring(&vd,"typewinsize")), "8"));
305  g_free(value);
306  FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
307  FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
308  FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
309  FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
310  FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0));
311  FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
312  FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0));
313  FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0));
314  FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
315
316  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
317  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar", OWL_VARIABLE_STRING)));
318  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar")));
319  owl_variable_set_string(&vd, "stringvar", "new val");
320  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar")));
321
322  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
323  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar", OWL_VARIABLE_INT)));
324  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar"));
325  owl_variable_set_int(&vd, "intvar", 17);
326  FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar"));
327
328  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
329  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar", OWL_VARIABLE_BOOL)));
330  FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar"));
331  owl_variable_set_bool_off(&vd, "boolvar");
332  FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar"));
333
334  owl_variable_dict_cleanup(&vd);
335
336  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
337  printf("# END testing owl_variable (%d failures)\n", numfailed);
338  return(numfailed);
339}
340
341static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
342{
343  owl_filter *f;
344  int ok;
345  int failed = 0;
346  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
347    printf("not ok can't parse %s\n", filt);
348    failed = 1;
349    goto out;
350  }
351  ok = owl_filter_message_match(f, m);
352  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
353    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
354    failed = 1;
355  }
356 out:
357  owl_filter_delete(f);
358  if(!failed) {
359    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
360  }
361  return failed;
362}
363
364int owl_filter_regtest(void) {
365  int numfailed=0;
366  owl_message m;
367  owl_filter *f1, *f2, *f3, *f4, *f5;
368
369  owl_message_init(&m);
370  owl_message_set_type_zephyr(&m);
371  owl_message_set_direction_in(&m);
372  owl_message_set_class(&m, "owl");
373  owl_message_set_instance(&m, "tester");
374  owl_message_set_sender(&m, "owl-user");
375  owl_message_set_recipient(&m, "joe");
376  owl_message_set_attribute(&m, "foo", "bar");
377
378#define TEST_FILTER(f, e) do {                          \
379    numtests++;                                         \
380    numfailed += owl_filter_test_string(f, &m, e);      \
381      } while(0)
382
383  TEST_FILTER("true", 1);
384  TEST_FILTER("false", 0);
385  TEST_FILTER("( true )", 1);
386  TEST_FILTER("not false", 1);
387  TEST_FILTER("( true ) or ( false )", 1);
388  TEST_FILTER("true and false", 0);
389  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
390
391  TEST_FILTER("class owl", 1);
392  TEST_FILTER("class ^owl$", 1);
393  TEST_FILTER("instance test", 1);
394  TEST_FILTER("instance ^test$", 0);
395  TEST_FILTER("instance ^tester$", 1);
396
397  TEST_FILTER("foo bar", 1);
398  TEST_FILTER("class owl and instance tester", 1);
399  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
400
401  /* Order of operations and precedence */
402  TEST_FILTER("not true or false", 0);
403  TEST_FILTER("true or true and false", 0);
404  TEST_FILTER("true and true and false or true", 1);
405  TEST_FILTER("false and false or true", 1);
406  TEST_FILTER("true and false or false", 0);
407
408  f1 = owl_filter_new_fromstring("f1", "class owl");
409  owl_global_add_filter(&g, f1);
410  TEST_FILTER("filter f1", 1);
411  owl_global_remove_filter(&g, "f1");
412
413  /* Test recursion prevention */
414  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
415  owl_filter_delete(f2);
416
417  /* mutual recursion */
418  f3 = owl_filter_new_fromstring("f3", "filter f4");
419  owl_global_add_filter(&g, f3);
420  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
421  owl_global_remove_filter(&g, "f3");
422  owl_filter_delete(f4);
423
424  /* support referencing a filter several times */
425  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
426  owl_filter_delete(f5);
427
428  return 0;
429}
430
431int owl_editwin_regtest(void) {
432  int numfailed = 0;
433  const char *p;
434  owl_editwin *oe;
435  const char *autowrap_string = "we feel our owls should live "
436                                "closer to our ponies.";
437
438  printf("# BEGIN testing owl_editwin\n");
439
440  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
441
442  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
443
444  /* check paragraph fill */
445  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
446  owl_editwin_move_to_top(oe);
447  owl_editwin_fill_paragraph(oe);
448  p = owl_editwin_get_text(oe);
449  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
450                                                            "blah blah blah.\n"
451                                                            "\n"
452                                                            "blah"));
453
454  owl_editwin_unref(oe); oe = NULL;
455  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
456
457  /* check that lines ending with ". " correctly fill */
458  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
459  owl_editwin_move_to_top(oe);
460  owl_editwin_fill_paragraph(oe);
461  p = owl_editwin_get_text(oe);
462  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
463                                                            "blah blah blah. \n"
464                                                            "\n"
465                                                            "blah"));
466
467  owl_editwin_unref(oe); oe = NULL;
468
469  /* Test owl_editwin_move_to_beginning_of_line. */
470  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
471  owl_editwin_insert_string(oe, "\n");
472  owl_editwin_insert_string(oe, "12345678\n");
473  owl_editwin_insert_string(oe, "\n");
474  owl_editwin_insert_string(oe, "abcdefg\n");
475  owl_editwin_move_to_top(oe);
476  FAIL_UNLESS("already at beginning of line",
477              owl_editwin_move_to_beginning_of_line(oe) == 0);
478  owl_editwin_line_move(oe, 1);
479  owl_editwin_point_move(oe, 5);
480  FAIL_UNLESS("find beginning of line after empty first line",
481              owl_editwin_move_to_beginning_of_line(oe) == -5);
482  owl_editwin_line_move(oe, 1);
483  FAIL_UNLESS("find beginning empty middle line",
484              owl_editwin_move_to_beginning_of_line(oe) == 0);
485  owl_editwin_line_move(oe, 1);
486  owl_editwin_point_move(oe, 2);
487  FAIL_UNLESS("find beginning of line after empty middle line",
488              owl_editwin_move_to_beginning_of_line(oe) == -2);
489  owl_editwin_unref(oe); oe = NULL;
490
491  /* Test automatic line-wrapping. */
492  owl_global_set_edit_maxwrapcols(&g, 10);
493  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
494  for (p = autowrap_string; *p; p++) {
495    owl_input j;
496    j.ch = *p;
497    j.uch = *p; /* Assuming ASCII. */
498    owl_editwin_process_char(oe, j);
499  }
500  p = owl_editwin_get_text(oe);
501  FAIL_UNLESS("text was automatically wrapped",
502              p && !strcmp(p, "we feel\n"
503                           "our owls\n"
504                           "should\n"
505                           "live\n"
506                           "closer to\n"
507                           "our\n"
508                           "ponies."));
509  owl_editwin_unref(oe); oe = NULL;
510  owl_global_set_edit_maxwrapcols(&g, 70);
511
512  /* Test owl_editwin_current_column. */
513  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
514  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
515  owl_editwin_insert_string(oe, "abcdef");
516  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
517  owl_editwin_insert_string(oe, "\t");
518  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
519  owl_editwin_insert_string(oe, "123\n12\t3");
520  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
521  owl_editwin_move_to_beginning_of_line(oe);
522  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
523  owl_editwin_unref(oe); oe = NULL;
524
525  printf("# END testing owl_editwin (%d failures)\n", numfailed);
526
527  return numfailed;
528}
529
530int owl_fmtext_regtest(void) {
531  int numfailed = 0;
532  int start, end;
533  owl_fmtext fm1;
534  owl_fmtext fm2;
535  owl_regex re;
536  char *str;
537
538  printf("# BEGIN testing owl_fmtext\n");
539
540  owl_fmtext_init_null(&fm1);
541  owl_fmtext_init_null(&fm2);
542
543  /* Verify text gets correctly appended. */
544  owl_fmtext_append_normal(&fm1, "1234567898");
545  owl_fmtext_append_fmtext(&fm2, &fm1);
546  FAIL_UNLESS("string lengths correct",
547              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
548
549  /* Test owl_fmtext_num_lines. */
550  owl_fmtext_clear(&fm1);
551  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
552  owl_fmtext_append_normal(&fm1, "12345\n67898");
553  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
554  owl_fmtext_append_normal(&fm1, "\n");
555  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
556  owl_fmtext_append_bold(&fm1, "");
557  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
558
559  /* Test owl_fmtext_truncate_lines */
560  owl_fmtext_clear(&fm1);
561  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
562
563  owl_fmtext_clear(&fm2);
564  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
565  str = owl_fmtext_print_plain(&fm2);
566  FAIL_UNLESS("lines corrected truncated",
567              str && !strcmp(str, "1\n2\n3\n"));
568  g_free(str);
569
570  owl_fmtext_clear(&fm2);
571  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
572  str = owl_fmtext_print_plain(&fm2);
573  FAIL_UNLESS("lines corrected truncated",
574              str && !strcmp(str, "1\n2\n3\n4\n"));
575  g_free(str);
576
577  /* Test owl_fmtext_truncate_cols. */
578  owl_fmtext_clear(&fm1);
579  owl_fmtext_append_normal(&fm1, "123456789012345\n");
580  owl_fmtext_append_normal(&fm1, "123456789\n");
581  owl_fmtext_append_normal(&fm1, "1234567890\n");
582
583  owl_fmtext_clear(&fm2);
584  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
585  str = owl_fmtext_print_plain(&fm2);
586  FAIL_UNLESS("columns correctly truncated",
587              str && !strcmp(str, "567890"
588                                  "56789\n"
589                                  "567890"));
590  g_free(str);
591
592  owl_fmtext_clear(&fm1);
593  owl_fmtext_append_normal(&fm1, "12\t1234");
594  owl_fmtext_append_bold(&fm1, "56\n");
595  owl_fmtext_append_bold(&fm1, "12345678\t\n");
596
597  owl_fmtext_clear(&fm2);
598  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
599  str = owl_fmtext_print_plain(&fm2);
600  FAIL_UNLESS("columns correctly truncated",
601              str && !strcmp(str, "    123456"
602                                  "5678      "));
603  g_free(str);
604
605  /* Test owl_fmtext_expand_tabs. */
606  owl_fmtext_clear(&fm1);
607  owl_fmtext_append_normal(&fm1, "12\t1234");
608  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
609  owl_fmtext_clear(&fm2);
610  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
611  str = owl_fmtext_print_plain(&fm2);
612  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
613  FAIL_UNLESS("tabs corrected expanded",
614              str && !strcmp(str, "12      1234567 1\n"
615                                  "12345678        1"));
616  g_free(str);
617
618  owl_fmtext_clear(&fm2);
619  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
620  str = owl_fmtext_print_plain(&fm2);
621  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
622  FAIL_UNLESS("tabs corrected expanded",
623              str && !strcmp(str, "12     1234567 1\n"
624                                  "12345678       1"));
625  g_free(str);
626
627  /* Test owl_fmtext_search. */
628  owl_fmtext_clear(&fm1);
629  owl_fmtext_append_normal(&fm1, "123123123123");
630  owl_regex_create(&re, "12");
631  {
632    int count = 0, offset;
633    offset = owl_fmtext_search(&fm1, &re, 0);
634    while (offset >= 0) {
635      FAIL_UNLESS("search matches",
636                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
637      count++;
638      offset = owl_fmtext_search(&fm1, &re, offset+1);
639    }
640    FAIL_UNLESS("exactly four matches", count == 4);
641  }
642  owl_regex_cleanup(&re);
643
644  /* Test owl_fmtext_line_number. */
645  owl_fmtext_clear(&fm1);
646  owl_fmtext_append_normal(&fm1, "123\n456\n");
647  owl_fmtext_append_bold(&fm1, "");
648  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
649  FAIL_UNLESS("trailing formatting characters part of false line",
650              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
651  owl_regex_create_quoted(&re, "456");
652  FAIL_UNLESS("correctly find second line (line 1)",
653              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
654  owl_regex_cleanup(&re);
655
656  /* Test owl_fmtext_line_extents. */
657  owl_fmtext_clear(&fm1);
658  owl_fmtext_append_normal(&fm1, "123\n456\n789");
659  owl_fmtext_line_extents(&fm1, 1, &start, &end);
660  FAIL_UNLESS("line contents",
661              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
662  owl_fmtext_line_extents(&fm1, 2, &start, &end);
663  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
664
665  owl_fmtext_cleanup(&fm1);
666  owl_fmtext_cleanup(&fm2);
667
668  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
669
670  return numfailed;
671}
672
673static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
674  owl_filter *f = NULL;
675  char *filtstr = NULL;
676  int failed = 0;
677  f = owl_global_get_filter(&g, filtname);
678  if (f == NULL) {
679    printf("not ok filter missing: %s\n", filtname);
680    failed = 1;
681    goto out;
682  }
683
684  /* TODO: Come up with a better way to test this. */
685  filtstr = owl_filter_print(f);
686  if (strcmp(expected, filtstr)) {
687    printf("not ok filter incorrect: |%s| instead of |%s|\n",
688           filtstr, expected);
689    failed = 1;
690    goto out;
691  }
692
693 out:
694  g_free(filtstr);
695  return failed;
696}
697
698static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
699  char *filtname = NULL;
700  int failed = 0;
701
702  filtname = owl_function_classinstfilt(c, i, related);
703  if (filtname == NULL) {
704    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
705           related ? "related" : "not related");
706    failed = 1;
707    goto out;
708  }
709  if (owl_smartfilter_test_equals(filtname, expected)) {
710    failed = 1;
711    goto out;
712  }
713 out:
714  if (!failed) {
715    printf("ok %s\n", filtname);
716  }
717  if (filtname)
718    owl_global_remove_filter(&g, filtname);
719  g_free(filtname);
720  return failed;
721}
722
723static int owl_zuserfilt_test(const char *longuser, const char *expected) {
724  char *filtname = NULL;
725  int failed = 0;
726
727  filtname = owl_function_zuserfilt(longuser);
728  if (filtname == NULL) {
729    printf("not ok null filtname: %s\n", longuser);
730    failed = 1;
731    goto out;
732  }
733  if (owl_smartfilter_test_equals(filtname, expected)) {
734    failed = 1;
735    goto out;
736  }
737 out:
738  if (!failed) {
739    printf("ok %s\n", filtname);
740  }
741  if (filtname)
742    owl_global_remove_filter(&g, filtname);
743  g_free(filtname);
744  return failed;
745}
746
747
748int owl_smartfilter_regtest(void) {
749  int numfailed = 0;
750
751  printf("# BEGIN testing owl_smartfilter\n");
752
753  /* Check classinst making. */
754
755#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
756    numtests++;                                         \
757    numfailed += owl_classinstfilt_test(c, i, r, e);    \
758  } while (0)
759  TEST_CLASSINSTFILT("message", NULL, false,
760                     "class ^message$\n");
761  TEST_CLASSINSTFILT("message", NULL, true,
762                     "class ^(un)*message(\\.d)*$\n");
763  TEST_CLASSINSTFILT("message", "personal", false,
764                     "class ^message$ and instance ^personal$\n");
765  TEST_CLASSINSTFILT("message", "personal", true,
766                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
767
768  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
769                     "class ^message$ and instance '^evil\tinstance$'\n");
770  TEST_CLASSINSTFILT("message", "evil instance", false,
771                     "class ^message$ and instance '^evil instance$'\n");
772  TEST_CLASSINSTFILT("message", "evil'instance", false,
773                     "class ^message$ and instance \"^evil'instance$\"\n");
774  TEST_CLASSINSTFILT("message", "evil\"instance", false,
775                     "class ^message$ and instance '^evil\"instance$'\n");
776  TEST_CLASSINSTFILT("message", "evil$instance", false,
777                     "class ^message$ and instance ^evil\\$instance$\n");
778
779#define TEST_ZUSERFILT(l, e) do {                       \
780    numtests++;                                         \
781    numfailed += owl_zuserfilt_test(l, e);              \
782  } while (0)
783  TEST_ZUSERFILT("user",
784                 "( type ^zephyr$ and filter personal and "
785                 "( ( direction ^in$ and sender "
786                 "^user$"
787                 " ) or ( direction ^out$ and recipient "
788                 "^user$"
789                 " ) ) ) or ( ( class ^login$ ) and ( sender "
790                 "^user$"
791                 " ) )\n");
792  TEST_ZUSERFILT("very evil\t.user",
793                 "( type ^zephyr$ and filter personal and "
794                 "( ( direction ^in$ and sender "
795                 "'^very evil\t\\.user$'"
796                 " ) or ( direction ^out$ and recipient "
797                 "'^very evil\t\\.user$'"
798                 " ) ) ) or ( ( class ^login$ ) and ( sender "
799                 "'^very evil\t\\.user$'"
800                 " ) )\n");
801
802  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
803
804  return numfailed;
805}
Note: See TracBrowser for help on using the repository browser.