source: tester.c @ ca54fd6

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