source: tester.c @ bc1d648

release-1.8release-1.9
Last change on this file since bc1d648 was bc1d648, checked in by Jason Gross <jgross@mit.edu>, 10 years ago
Drop require_type It's not doing much for us (and would do even less after the next commit). I'm not sure whether or not we should type-check owl_variable_get_*
  • Property mode set to 100644
File size: 30.5 KB
RevLine 
[5aa33fd]1#define OWL_PERL
2#define WINDOW FAKE_WINDOW
[7d4fbcd]3#include "owl.h"
[5aa33fd]4#undef WINDOW
5
[7d4fbcd]6#include <unistd.h>
7#include <stdlib.h>
8
[5aa33fd]9#undef instr
10#include <curses.h>
11
[b2b0773]12owl_global g;
13
[a2b3289]14int numtests;
[1cf3f8d3]15
[c2673ab]16int owl_regtest(void);
[8bce750]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);
[4cc02605]22int owl_editwin_regtest(void);
[6772d19]23int owl_fmtext_regtest(void);
[b31252d]24int owl_smartfilter_regtest(void);
[25891a8]25int owl_history_regtest(void);
[8bce750]26
[5aa33fd]27extern void owl_perl_xs_init(pTHX);
28
[094009a]29int main(int argc, char **argv, char **env)
30{
[95414bf]31  FILE *rnull;
32  FILE *wnull;
[c2673ab]33  char *perlerr;
34  int status = 0;
[e93dd78]35  SCREEN *screen;
[95414bf]36
37  if (argc <= 1) {
38    fprintf(stderr, "Usage: %s --builtin|TEST.t|-le CODE\n", argv[0]);
39    return 1;
40  }
41
[f034ac0]42  /* initialize a fake ncurses, detached from std{in,out} */
[95414bf]43  wnull = fopen("/dev/null", "w");
44  rnull = fopen("/dev/null", "r");
[e93dd78]45  screen = newterm("xterm", wnull, rnull);
[f034ac0]46  /* initialize global structures */
47  owl_global_init(&g);
[124aebc]48
[c2673ab]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  }
[98d7757]56
[c2673ab]57  owl_global_complete_setup(&g);
[04b16f8]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"));
[22e02cd]63
[98d7757]64  owl_function_firstmsg();
[c2673ab]65
[95414bf]66  ENTER;
67  SAVETMPS;
[5aa33fd]68
[95414bf]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]);
[5aa33fd]81
[95414bf]82    eval_pv("do $main::test_prog; die($@) if($@)", true);
83  }
[5aa33fd]84
[95414bf]85  status = 0;
[5aa33fd]86
[95414bf]87  FREETMPS;
88  LEAVE;
[c2673ab]89
90 out:
[5aa33fd]91  perl_destruct(owl_global_get_perlinterp(&g));
92  perl_free(owl_global_get_perlinterp(&g));
[c2673ab]93  /* probably not necessary, but tear down the screen */
94  endwin();
[e93dd78]95  delscreen(screen);
[c2673ab]96  fclose(rnull);
97  fclose(wnull);
98  return status;
99}
100
101int owl_regtest(void) {
[a2b3289]102  numtests = 0;
[7d4fbcd]103  int numfailures=0;
[a2b3289]104  /*
105    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
106    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
107  */
[3381399]108  numfailures += owl_util_regtest();
109  numfailures += owl_dict_regtest();
110  numfailures += owl_variable_regtest();
111  numfailures += owl_filter_regtest();
[4cc02605]112  numfailures += owl_editwin_regtest();
[6772d19]113  numfailures += owl_fmtext_regtest();
[b31252d]114  numfailures += owl_smartfilter_regtest();
[25891a8]115  numfailures += owl_history_regtest();
[3381399]116  if (numfailures) {
[1cf3f8d3]117      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
[7d4fbcd]118  }
[a2b3289]119  printf("1..%d\n", numtests);
[f034ac0]120
[3381399]121  return(numfailures);
[7d4fbcd]122}
[8bce750]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
[a04218c]136#define CHECK_STR_AND_FREE(desc, expected, expr)         \
137    do {                                                 \
138      char *__value = (expr);                            \
139      FAIL_UNLESS((desc), !strcmp((expected), __value)); \
[ddbbcffa]140      g_free(__value);                                 \
[a04218c]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"));
[8bce750]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
[a04218c]155  CHECK_STR_AND_FREE("expand_tabs 1", "        hi", owl_text_expand_tabs("\thi"));
[8bce750]156
[a04218c]157  CHECK_STR_AND_FREE("expand_tabs 2", "        hi\nword    tab",
158                     owl_text_expand_tabs("\thi\nword\ttab"));
[8bce750]159
[a04218c]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"));
[8bce750]166
[e30ed92]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
[2bc6ad35]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);    \
[65c753e]190      FAIL_UNLESS(desc " - null-terminated",            \
191                  __argv[__argc] == NULL);              \
[2bc6ad35]192      FAIL_UNLESS(desc " - parsed",                     \
193                  !strcmp(__argv[0], unquoted));        \
[e56303f]194      g_strfreev(__argv);                               \
[ddbbcffa]195      g_free(__quoted);                         \
[2bc6ad35]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
[f47696f]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",
[596c8b10]224              !strcmp(g->str, "hello foo 'world is'\"can't\" %s %"));
225  g_string_free(g, true);
[f47696f]226
[8bce750]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;
[ce68f23]234  GPtrArray *l;
[8bce750]235  int numfailed=0;
[4e37d56]236  char *av = g_strdup("aval"), *bv = g_strdup("bval"), *cv = g_strdup("cval"),
237    *dv = g_strdup("dval");
[8bce750]238
239  printf("# BEGIN testing owl_dict\n");
[4c7c21f]240  owl_dict_create(&d);
[a1074de]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));
[8bce750]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));
[ce68f23]255  l = owl_dict_get_keys(&d);
256  FAIL_UNLESS("get_keys result size", 3 == l->len);
[8bce750]257 
258  /* these assume the returned keys are sorted */
[ce68f23]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]));
[8bce750]262
[ce68f23]263  owl_ptr_array_free(l, g_free);
[bf7aa1d]264  owl_dict_cleanup(&d, NULL);
[8bce750]265
[4e37d56]266  g_free(av);
267  g_free(bv);
268  g_free(cv);
269  g_free(dv);
270
[8bce750]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;
[010a951]279  char *value;
[8bce750]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"));
[010a951]287  FAIL_UNLESS("get bool as string",
288              !strcmp((value = owl_variable_get_tostring(&vd,"rxping")), "off"));
289  g_free(value);
[8bce750]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"));
[010a951]304  FAIL_UNLESS("get int as string",
305              !strcmp((value = owl_variable_get_tostring(&vd,"typewinsize")), "8"));
306  g_free(value);
[8bce750]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");
[bc1d648]318  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar")));
[8bce750]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);
[bc1d648]324  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar")));
[8bce750]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);
[bc1d648]330  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar")));
[8bce750]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
[0fef6eb]335  owl_variable_dict_cleanup(&vd);
[8bce750]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
[e9c6fc8]342static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
343{
[23fddad]344  owl_filter *f;
[8bce750]345  int ok;
346  int failed = 0;
[23fddad]347  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
[8bce750]348    printf("not ok can't parse %s\n", filt);
349    failed = 1;
350    goto out;
351  }
[23fddad]352  ok = owl_filter_message_match(f, m);
[8bce750]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:
[23fddad]358  owl_filter_delete(f);
[8bce750]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;
[23fddad]368  owl_filter *f1, *f2, *f3, *f4, *f5;
[8bce750]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
[23fddad]409  f1 = owl_filter_new_fromstring("f1", "class owl");
410  owl_global_add_filter(&g, f1);
[8bce750]411  TEST_FILTER("filter f1", 1);
[23fddad]412  owl_global_remove_filter(&g, "f1");
[8bce750]413
414  /* Test recursion prevention */
[23fddad]415  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
416  owl_filter_delete(f2);
[8bce750]417
418  /* mutual recursion */
[23fddad]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);
[8bce750]424
425  /* support referencing a filter several times */
[23fddad]426  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
427  owl_filter_delete(f5);
[8bce750]428
429  return 0;
430}
431
[4cc02605]432int owl_editwin_regtest(void) {
433  int numfailed = 0;
434  const char *p;
[35a30f9]435  owl_editwin *oe;
[58a16cc]436  const char *autowrap_string = "we feel our owls should live "
437                                "closer to our ponies.";
[4cc02605]438
439  printf("# BEGIN testing owl_editwin\n");
440
[4123da1]441  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
[4cc02605]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
[9190285]455  owl_editwin_unref(oe); oe = NULL;
[4123da1]456  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
457
[4cc02605]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
[9190285]468  owl_editwin_unref(oe); oe = NULL;
[4cc02605]469
[e5c9d3de]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);
[9190285]490  owl_editwin_unref(oe); oe = NULL;
[e5c9d3de]491
[58a16cc]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
[ac6d4e4]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
[4cc02605]526  printf("# END testing owl_editwin (%d failures)\n", numfailed);
527
528  return numfailed;
529}
[6772d19]530
531int owl_fmtext_regtest(void) {
532  int numfailed = 0;
[7ba2ad4]533  int start, end;
[6772d19]534  owl_fmtext fm1;
535  owl_fmtext fm2;
[f7456bc]536  owl_regex re;
[6772d19]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"));
[ddbbcffa]569  g_free(str);
[6772d19]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"));
[ddbbcffa]576  g_free(str);
[6772d19]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"));
[ddbbcffa]591  g_free(str);
[6772d19]592
[e0022d2]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      "));
[ddbbcffa]604  g_free(str);
[e0022d2]605
[2b83ad6]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"));
[ddbbcffa]617  g_free(str);
[2b83ad6]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"));
[ddbbcffa]626  g_free(str);
[2b83ad6]627
[dc9665a]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
[f7456bc]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
[7ba2ad4]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
[6772d19]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}
[b31252d]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:
[ddbbcffa]695  g_free(filtstr);
[b31252d]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);
[ddbbcffa]720  g_free(filtname);
[b31252d]721  return failed;
722}
723
[443dcfa]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);
[ddbbcffa]744  g_free(filtname);
[443dcfa]745  return failed;
746}
747
748
[b31252d]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
[443dcfa]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");
[b31252d]802
803  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
804
805  return numfailed;
806}
[25891a8]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.