source: tester.c @ 1bd5aa9

release-1.10
Last change on this file since 1bd5aa9 was 21dc927, checked in by Anders Kaseorg <andersk@mit.edu>, 11 years ago
Test owl_zephyr_smartstripped_user Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 36.9 KB
RevLine 
[5aa33fd]1#define OWL_PERL
2#define WINDOW FAKE_WINDOW
[7d4fbcd]3#include "owl.h"
[5aa33fd]4#undef WINDOW
[97cdbaf5]5#include "filterproc.h"
[5aa33fd]6
[bf5e6a2]7#include <stdio.h>
[7d4fbcd]8
[5aa33fd]9#undef instr
[67e5ba36]10#include <ncursesw/curses.h>
[5aa33fd]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);
[97cdbaf5]26int call_filter_regtest(void);
[21dc927]27int owl_smartstrip_regtest(void);
[8bce750]28
[5aa33fd]29extern void owl_perl_xs_init(pTHX);
30
[094009a]31int main(int argc, char **argv, char **env)
32{
[95414bf]33  FILE *rnull;
34  FILE *wnull;
[c2673ab]35  char *perlerr;
36  int status = 0;
[e93dd78]37  SCREEN *screen;
[95414bf]38
39  if (argc <= 1) {
40    fprintf(stderr, "Usage: %s --builtin|TEST.t|-le CODE\n", argv[0]);
41    return 1;
42  }
43
[f034ac0]44  /* initialize a fake ncurses, detached from std{in,out} */
[95414bf]45  wnull = fopen("/dev/null", "w");
46  rnull = fopen("/dev/null", "r");
[e93dd78]47  screen = newterm("xterm", wnull, rnull);
[f034ac0]48  /* initialize global structures */
49  owl_global_init(&g);
[124aebc]50
[c2673ab]51  perlerr = owl_perlconfig_initperl(NULL, &argc, &argv, &env);
52  if (perlerr) {
53    endwin();
54    fprintf(stderr, "Internal perl error: %s\n", perlerr);
55    status = 1;
56    goto out;
57  }
[98d7757]58
[c2673ab]59  owl_global_complete_setup(&g);
[04b16f8]60  owl_global_setup_default_filters(&g);
61
62  owl_view_create(owl_global_get_current_view(&g), "main",
63                  owl_global_get_filter(&g, "all"),
64                  owl_global_get_style_by_name(&g, "default"));
[22e02cd]65
[98d7757]66  owl_function_firstmsg();
[c2673ab]67
[95414bf]68  ENTER;
69  SAVETMPS;
[5aa33fd]70
[95414bf]71  if (strcmp(argv[1], "--builtin") == 0) {
72    status = owl_regtest();
73  } else if (strcmp(argv[1], "-le") == 0 && argc > 2) {
74    /*
75     * 'prove' runs its harness perl with '-le CODE' to get some
76     * information out.
77     */
78    moreswitches("l");
79    eval_pv(argv[2], true);
80  } else {
81    sv_setpv(get_sv("0", false), argv[1]);
82    sv_setpv(get_sv("main::test_prog", TRUE), argv[1]);
[5aa33fd]83
[95414bf]84    eval_pv("do $main::test_prog; die($@) if($@)", true);
85  }
[5aa33fd]86
[95414bf]87  status = 0;
[5aa33fd]88
[95414bf]89  FREETMPS;
90  LEAVE;
[c2673ab]91
92 out:
[5aa33fd]93  perl_destruct(owl_global_get_perlinterp(&g));
94  perl_free(owl_global_get_perlinterp(&g));
[c2673ab]95  /* probably not necessary, but tear down the screen */
96  endwin();
[e93dd78]97  delscreen(screen);
[c2673ab]98  fclose(rnull);
99  fclose(wnull);
100  return status;
101}
102
103int owl_regtest(void) {
[a2b3289]104  numtests = 0;
[7d4fbcd]105  int numfailures=0;
[a2b3289]106  /*
107    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
108    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
109  */
[3381399]110  numfailures += owl_util_regtest();
111  numfailures += owl_dict_regtest();
112  numfailures += owl_variable_regtest();
113  numfailures += owl_filter_regtest();
[4cc02605]114  numfailures += owl_editwin_regtest();
[6772d19]115  numfailures += owl_fmtext_regtest();
[b31252d]116  numfailures += owl_smartfilter_regtest();
[25891a8]117  numfailures += owl_history_regtest();
[97cdbaf5]118  numfailures += call_filter_regtest();
[21dc927]119  numfailures += owl_smartstrip_regtest();
[3381399]120  if (numfailures) {
[1cf3f8d3]121      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
[7d4fbcd]122  }
[a2b3289]123  printf("1..%d\n", numtests);
[f034ac0]124
[3381399]125  return(numfailures);
[7d4fbcd]126}
[8bce750]127
128#define FAIL_UNLESS(desc,pred) do { int __pred = (pred);                \
129    numtests++;                                                         \
[14c9e05]130    printf("%s %d %s", (__pred) ? "ok" : (numfailed++, "not ok"), numtests, desc); \
[8bce750]131    if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0)
132
133
134int owl_util_regtest(void)
135{
136  int numfailed=0;
[6a20996]137  char *path, *home;
[8bce750]138
139  printf("# BEGIN testing owl_util\n");
140
[a04218c]141#define CHECK_STR_AND_FREE(desc, expected, expr)         \
142    do {                                                 \
143      char *__value = (expr);                            \
144      FAIL_UNLESS((desc), !strcmp((expected), __value)); \
[ddbbcffa]145      g_free(__value);                                 \
[a04218c]146    } while (0)
147
148  CHECK_STR_AND_FREE("owl_text_substitute 2", "fYZYZ",
149                     owl_text_substitute("foo", "o", "YZ"));
150  CHECK_STR_AND_FREE("owl_text_substitute 3", "foo",
151                     owl_text_substitute("fYZYZ", "YZ", "o"));
152  CHECK_STR_AND_FREE("owl_text_substitute 4", "/u/foo/meep",
153                     owl_text_substitute("~/meep", "~", "/u/foo"));
[8bce750]154
155  FAIL_UNLESS("skiptokens 1", 
156              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
157  FAIL_UNLESS("skiptokens 2", 
158              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
159
[a04218c]160  CHECK_STR_AND_FREE("expand_tabs 1", "        hi", owl_text_expand_tabs("\thi"));
[8bce750]161
[a04218c]162  CHECK_STR_AND_FREE("expand_tabs 2", "        hi\nword    tab",
163                     owl_text_expand_tabs("\thi\nword\ttab"));
[8bce750]164
[a04218c]165  CHECK_STR_AND_FREE("expand_tabs 3", "                2 tabs",
166                     owl_text_expand_tabs("\t\t2 tabs"));
167  CHECK_STR_AND_FREE("expand_tabs 4", "α       ααααααα!        ",
168                     owl_text_expand_tabs(\tααααααα!\t"));
169  CHECK_STR_AND_FREE("expand_tabs 5", "A      AAA!!        ",
170                     owl_text_expand_tabs("A\tAAA!!\t"));
[8bce750]171
[e30ed92]172  FAIL_UNLESS("skiptokens 1",
173              !strcmp("world", skiptokens("hello world", 1)));
174
175  FAIL_UNLESS("skiptokens 2",
176              !strcmp("c d e", skiptokens("a   b c d e", 2)));
177
178  FAIL_UNLESS("skiptokens 3",
179              !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
180
181  FAIL_UNLESS("skiptokens 4",
182              !strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
183
184  FAIL_UNLESS("skiptokens 5",
185              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
186
[2bc6ad35]187#define CHECK_QUOTING(desc, unquoted, quoted)           \
188  do {                                                  \
189      int __argc;                                       \
190      char *__quoted = owl_arg_quote(unquoted);         \
191      char **__argv;                                    \
192      FAIL_UNLESS(desc, !strcmp(quoted, __quoted));     \
193      __argv = owl_parseline(__quoted, &__argc);        \
194      FAIL_UNLESS(desc " - arg count", __argc == 1);    \
[65c753e]195      FAIL_UNLESS(desc " - null-terminated",            \
196                  __argv[__argc] == NULL);              \
[2bc6ad35]197      FAIL_UNLESS(desc " - parsed",                     \
198                  !strcmp(__argv[0], unquoted));        \
[e56303f]199      g_strfreev(__argv);                               \
[ddbbcffa]200      g_free(__quoted);                         \
[2bc6ad35]201    } while (0)
202
203  CHECK_QUOTING("boring text", "mango", "mango");
204  CHECK_QUOTING("spaces", "mangos are tasty", "'mangos are tasty'");
205  CHECK_QUOTING("single quotes", "mango's", "\"mango's\"");
206  CHECK_QUOTING("double quotes", "he said \"mangos are tasty\"",
207                "'he said \"mangos are tasty\"'");
208  CHECK_QUOTING("both quotes",
209                "he said \"mango's are tasty even when you put in "
210                "a random apostrophe\"",
211                "\"he said \"'\"'\"mango's are tasty even when you put in "
212                "a random apostrophe\"'\"'\"\"");
213  CHECK_QUOTING("quote monster", "'\"\"'\"'''\"",
214                "\""
215                "'"
216                "\"'\"'\""
217                "\"'\"'\""
218                "'"
219                "\"'\"'\""
220                "'"
221                "'"
222                "'"
223                "\"'\"'\""
224                "\"");
225
[396505be]226  GString *quoted = g_string_new("");
227  owl_string_appendf_quoted(quoted, "%q foo %q%q %s %", "hello", "world is", "can't");
[f47696f]228  FAIL_UNLESS("owl_string_appendf",
[396505be]229              !strcmp(quoted->str, "hello foo 'world is'\"can't\" %s %"));
230  g_string_free(quoted, true);
[f47696f]231
[7feba19]232
[6a20996]233  CHECK_STR_AND_FREE("baseclass barnowl",
234                     "barnowl", owl_util_baseclass("barnowl"));
235  CHECK_STR_AND_FREE("baseclass unbarnowl",
236                     "barnowl", owl_util_baseclass("unbarnowl"));
237  CHECK_STR_AND_FREE("baseclass unununbarnowl.d.d",
238                     "barnowl", owl_util_baseclass("unununbarnowl.d.d"));
239  CHECK_STR_AND_FREE("baseclass ununun.d.d",
240                     "", owl_util_baseclass("ununun.d.d"));
241  CHECK_STR_AND_FREE("baseclass d.d.d.d",
242                     "d", owl_util_baseclass("d.d.d.d"));
243  CHECK_STR_AND_FREE("baseclass n.d.d.d",
244                     "n", owl_util_baseclass("n.d.d.d"));
245  CHECK_STR_AND_FREE("baseclass ununun.",
246                     ".", owl_util_baseclass("ununun."));
247  CHECK_STR_AND_FREE("baseclass unununu",
248                     "u", owl_util_baseclass("unununu"));
249
250
251  CHECK_STR_AND_FREE("makepath foo/bar",
252                     "foo/bar", owl_util_makepath("foo/bar"));
253  CHECK_STR_AND_FREE("makepath //foo///bar",
254                     "/foo/bar", owl_util_makepath("//foo///bar"));
255  CHECK_STR_AND_FREE("makepath foo/~//bar/",
256                     "foo/~/bar/", owl_util_makepath("foo/~//bar/"));
257  CHECK_STR_AND_FREE("makepath ~thisuserhadreallybetternotexist/foobar/",
258                     "~thisuserhadreallybetternotexist/foobar/",
259                     owl_util_makepath("~thisuserhadreallybetternotexist/foobar/"));
[bf5e6a2]260
[8219374]261  home = g_strdup(owl_global_get_homedir(&g));
[6a20996]262  CHECK_STR_AND_FREE("makepath ~",
263                     home, owl_util_makepath("~"));
[bf5e6a2]264
[0ba7333]265  path = g_build_filename(home, "foo/bar/baz", NULL);
[6a20996]266  CHECK_STR_AND_FREE("makepath ~///foo/bar//baz",
267                     path, owl_util_makepath("~///foo/bar//baz"));
[bf5e6a2]268  g_free(path);
[8219374]269  g_free(home);
[bf5e6a2]270
[8219374]271  home = owl_util_homedir_for_user("root");
272  if (home == NULL) {
[bf5e6a2]273    /* Just make some noise so we notice. */
[8219374]274    home = g_strdup("<WHAT>");
275    fprintf(stderr, "owl_util_homedir_for_user failed");
[bf5e6a2]276  }
277
[6a20996]278  CHECK_STR_AND_FREE("makepath ~root",
279                     home, owl_util_makepath("~root"));
[bf5e6a2]280
[0ba7333]281  path = g_build_filename(home, "foo/bar/baz", NULL);
[6a20996]282  CHECK_STR_AND_FREE("makepath ~root///foo/bar//baz",
283                     path, owl_util_makepath("~root///foo/bar//baz"));
[bf5e6a2]284  g_free(path);
[8219374]285  g_free(home);
[7feba19]286
[8bce750]287  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
288  printf("# END testing owl_util (%d failures)\n", numfailed);
289  return(numfailed);
290}
291
292int owl_dict_regtest(void) {
293  owl_dict d;
[ce68f23]294  GPtrArray *l;
[8bce750]295  int numfailed=0;
[4e37d56]296  char *av = g_strdup("aval"), *bv = g_strdup("bval"), *cv = g_strdup("cval"),
297    *dv = g_strdup("dval");
[8bce750]298
299  printf("# BEGIN testing owl_dict\n");
[4c7c21f]300  owl_dict_create(&d);
[a1074de]301  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
302  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
303  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
304  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
[8bce750]305  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
306  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
307  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
308  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
309  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
310  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
311  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
312  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
313
314  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
[ce68f23]315  l = owl_dict_get_keys(&d);
316  FAIL_UNLESS("get_keys result size", 3 == l->len);
[8bce750]317 
318  /* these assume the returned keys are sorted */
[ce68f23]319  FAIL_UNLESS("get_keys result val", 0 == strcmp("a", l->pdata[0]));
320  FAIL_UNLESS("get_keys result val", 0 == strcmp("b", l->pdata[1]));
321  FAIL_UNLESS("get_keys result val", 0 == strcmp("c", l->pdata[2]));
[8bce750]322
[ce68f23]323  owl_ptr_array_free(l, g_free);
[bf7aa1d]324  owl_dict_cleanup(&d, NULL);
[8bce750]325
[4e37d56]326  g_free(av);
327  g_free(bv);
328  g_free(cv);
329  g_free(dv);
330
[8bce750]331  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
332  printf("# END testing owl_dict (%d failures)\n", numfailed);
333  return(numfailed);
334}
335
336int owl_variable_regtest(void) {
337  owl_vardict vd;
[ca54fd6]338  owl_variable *var;
[8bce750]339  int numfailed=0;
[010a951]340  char *value;
[8bce750]341
342  printf("# BEGIN testing owl_variable\n");
[6a8b519]343  owl_variable_dict_setup(&vd);
[8bce750]344
[ca54fd6]345  FAIL_UNLESS("get bool var", NULL != (var = owl_variable_get_var(&vd, "rxping")));
346  FAIL_UNLESS("get bool", 0 == owl_variable_get_bool(var));
347  FAIL_UNLESS("get bool (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
[010a951]348  FAIL_UNLESS("get bool as string",
[ca54fd6]349              !strcmp((value = owl_variable_get_tostring(var)), "off"));
[010a951]350  g_free(value);
[ca54fd6]351  FAIL_UNLESS("set bool 1", 0 == owl_variable_set_bool_on(var));
352  FAIL_UNLESS("get bool 2", 1 == owl_variable_get_bool(var));
353  FAIL_UNLESS("set bool 3", 0 == owl_variable_set_fromstring(var, "off", 0));
354  FAIL_UNLESS("get bool 4", 0 == owl_variable_get_bool(var));
355  FAIL_UNLESS("set bool 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
356  FAIL_UNLESS("get bool 6", 0 == owl_variable_get_bool(var));
357
358
359  FAIL_UNLESS("get string var", NULL != (var = owl_variable_get_var(&vd, "logpath")));
360  FAIL_UNLESS("get string", 0 == strcmp("~/zlog/people", owl_variable_get_string(var)));
361  FAIL_UNLESS("set string 7", 0 == owl_variable_set_string(var, "whee"));
362  FAIL_UNLESS("get string", !strcmp("whee", owl_variable_get_string(var)));
363
364  FAIL_UNLESS("get int var", NULL != (var = owl_variable_get_var(&vd, "typewinsize")));
365  FAIL_UNLESS("get int", 8 == owl_variable_get_int(var));
366  FAIL_UNLESS("get int (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
[010a951]367  FAIL_UNLESS("get int as string",
[ca54fd6]368              !strcmp((value = owl_variable_get_tostring(var)), "8"));
[010a951]369  g_free(value);
[ca54fd6]370  FAIL_UNLESS("set int 1", 0 == owl_variable_set_int(var, 12));
371  FAIL_UNLESS("get int 2", 12 == owl_variable_get_int(var));
372  FAIL_UNLESS("set int 1b", -1 == owl_variable_set_int(var, -3));
373  FAIL_UNLESS("get int 2b", 12 == owl_variable_get_int(var));
374  FAIL_UNLESS("set int 3", 0 == owl_variable_set_fromstring(var, "9", 0));
375  FAIL_UNLESS("get int 4", 9 == owl_variable_get_int(var));
376  FAIL_UNLESS("set int 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
377  FAIL_UNLESS("set int 6", -1 == owl_variable_set_fromstring(var, "", 0));
378  FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var));
[8bce750]379
[366badc]380  FAIL_UNLESS("get enum var", NULL != (var = owl_variable_get_var(&vd, "scrollmode")));
381  FAIL_UNLESS("get enum", OWL_SCROLLMODE_NORMAL == owl_variable_get_int(var));
382  FAIL_UNLESS("get enum as string",
383              !strcmp((value = owl_variable_get_tostring(var)), "normal"));
384  g_free(value);
385  FAIL_UNLESS("set enum 1", 0 == owl_variable_set_int(var, OWL_SCROLLMODE_TOP));
386  FAIL_UNLESS("get enum 1", OWL_SCROLLMODE_TOP == owl_variable_get_int(var));
387  FAIL_UNLESS("set enum 2a", -1 == owl_variable_set_int(var, -1));
388  FAIL_UNLESS("set enum 2b", -1 == owl_variable_set_int(var, OWL_SCROLLMODE_PAGEDCENTER + 1));
389  FAIL_UNLESS("get enum 2", OWL_SCROLLMODE_TOP == owl_variable_get_int(var));
390  FAIL_UNLESS("set enum 3", 0 == owl_variable_set_fromstring(var, "center", 0));
391  FAIL_UNLESS("get enum 4", OWL_SCROLLMODE_CENTER == owl_variable_get_int(var));
392  FAIL_UNLESS("set enum 5", -1 == owl_variable_set_fromstring(var, "bogus", 0));
393  FAIL_UNLESS("set enum 6", -1 == owl_variable_set_fromstring(var, "", 0));
394  FAIL_UNLESS("get enum 7", OWL_SCROLLMODE_CENTER == owl_variable_get_int(var));
395
[1a4508b]396  owl_variable_dict_newvar_string(&vd, "stringvar", "testval", "", "");
[ca54fd6]397  FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar")));
398  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var)));
399  owl_variable_set_string(var, "new val");
400  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var)));
[8bce750]401
[1a4508b]402  owl_variable_dict_newvar_int(&vd, "intvar", 47, "", "");
[ca54fd6]403  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
404  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
405  owl_variable_set_int(var, 17);
406  FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var));
[8bce750]407
[1a4508b]408  owl_variable_dict_newvar_bool(&vd, "boolvar", true, "", "");
[ca54fd6]409  FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar")));
410  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
411  owl_variable_set_bool_off(var);
412  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
[8bce750]413
[1a4508b]414  owl_variable_dict_newvar_string(&vd, "nullstringvar", NULL, "", "");
[b4a678a]415  FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar")));
416  FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var)));
417  g_free(value);
418  var = owl_variable_get_var(&vd, "zsigproc");
419  FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var)));
420  g_free(value);
421
[366badc]422  owl_variable_dict_newvar_enum(&vd, "enumvar", 0, "", "", "a,b,c,d");
423  FAIL_UNLESS("get new enum var", NULL != (var = owl_variable_get_var(&vd, "enumvar")));
424  FAIL_UNLESS("get new enum val", 0 == owl_variable_get_int(var));
425  owl_variable_set_fromstring(var, "c", 0);
426  FAIL_UNLESS("update enum val", 2 == owl_variable_get_int(var));
427
[0fef6eb]428  owl_variable_dict_cleanup(&vd);
[8bce750]429
430  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
431  printf("# END testing owl_variable (%d failures)\n", numfailed);
432  return(numfailed);
433}
434
[e9c6fc8]435static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
436{
[23fddad]437  owl_filter *f;
[8bce750]438  int ok;
439  int failed = 0;
[23fddad]440  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
[8bce750]441    printf("not ok can't parse %s\n", filt);
442    failed = 1;
443    goto out;
444  }
[23fddad]445  ok = owl_filter_message_match(f, m);
[8bce750]446  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
447    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
448    failed = 1;
449  }
450 out:
[23fddad]451  owl_filter_delete(f);
[8bce750]452  if(!failed) {
453    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
454  }
455  return failed;
456}
457
458int owl_filter_regtest(void) {
459  int numfailed=0;
460  owl_message m;
[23fddad]461  owl_filter *f1, *f2, *f3, *f4, *f5;
[8bce750]462
463  owl_message_init(&m);
464  owl_message_set_type_zephyr(&m);
465  owl_message_set_direction_in(&m);
466  owl_message_set_class(&m, "owl");
467  owl_message_set_instance(&m, "tester");
468  owl_message_set_sender(&m, "owl-user");
469  owl_message_set_recipient(&m, "joe");
470  owl_message_set_attribute(&m, "foo", "bar");
471
472#define TEST_FILTER(f, e) do {                          \
473    numtests++;                                         \
474    numfailed += owl_filter_test_string(f, &m, e);      \
475      } while(0)
476
477  TEST_FILTER("true", 1);
478  TEST_FILTER("false", 0);
479  TEST_FILTER("( true )", 1);
480  TEST_FILTER("not false", 1);
481  TEST_FILTER("( true ) or ( false )", 1);
482  TEST_FILTER("true and false", 0);
483  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
484
485  TEST_FILTER("class owl", 1);
486  TEST_FILTER("class ^owl$", 1);
487  TEST_FILTER("instance test", 1);
488  TEST_FILTER("instance ^test$", 0);
489  TEST_FILTER("instance ^tester$", 1);
490
491  TEST_FILTER("foo bar", 1);
492  TEST_FILTER("class owl and instance tester", 1);
493  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
494
495  /* Order of operations and precedence */
496  TEST_FILTER("not true or false", 0);
497  TEST_FILTER("true or true and false", 0);
498  TEST_FILTER("true and true and false or true", 1);
499  TEST_FILTER("false and false or true", 1);
500  TEST_FILTER("true and false or false", 0);
501
[23fddad]502  f1 = owl_filter_new_fromstring("f1", "class owl");
503  owl_global_add_filter(&g, f1);
[8bce750]504  TEST_FILTER("filter f1", 1);
[23fddad]505  owl_global_remove_filter(&g, "f1");
[8bce750]506
507  /* Test recursion prevention */
[23fddad]508  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
509  owl_filter_delete(f2);
[8bce750]510
511  /* mutual recursion */
[23fddad]512  f3 = owl_filter_new_fromstring("f3", "filter f4");
513  owl_global_add_filter(&g, f3);
514  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
515  owl_global_remove_filter(&g, "f3");
516  owl_filter_delete(f4);
[8bce750]517
518  /* support referencing a filter several times */
[23fddad]519  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
520  owl_filter_delete(f5);
[8bce750]521
[a74a044]522  owl_message_cleanup(&m);
523
[8bce750]524  return 0;
525}
526
[4cc02605]527int owl_editwin_regtest(void) {
528  int numfailed = 0;
529  const char *p;
[35a30f9]530  owl_editwin *oe;
[58a16cc]531  const char *autowrap_string = "we feel our owls should live "
532                                "closer to our ponies.";
[4cc02605]533
534  printf("# BEGIN testing owl_editwin\n");
535
[4123da1]536  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
[4cc02605]537
538  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
539
540  /* check paragraph fill */
541  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
542  owl_editwin_move_to_top(oe);
543  owl_editwin_fill_paragraph(oe);
544  p = owl_editwin_get_text(oe);
545  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
546                                                            "blah blah blah.\n"
547                                                            "\n"
548                                                            "blah"));
549
[9190285]550  owl_editwin_unref(oe); oe = NULL;
[4123da1]551  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
552
[4cc02605]553  /* check that lines ending with ". " correctly fill */
554  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
555  owl_editwin_move_to_top(oe);
556  owl_editwin_fill_paragraph(oe);
557  p = owl_editwin_get_text(oe);
558  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
559                                                            "blah blah blah. \n"
560                                                            "\n"
561                                                            "blah"));
562
[9190285]563  owl_editwin_unref(oe); oe = NULL;
[4cc02605]564
[e5c9d3de]565  /* Test owl_editwin_move_to_beginning_of_line. */
566  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
567  owl_editwin_insert_string(oe, "\n");
568  owl_editwin_insert_string(oe, "12345678\n");
569  owl_editwin_insert_string(oe, "\n");
570  owl_editwin_insert_string(oe, "abcdefg\n");
571  owl_editwin_move_to_top(oe);
572  FAIL_UNLESS("already at beginning of line",
573              owl_editwin_move_to_beginning_of_line(oe) == 0);
574  owl_editwin_line_move(oe, 1);
575  owl_editwin_point_move(oe, 5);
576  FAIL_UNLESS("find beginning of line after empty first line",
577              owl_editwin_move_to_beginning_of_line(oe) == -5);
578  owl_editwin_line_move(oe, 1);
579  FAIL_UNLESS("find beginning empty middle line",
580              owl_editwin_move_to_beginning_of_line(oe) == 0);
581  owl_editwin_line_move(oe, 1);
582  owl_editwin_point_move(oe, 2);
583  FAIL_UNLESS("find beginning of line after empty middle line",
584              owl_editwin_move_to_beginning_of_line(oe) == -2);
[9190285]585  owl_editwin_unref(oe); oe = NULL;
[e5c9d3de]586
[58a16cc]587  /* Test automatic line-wrapping. */
588  owl_global_set_edit_maxwrapcols(&g, 10);
589  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
590  for (p = autowrap_string; *p; p++) {
591    owl_input j;
592    j.ch = *p;
593    j.uch = *p; /* Assuming ASCII. */
594    owl_editwin_process_char(oe, j);
595  }
596  p = owl_editwin_get_text(oe);
597  FAIL_UNLESS("text was automatically wrapped",
598              p && !strcmp(p, "we feel\n"
599                           "our owls\n"
600                           "should\n"
601                           "live\n"
602                           "closer to\n"
603                           "our\n"
604                           "ponies."));
605  owl_editwin_unref(oe); oe = NULL;
606  owl_global_set_edit_maxwrapcols(&g, 70);
607
[ac6d4e4]608  /* Test owl_editwin_current_column. */
609  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
610  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
611  owl_editwin_insert_string(oe, "abcdef");
612  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
613  owl_editwin_insert_string(oe, "\t");
614  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
615  owl_editwin_insert_string(oe, "123\n12\t3");
616  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
617  owl_editwin_move_to_beginning_of_line(oe);
618  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
619  owl_editwin_unref(oe); oe = NULL;
620
[4cc02605]621  printf("# END testing owl_editwin (%d failures)\n", numfailed);
622
623  return numfailed;
624}
[6772d19]625
626int owl_fmtext_regtest(void) {
627  int numfailed = 0;
[7ba2ad4]628  int start, end;
[6772d19]629  owl_fmtext fm1;
630  owl_fmtext fm2;
[f7456bc]631  owl_regex re;
[6772d19]632  char *str;
633
634  printf("# BEGIN testing owl_fmtext\n");
635
636  owl_fmtext_init_null(&fm1);
637  owl_fmtext_init_null(&fm2);
638
639  /* Verify text gets correctly appended. */
640  owl_fmtext_append_normal(&fm1, "1234567898");
641  owl_fmtext_append_fmtext(&fm2, &fm1);
642  FAIL_UNLESS("string lengths correct",
643              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
644
645  /* Test owl_fmtext_num_lines. */
646  owl_fmtext_clear(&fm1);
647  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
648  owl_fmtext_append_normal(&fm1, "12345\n67898");
649  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
650  owl_fmtext_append_normal(&fm1, "\n");
651  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
652  owl_fmtext_append_bold(&fm1, "");
653  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
654
655  /* Test owl_fmtext_truncate_lines */
656  owl_fmtext_clear(&fm1);
657  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
658
659  owl_fmtext_clear(&fm2);
660  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
661  str = owl_fmtext_print_plain(&fm2);
662  FAIL_UNLESS("lines corrected truncated",
663              str && !strcmp(str, "1\n2\n3\n"));
[ddbbcffa]664  g_free(str);
[6772d19]665
666  owl_fmtext_clear(&fm2);
667  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
668  str = owl_fmtext_print_plain(&fm2);
669  FAIL_UNLESS("lines corrected truncated",
670              str && !strcmp(str, "1\n2\n3\n4\n"));
[ddbbcffa]671  g_free(str);
[6772d19]672
673  /* Test owl_fmtext_truncate_cols. */
674  owl_fmtext_clear(&fm1);
675  owl_fmtext_append_normal(&fm1, "123456789012345\n");
676  owl_fmtext_append_normal(&fm1, "123456789\n");
677  owl_fmtext_append_normal(&fm1, "1234567890\n");
678
679  owl_fmtext_clear(&fm2);
680  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
681  str = owl_fmtext_print_plain(&fm2);
682  FAIL_UNLESS("columns correctly truncated",
683              str && !strcmp(str, "567890"
684                                  "56789\n"
685                                  "567890"));
[ddbbcffa]686  g_free(str);
[6772d19]687
[e0022d2]688  owl_fmtext_clear(&fm1);
689  owl_fmtext_append_normal(&fm1, "12\t1234");
690  owl_fmtext_append_bold(&fm1, "56\n");
691  owl_fmtext_append_bold(&fm1, "12345678\t\n");
692
693  owl_fmtext_clear(&fm2);
694  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
695  str = owl_fmtext_print_plain(&fm2);
696  FAIL_UNLESS("columns correctly truncated",
697              str && !strcmp(str, "    123456"
698                                  "5678      "));
[ddbbcffa]699  g_free(str);
[e0022d2]700
[2b83ad6]701  /* Test owl_fmtext_expand_tabs. */
702  owl_fmtext_clear(&fm1);
703  owl_fmtext_append_normal(&fm1, "12\t1234");
704  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
705  owl_fmtext_clear(&fm2);
706  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
707  str = owl_fmtext_print_plain(&fm2);
708  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
709  FAIL_UNLESS("tabs corrected expanded",
710              str && !strcmp(str, "12      1234567 1\n"
711                                  "12345678        1"));
[ddbbcffa]712  g_free(str);
[2b83ad6]713
714  owl_fmtext_clear(&fm2);
715  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
716  str = owl_fmtext_print_plain(&fm2);
717  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
718  FAIL_UNLESS("tabs corrected expanded",
719              str && !strcmp(str, "12     1234567 1\n"
720                                  "12345678       1"));
[ddbbcffa]721  g_free(str);
[2b83ad6]722
[dc9665a]723  /* Test owl_fmtext_search. */
724  owl_fmtext_clear(&fm1);
725  owl_fmtext_append_normal(&fm1, "123123123123");
726  owl_regex_create(&re, "12");
727  {
728    int count = 0, offset;
729    offset = owl_fmtext_search(&fm1, &re, 0);
730    while (offset >= 0) {
731      FAIL_UNLESS("search matches",
732                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
733      count++;
734      offset = owl_fmtext_search(&fm1, &re, offset+1);
735    }
736    FAIL_UNLESS("exactly four matches", count == 4);
737  }
738  owl_regex_cleanup(&re);
739
[f7456bc]740  /* Test owl_fmtext_line_number. */
741  owl_fmtext_clear(&fm1);
742  owl_fmtext_append_normal(&fm1, "123\n456\n");
743  owl_fmtext_append_bold(&fm1, "");
744  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
745  FAIL_UNLESS("trailing formatting characters part of false line",
746              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
747  owl_regex_create_quoted(&re, "456");
748  FAIL_UNLESS("correctly find second line (line 1)",
749              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
750  owl_regex_cleanup(&re);
751
[7ba2ad4]752  /* Test owl_fmtext_line_extents. */
753  owl_fmtext_clear(&fm1);
754  owl_fmtext_append_normal(&fm1, "123\n456\n789");
755  owl_fmtext_line_extents(&fm1, 1, &start, &end);
756  FAIL_UNLESS("line contents",
757              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
758  owl_fmtext_line_extents(&fm1, 2, &start, &end);
759  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
760
[6772d19]761  owl_fmtext_cleanup(&fm1);
762  owl_fmtext_cleanup(&fm2);
763
764  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
765
766  return numfailed;
767}
[b31252d]768
769static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
770  owl_filter *f = NULL;
771  char *filtstr = NULL;
772  int failed = 0;
773  f = owl_global_get_filter(&g, filtname);
774  if (f == NULL) {
775    printf("not ok filter missing: %s\n", filtname);
776    failed = 1;
777    goto out;
778  }
779
780  /* TODO: Come up with a better way to test this. */
781  filtstr = owl_filter_print(f);
782  if (strcmp(expected, filtstr)) {
783    printf("not ok filter incorrect: |%s| instead of |%s|\n",
784           filtstr, expected);
785    failed = 1;
786    goto out;
787  }
788
789 out:
[ddbbcffa]790  g_free(filtstr);
[b31252d]791  return failed;
792}
793
794static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
795  char *filtname = NULL;
796  int failed = 0;
797
798  filtname = owl_function_classinstfilt(c, i, related);
799  if (filtname == NULL) {
800    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
801           related ? "related" : "not related");
802    failed = 1;
803    goto out;
804  }
805  if (owl_smartfilter_test_equals(filtname, expected)) {
806    failed = 1;
807    goto out;
808  }
809 out:
810  if (!failed) {
811    printf("ok %s\n", filtname);
812  }
813  if (filtname)
814    owl_global_remove_filter(&g, filtname);
[ddbbcffa]815  g_free(filtname);
[b31252d]816  return failed;
817}
818
[443dcfa]819static int owl_zuserfilt_test(const char *longuser, const char *expected) {
820  char *filtname = NULL;
821  int failed = 0;
822
823  filtname = owl_function_zuserfilt(longuser);
824  if (filtname == NULL) {
825    printf("not ok null filtname: %s\n", longuser);
826    failed = 1;
827    goto out;
828  }
829  if (owl_smartfilter_test_equals(filtname, expected)) {
830    failed = 1;
831    goto out;
832  }
833 out:
834  if (!failed) {
835    printf("ok %s\n", filtname);
836  }
837  if (filtname)
838    owl_global_remove_filter(&g, filtname);
[ddbbcffa]839  g_free(filtname);
[443dcfa]840  return failed;
841}
842
843
[b31252d]844int owl_smartfilter_regtest(void) {
845  int numfailed = 0;
846
847  printf("# BEGIN testing owl_smartfilter\n");
848
849  /* Check classinst making. */
850
851#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
852    numtests++;                                         \
853    numfailed += owl_classinstfilt_test(c, i, r, e);    \
854  } while (0)
855  TEST_CLASSINSTFILT("message", NULL, false,
856                     "class ^message$\n");
857  TEST_CLASSINSTFILT("message", NULL, true,
858                     "class ^(un)*message(\\.d)*$\n");
859  TEST_CLASSINSTFILT("message", "personal", false,
860                     "class ^message$ and instance ^personal$\n");
861  TEST_CLASSINSTFILT("message", "personal", true,
862                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
863
864  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
865                     "class ^message$ and instance '^evil\tinstance$'\n");
866  TEST_CLASSINSTFILT("message", "evil instance", false,
867                     "class ^message$ and instance '^evil instance$'\n");
868  TEST_CLASSINSTFILT("message", "evil'instance", false,
869                     "class ^message$ and instance \"^evil'instance$\"\n");
870  TEST_CLASSINSTFILT("message", "evil\"instance", false,
871                     "class ^message$ and instance '^evil\"instance$'\n");
872  TEST_CLASSINSTFILT("message", "evil$instance", false,
873                     "class ^message$ and instance ^evil\\$instance$\n");
874
[443dcfa]875#define TEST_ZUSERFILT(l, e) do {                       \
876    numtests++;                                         \
877    numfailed += owl_zuserfilt_test(l, e);              \
878  } while (0)
879  TEST_ZUSERFILT("user",
880                 "( type ^zephyr$ and filter personal and "
881                 "( ( direction ^in$ and sender "
882                 "^user$"
883                 " ) or ( direction ^out$ and recipient "
884                 "^user$"
885                 " ) ) ) or ( ( class ^login$ ) and ( sender "
886                 "^user$"
887                 " ) )\n");
888  TEST_ZUSERFILT("very evil\t.user",
889                 "( type ^zephyr$ and filter personal and "
890                 "( ( direction ^in$ and sender "
891                 "'^very evil\t\\.user$'"
892                 " ) or ( direction ^out$ and recipient "
893                 "'^very evil\t\\.user$'"
894                 " ) ) ) or ( ( class ^login$ ) and ( sender "
895                 "'^very evil\t\\.user$'"
896                 " ) )\n");
[b31252d]897
898  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
899
900  return numfailed;
901}
[25891a8]902
903int owl_history_regtest(void)
904{
905  int numfailed = 0;
906  int i;
907  owl_history h;
908
909  printf("# BEGIN testing owl_history\n");
910  owl_history_init(&h);
911
912  /* Operations on empty history. */
913  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
914  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
915  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
916
917  /* Insert a few records. */
918  owl_history_store(&h, "a", false);
919  owl_history_store(&h, "b", false);
920  owl_history_store(&h, "c", false);
921  owl_history_store(&h, "d", true);
922
923  /* Walk up and down the history a bit. */
924  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
925  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
926  FAIL_UNLESS("touched", owl_history_is_touched(&h));
927  FAIL_UNLESS("next d", strcmp(owl_history_get_next(&h), "d") == 0);
928  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
929  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
930  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
931  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
932  FAIL_UNLESS("prev a", strcmp(owl_history_get_prev(&h), "a") == 0);
933  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
934
935  /* Now insert something. It should reset and blow away 'd'. */
936  owl_history_store(&h, "e", false);
937  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
938  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
939  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
940  FAIL_UNLESS("touched", owl_history_is_touched(&h));
941  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
942  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
943
944  /* Lines get de-duplicated on insert. */
945  owl_history_store(&h, "e", false);
946  owl_history_store(&h, "e", false);
947  owl_history_store(&h, "e", false);
948  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
949  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
950
951  /* But a partial is not deduplicated, as it'll go away soon. */
952  owl_history_store(&h, "e", true);
953  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
954  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
955  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
956  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
957
958  /* Reset moves to the front... */
959  owl_history_store(&h, "f", true);
960  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
961  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
962  owl_history_reset(&h);
963  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
964  /* ...and destroys any pending partial entry... */
965  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
966  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
967  /* ...but not non-partial ones. */
968  owl_history_reset(&h);
969  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
970
971  /* Finally, check we are bounded by OWL_HISTORYSIZE. */
972  for (i = 0; i < OWL_HISTORYSIZE; i++) {
973    char *string = g_strdup_printf("mango%d", i);
974    owl_history_store(&h, string, false);
975    g_free(string);
976  }
977  /* The OWL_HISTORYSIZE'th prev gets NULL. */
978  for (i = OWL_HISTORYSIZE - 2; i >= 0; i--) {
979    char *string = g_strdup_printf("mango%d", i);
980    FAIL_UNLESS("prev mango_N", strcmp(owl_history_get_prev(&h), string) == 0);
981    g_free(string);
982  }
983  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
984
985  owl_history_cleanup(&h);
986
987  printf("# END testing owl_history (%d failures)\n", numfailed);
988  return numfailed;
989}
[97cdbaf5]990
991int call_filter_regtest(void)
992{
993  int numfailed = 0;
994  int ret;
995  char *out = NULL;
996  int status;
997
998  printf("# BEGIN testing call_filter\n");
999
1000  const char *cat_argv[] = { "cat", NULL };
1001  ret = call_filter(cat_argv, "Mangos!", &out, &status);
1002  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1003                                  status == 0 &&
1004                                  strcmp(out, "Mangos!") == 0));
1005  g_free(out); out = NULL;
1006
1007  ret = call_filter(cat_argv, "", &out, &status);
1008  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1009                                  status == 0 &&
1010                                  strcmp(out, "") == 0));
1011  g_free(out); out = NULL;
1012
1013  ret = call_filter(cat_argv, NULL, &out, &status);
1014  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1015                                  status == 0 &&
1016                                  strcmp(out, "") == 0));
1017  g_free(out); out = NULL;
1018
1019  printf("# END testing call_filter (%d failures)\n", numfailed);
1020  return numfailed;
1021}
[21dc927]1022
1023int owl_smartstrip_regtest(void)
1024{
1025  int numfailed = 0;
1026
1027  printf("# BEGIN testing owl_zephyr_smartstripped_user\n");
1028
1029#define CHECK_SMARTSTRIP(in, expected)                  \
1030  do {                                                  \
1031    char *__value = owl_zephyr_smartstripped_user(in);  \
1032    FAIL_UNLESS("owl_zephyr_smartstripped_user " in,    \
1033                strcmp((expected), __value) == 0);      \
1034    g_free(__value);                                    \
1035  } while (0)
1036
1037  CHECK_SMARTSTRIP("foo", "foo");
1038  CHECK_SMARTSTRIP("foo.bar", "foo");
1039  CHECK_SMARTSTRIP("foo/bar", "foo");
1040  CHECK_SMARTSTRIP("host/bar", "host/bar");
1041  CHECK_SMARTSTRIP("rcmd.bar", "rcmd.bar");
1042  CHECK_SMARTSTRIP("daemon/bar", "daemon/bar");
1043  CHECK_SMARTSTRIP("daemon.bar", "daemon.bar");
1044
1045  CHECK_SMARTSTRIP("foo@ATHENA.MIT.EDU", "foo@ATHENA.MIT.EDU");
1046  CHECK_SMARTSTRIP("foo.bar@ATHENA.MIT.EDU", "foo@ATHENA.MIT.EDU");
1047  CHECK_SMARTSTRIP("foo/bar@ATHENA.MIT.EDU", "foo@ATHENA.MIT.EDU");
1048  CHECK_SMARTSTRIP("host/bar@ATHENA.MIT.EDU", "host/bar@ATHENA.MIT.EDU");
1049  CHECK_SMARTSTRIP("rcmd.bar@ATHENA.MIT.EDU", "rcmd.bar@ATHENA.MIT.EDU");
1050  CHECK_SMARTSTRIP("daemon/bar@ATHENA.MIT.EDU", "daemon/bar@ATHENA.MIT.EDU");
1051  CHECK_SMARTSTRIP("daemon.bar@ATHENA.MIT.EDU", "daemon.bar@ATHENA.MIT.EDU");
1052
1053  printf("# END testing owl_zephyr_smartstripped_user\n");
1054
1055  return numfailed;
1056}
Note: See TracBrowser for help on using the repository browser.