source: tester.c @ fa981f3

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