source: tester.c @ f2a96c0

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