source: tester.c @ d222c44

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