source: tester.c @ ce35060

release-1.10release-1.7release-1.8release-1.9
Last change on this file since ce35060 was e45d9f4, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Don't re-initialize filters in owl_filter_regtest We setup the default filters now, so avoid leaking all of them.
  • Property mode set to 100644
File size: 21.9 KB
Line 
1#define OWL_PERL
2#define WINDOW FAKE_WINDOW
3#include "owl.h"
4#undef WINDOW
5
6#include <unistd.h>
7#include <stdlib.h>
8
9#undef instr
10#include <curses.h>
11
12owl_global g;
13
14int numtests;
15
16int owl_regtest(void);
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);
22int owl_editwin_regtest(void);
23int owl_fmtext_regtest(void);
24
25extern void owl_perl_xs_init(pTHX);
26
27int main(int argc, char **argv, char **env)
28{
29  FILE *rnull;
30  FILE *wnull;
31  char *perlerr;
32  int status = 0;
33
34  if (argc <= 1) {
35    fprintf(stderr, "Usage: %s --builtin|TEST.t|-le CODE\n", argv[0]);
36    return 1;
37  }
38
39  /* initialize a fake ncurses, detached from std{in,out} */
40  wnull = fopen("/dev/null", "w");
41  rnull = fopen("/dev/null", "r");
42  newterm("xterm", wnull, rnull);
43  /* initialize global structures */
44  owl_global_init(&g);
45
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  }
53
54  owl_global_complete_setup(&g);
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"));
60
61  owl_function_firstmsg();
62
63  ENTER;
64  SAVETMPS;
65
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]);
78
79    eval_pv("do $main::test_prog; die($@) if($@)", true);
80  }
81
82  status = 0;
83
84  FREETMPS;
85  LEAVE;
86
87 out:
88  perl_destruct(owl_global_get_perlinterp(&g));
89  perl_free(owl_global_get_perlinterp(&g));
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) {
98  numtests = 0;
99  int numfailures=0;
100  /*
101    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
102    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
103  */
104  numfailures += owl_util_regtest();
105  numfailures += owl_dict_regtest();
106  numfailures += owl_variable_regtest();
107  numfailures += owl_filter_regtest();
108  numfailures += owl_editwin_regtest();
109  numfailures += owl_fmtext_regtest();
110  if (numfailures) {
111      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
112  }
113  printf("1..%d\n", numtests);
114
115  return(numfailures);
116}
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
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"));
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
151  CHECK_STR_AND_FREE("expand_tabs 1", "        hi", owl_text_expand_tabs("\thi"));
152
153  CHECK_STR_AND_FREE("expand_tabs 2", "        hi\nword    tab",
154                     owl_text_expand_tabs("\thi\nword\ttab"));
155
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"));
162
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
178  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
179  printf("# END testing owl_util (%d failures)\n", numfailed);
180  return(numfailed);
181}
182
183int owl_dict_regtest(void) {
184  owl_dict d;
185  owl_list l;
186  int numfailed=0;
187  char *av="aval", *bv="bval", *cv="cval", *dv="dval";
188
189  printf("# BEGIN testing owl_dict\n");
190  FAIL_UNLESS("create", 0==owl_dict_create(&d));
191  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
192  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
193  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
194  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
195  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
196  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
197  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
198  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
199  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
200  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
201  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
202  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
203
204  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
205  FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l));
206  FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l));
207 
208  /* these assume the returned keys are sorted */
209  FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0)));
210  FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1)));
211  FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2)));
212
213  owl_list_cleanup(&l, owl_free);
214  owl_dict_cleanup(&d, NULL);
215
216  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
217  printf("# END testing owl_dict (%d failures)\n", numfailed);
218  return(numfailed);
219}
220
221int owl_variable_regtest(void) {
222  owl_vardict vd;
223  int numfailed=0;
224  char buf[1024];
225  const void *v;
226
227  printf("# BEGIN testing owl_variable\n");
228  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
229
230  FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping"));
231  FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
232  FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"rxping", buf, 1024));
233  FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off"));
234  FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
235  FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
236  FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0));
237  FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping"));
238  FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0));
239  FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping"));
240
241
242  FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
243  FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
244  FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
245
246  FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
247  FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
248  FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024));
249  FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8"));
250  FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
251  FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
252  FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
253  FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
254  FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0));
255  FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
256  FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0));
257  FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0));
258  FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
259
260  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
261  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar", OWL_VARIABLE_STRING)));
262  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar")));
263  owl_variable_set_string(&vd, "stringvar", "new val");
264  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar")));
265
266  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
267  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar", OWL_VARIABLE_INT)));
268  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar"));
269  owl_variable_set_int(&vd, "intvar", 17);
270  FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar"));
271
272  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
273  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar", OWL_VARIABLE_BOOL)));
274  FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar"));
275  owl_variable_set_bool_off(&vd, "boolvar");
276  FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar"));
277
278  owl_variable_dict_cleanup(&vd);
279
280  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
281  printf("# END testing owl_variable (%d failures)\n", numfailed);
282  return(numfailed);
283}
284
285static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
286{
287  owl_filter *f;
288  int ok;
289  int failed = 0;
290  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
291    printf("not ok can't parse %s\n", filt);
292    failed = 1;
293    goto out;
294  }
295  ok = owl_filter_message_match(f, m);
296  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
297    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
298    failed = 1;
299  }
300 out:
301  owl_filter_delete(f);
302  if(!failed) {
303    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
304  }
305  return failed;
306}
307
308int owl_filter_regtest(void) {
309  int numfailed=0;
310  owl_message m;
311  owl_filter *f1, *f2, *f3, *f4, *f5;
312
313  owl_message_init(&m);
314  owl_message_set_type_zephyr(&m);
315  owl_message_set_direction_in(&m);
316  owl_message_set_class(&m, "owl");
317  owl_message_set_instance(&m, "tester");
318  owl_message_set_sender(&m, "owl-user");
319  owl_message_set_recipient(&m, "joe");
320  owl_message_set_attribute(&m, "foo", "bar");
321
322#define TEST_FILTER(f, e) do {                          \
323    numtests++;                                         \
324    numfailed += owl_filter_test_string(f, &m, e);      \
325      } while(0)
326
327  TEST_FILTER("true", 1);
328  TEST_FILTER("false", 0);
329  TEST_FILTER("( true )", 1);
330  TEST_FILTER("not false", 1);
331  TEST_FILTER("( true ) or ( false )", 1);
332  TEST_FILTER("true and false", 0);
333  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
334
335  TEST_FILTER("class owl", 1);
336  TEST_FILTER("class ^owl$", 1);
337  TEST_FILTER("instance test", 1);
338  TEST_FILTER("instance ^test$", 0);
339  TEST_FILTER("instance ^tester$", 1);
340
341  TEST_FILTER("foo bar", 1);
342  TEST_FILTER("class owl and instance tester", 1);
343  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
344
345  /* Order of operations and precedence */
346  TEST_FILTER("not true or false", 0);
347  TEST_FILTER("true or true and false", 0);
348  TEST_FILTER("true and true and false or true", 1);
349  TEST_FILTER("false and false or true", 1);
350  TEST_FILTER("true and false or false", 0);
351
352  f1 = owl_filter_new_fromstring("f1", "class owl");
353  owl_global_add_filter(&g, f1);
354  TEST_FILTER("filter f1", 1);
355  owl_global_remove_filter(&g, "f1");
356
357  /* Test recursion prevention */
358  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
359  owl_filter_delete(f2);
360
361  /* mutual recursion */
362  f3 = owl_filter_new_fromstring("f3", "filter f4");
363  owl_global_add_filter(&g, f3);
364  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
365  owl_global_remove_filter(&g, "f3");
366  owl_filter_delete(f4);
367
368  /* support referencing a filter several times */
369  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
370  owl_filter_delete(f5);
371
372  return 0;
373}
374
375int owl_editwin_regtest(void) {
376  int numfailed = 0;
377  const char *p;
378  owl_editwin *oe;
379  const char *autowrap_string = "we feel our owls should live "
380                                "closer to our ponies.";
381
382  printf("# BEGIN testing owl_editwin\n");
383
384  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
385
386  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
387
388  /* check paragraph fill */
389  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
390  owl_editwin_move_to_top(oe);
391  owl_editwin_fill_paragraph(oe);
392  p = owl_editwin_get_text(oe);
393  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
394                                                            "blah blah blah.\n"
395                                                            "\n"
396                                                            "blah"));
397
398  owl_editwin_unref(oe); oe = NULL;
399  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
400
401  /* check that lines ending with ". " correctly fill */
402  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
403  owl_editwin_move_to_top(oe);
404  owl_editwin_fill_paragraph(oe);
405  p = owl_editwin_get_text(oe);
406  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
407                                                            "blah blah blah. \n"
408                                                            "\n"
409                                                            "blah"));
410
411  owl_editwin_unref(oe); oe = NULL;
412
413  /* Test owl_editwin_move_to_beginning_of_line. */
414  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
415  owl_editwin_insert_string(oe, "\n");
416  owl_editwin_insert_string(oe, "12345678\n");
417  owl_editwin_insert_string(oe, "\n");
418  owl_editwin_insert_string(oe, "abcdefg\n");
419  owl_editwin_move_to_top(oe);
420  FAIL_UNLESS("already at beginning of line",
421              owl_editwin_move_to_beginning_of_line(oe) == 0);
422  owl_editwin_line_move(oe, 1);
423  owl_editwin_point_move(oe, 5);
424  FAIL_UNLESS("find beginning of line after empty first line",
425              owl_editwin_move_to_beginning_of_line(oe) == -5);
426  owl_editwin_line_move(oe, 1);
427  FAIL_UNLESS("find beginning empty middle line",
428              owl_editwin_move_to_beginning_of_line(oe) == 0);
429  owl_editwin_line_move(oe, 1);
430  owl_editwin_point_move(oe, 2);
431  FAIL_UNLESS("find beginning of line after empty middle line",
432              owl_editwin_move_to_beginning_of_line(oe) == -2);
433  owl_editwin_unref(oe); oe = NULL;
434
435  /* Test automatic line-wrapping. */
436  owl_global_set_edit_maxwrapcols(&g, 10);
437  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
438  for (p = autowrap_string; *p; p++) {
439    owl_input j;
440    j.ch = *p;
441    j.uch = *p; /* Assuming ASCII. */
442    owl_editwin_process_char(oe, j);
443  }
444  p = owl_editwin_get_text(oe);
445  FAIL_UNLESS("text was automatically wrapped",
446              p && !strcmp(p, "we feel\n"
447                           "our owls\n"
448                           "should\n"
449                           "live\n"
450                           "closer to\n"
451                           "our\n"
452                           "ponies."));
453  owl_editwin_unref(oe); oe = NULL;
454  owl_global_set_edit_maxwrapcols(&g, 70);
455
456  /* Test owl_editwin_current_column. */
457  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
458  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
459  owl_editwin_insert_string(oe, "abcdef");
460  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
461  owl_editwin_insert_string(oe, "\t");
462  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
463  owl_editwin_insert_string(oe, "123\n12\t3");
464  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
465  owl_editwin_move_to_beginning_of_line(oe);
466  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
467  owl_editwin_unref(oe); oe = NULL;
468
469  printf("# END testing owl_editwin (%d failures)\n", numfailed);
470
471  return numfailed;
472}
473
474int owl_fmtext_regtest(void) {
475  int numfailed = 0;
476  int start, end;
477  owl_fmtext fm1;
478  owl_fmtext fm2;
479  owl_regex re;
480  char *str;
481
482  printf("# BEGIN testing owl_fmtext\n");
483
484  owl_fmtext_init_null(&fm1);
485  owl_fmtext_init_null(&fm2);
486
487  /* Verify text gets correctly appended. */
488  owl_fmtext_append_normal(&fm1, "1234567898");
489  owl_fmtext_append_fmtext(&fm2, &fm1);
490  FAIL_UNLESS("string lengths correct",
491              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
492
493  /* Test owl_fmtext_num_lines. */
494  owl_fmtext_clear(&fm1);
495  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
496  owl_fmtext_append_normal(&fm1, "12345\n67898");
497  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
498  owl_fmtext_append_normal(&fm1, "\n");
499  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
500  owl_fmtext_append_bold(&fm1, "");
501  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
502
503  /* Test owl_fmtext_truncate_lines */
504  owl_fmtext_clear(&fm1);
505  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
506
507  owl_fmtext_clear(&fm2);
508  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
509  str = owl_fmtext_print_plain(&fm2);
510  FAIL_UNLESS("lines corrected truncated",
511              str && !strcmp(str, "1\n2\n3\n"));
512  owl_free(str);
513
514  owl_fmtext_clear(&fm2);
515  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
516  str = owl_fmtext_print_plain(&fm2);
517  FAIL_UNLESS("lines corrected truncated",
518              str && !strcmp(str, "1\n2\n3\n4\n"));
519  owl_free(str);
520
521  /* Test owl_fmtext_truncate_cols. */
522  owl_fmtext_clear(&fm1);
523  owl_fmtext_append_normal(&fm1, "123456789012345\n");
524  owl_fmtext_append_normal(&fm1, "123456789\n");
525  owl_fmtext_append_normal(&fm1, "1234567890\n");
526
527  owl_fmtext_clear(&fm2);
528  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
529  str = owl_fmtext_print_plain(&fm2);
530  FAIL_UNLESS("columns correctly truncated",
531              str && !strcmp(str, "567890"
532                                  "56789\n"
533                                  "567890"));
534  owl_free(str);
535
536  owl_fmtext_clear(&fm1);
537  owl_fmtext_append_normal(&fm1, "12\t1234");
538  owl_fmtext_append_bold(&fm1, "56\n");
539  owl_fmtext_append_bold(&fm1, "12345678\t\n");
540
541  owl_fmtext_clear(&fm2);
542  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
543  str = owl_fmtext_print_plain(&fm2);
544  FAIL_UNLESS("columns correctly truncated",
545              str && !strcmp(str, "    123456"
546                                  "5678      "));
547  owl_free(str);
548
549  /* Test owl_fmtext_expand_tabs. */
550  owl_fmtext_clear(&fm1);
551  owl_fmtext_append_normal(&fm1, "12\t1234");
552  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
553  owl_fmtext_clear(&fm2);
554  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
555  str = owl_fmtext_print_plain(&fm2);
556  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
557  FAIL_UNLESS("tabs corrected expanded",
558              str && !strcmp(str, "12      1234567 1\n"
559                                  "12345678        1"));
560  owl_free(str);
561
562  owl_fmtext_clear(&fm2);
563  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
564  str = owl_fmtext_print_plain(&fm2);
565  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
566  FAIL_UNLESS("tabs corrected expanded",
567              str && !strcmp(str, "12     1234567 1\n"
568                                  "12345678       1"));
569  owl_free(str);
570
571  /* Test owl_fmtext_search. */
572  owl_fmtext_clear(&fm1);
573  owl_fmtext_append_normal(&fm1, "123123123123");
574  owl_regex_create(&re, "12");
575  {
576    int count = 0, offset;
577    offset = owl_fmtext_search(&fm1, &re, 0);
578    while (offset >= 0) {
579      FAIL_UNLESS("search matches",
580                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
581      count++;
582      offset = owl_fmtext_search(&fm1, &re, offset+1);
583    }
584    FAIL_UNLESS("exactly four matches", count == 4);
585  }
586  owl_regex_cleanup(&re);
587
588  /* Test owl_fmtext_line_number. */
589  owl_fmtext_clear(&fm1);
590  owl_fmtext_append_normal(&fm1, "123\n456\n");
591  owl_fmtext_append_bold(&fm1, "");
592  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
593  FAIL_UNLESS("trailing formatting characters part of false line",
594              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
595  owl_regex_create_quoted(&re, "456");
596  FAIL_UNLESS("correctly find second line (line 1)",
597              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
598  owl_regex_cleanup(&re);
599
600  /* Test owl_fmtext_line_extents. */
601  owl_fmtext_clear(&fm1);
602  owl_fmtext_append_normal(&fm1, "123\n456\n789");
603  owl_fmtext_line_extents(&fm1, 1, &start, &end);
604  FAIL_UNLESS("line contents",
605              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
606  owl_fmtext_line_extents(&fm1, 2, &start, &end);
607  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
608
609  owl_fmtext_cleanup(&fm1);
610  owl_fmtext_cleanup(&fm2);
611
612  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
613
614  return numfailed;
615}
Note: See TracBrowser for help on using the repository browser.