forked from barnowl/barnowl
/
tester.c
408 lines (332 loc) · 14.4 KB
/
tester.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#define OWL_PERL
#define WINDOW FAKE_WINDOW
#include "owl.h"
#undef WINDOW
#include <unistd.h>
#include <stdlib.h>
#undef instr
#include <curses.h>
owl_global g;
int numtests;
int owl_regtest(void);
int owl_util_regtest(void);
int owl_dict_regtest(void);
int owl_variable_regtest(void);
int owl_filter_regtest(void);
int owl_obarray_regtest(void);
int owl_editwin_regtest(void);
extern void owl_perl_xs_init(pTHX);
int main(int argc, char **argv, char **env)
{
FILE *rnull;
FILE *wnull;
char *perlerr;
int status = 0;
if (argc <= 1) {
fprintf(stderr, "Usage: %s --builtin|TEST.t|-le CODE\n", argv[0]);
return 1;
}
/* initialize a fake ncurses, detached from std{in,out} */
wnull = fopen("/dev/null", "w");
rnull = fopen("/dev/null", "r");
newterm("xterm", wnull, rnull);
/* initialize global structures */
owl_global_init(&g);
perlerr = owl_perlconfig_initperl(NULL, &argc, &argv, &env);
if (perlerr) {
endwin();
fprintf(stderr, "Internal perl error: %s\n", perlerr);
status = 1;
goto out;
}
owl_global_complete_setup(&g);
owl_global_setup_default_filters(&g);
owl_view_create(owl_global_get_current_view(&g), "main",
owl_global_get_filter(&g, "all"),
owl_global_get_style_by_name(&g, "default"));
owl_function_firstmsg();
ENTER;
SAVETMPS;
if (strcmp(argv[1], "--builtin") == 0) {
status = owl_regtest();
} else if (strcmp(argv[1], "-le") == 0 && argc > 2) {
/*
* 'prove' runs its harness perl with '-le CODE' to get some
* information out.
*/
moreswitches("l");
eval_pv(argv[2], true);
} else {
sv_setpv(get_sv("0", false), argv[1]);
sv_setpv(get_sv("main::test_prog", TRUE), argv[1]);
eval_pv("do $main::test_prog; die($@) if($@)", true);
}
status = 0;
FREETMPS;
LEAVE;
out:
perl_destruct(owl_global_get_perlinterp(&g));
perl_free(owl_global_get_perlinterp(&g));
/* probably not necessary, but tear down the screen */
endwin();
fclose(rnull);
fclose(wnull);
return status;
}
int owl_regtest(void) {
numtests = 0;
int numfailures=0;
/*
printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
+OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
*/
numfailures += owl_util_regtest();
numfailures += owl_dict_regtest();
numfailures += owl_variable_regtest();
numfailures += owl_filter_regtest();
numfailures += owl_editwin_regtest();
if (numfailures) {
fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
}
printf("1..%d\n", numtests);
return(numfailures);
}
#define FAIL_UNLESS(desc,pred) do { int __pred = (pred); \
numtests++; \
printf("%s %s", (__pred)?"ok":(numfailed++,"not ok"), desc); \
if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0)
int owl_util_regtest(void)
{
int numfailed=0;
printf("# BEGIN testing owl_util\n");
FAIL_UNLESS("owl_util_substitute 1",
!strcmp("foo", owl_text_substitute("foo", "", "Y")));
FAIL_UNLESS("owl_text_substitute 2",
!strcmp("fYZYZ", owl_text_substitute("foo", "o", "YZ")));
FAIL_UNLESS("owl_text_substitute 3",
!strcmp("foo", owl_text_substitute("fYZYZ", "YZ", "o")));
FAIL_UNLESS("owl_text_substitute 4",
!strcmp("/u/foo/meep", owl_text_substitute("~/meep", "~", "/u/foo")));
FAIL_UNLESS("skiptokens 1",
!strcmp("bar quux", skiptokens("foo bar quux", 1)));
FAIL_UNLESS("skiptokens 2",
!strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
FAIL_UNLESS("expand_tabs 1",
!strcmp(" hi", owl_text_expand_tabs("\thi")));
FAIL_UNLESS("expand_tabs 2",
!strcmp(" hi\nword tab", owl_text_expand_tabs("\thi\nword\ttab")));
FAIL_UNLESS("expand_tabs 3",
!strcmp(" 2 tabs", owl_text_expand_tabs("\t\t2 tabs")));
FAIL_UNLESS("expand_tabs 4",
!strcmp("α ααααααα! ", owl_text_expand_tabs("α\tααααααα!\t")));
FAIL_UNLESS("expand_tabs 5",
!strcmp("A AAA!! ", owl_text_expand_tabs("A\tAAA!!\t")));
FAIL_UNLESS("skiptokens 1",
!strcmp("world", skiptokens("hello world", 1)));
FAIL_UNLESS("skiptokens 2",
!strcmp("c d e", skiptokens("a b c d e", 2)));
FAIL_UNLESS("skiptokens 3",
!strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
FAIL_UNLESS("skiptokens 4",
!strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
FAIL_UNLESS("skiptokens 5",
!strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
/* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
printf("# END testing owl_util (%d failures)\n", numfailed);
return(numfailed);
}
int owl_dict_regtest(void) {
owl_dict d;
owl_list l;
int numfailed=0;
char *av="aval", *bv="bval", *cv="cval", *dv="dval";
printf("# BEGIN testing owl_dict\n");
FAIL_UNLESS("create", 0==owl_dict_create(&d));
FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l));
FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l));
/* these assume the returned keys are sorted */
FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0)));
FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1)));
FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2)));
owl_list_cleanup(&l, owl_free);
owl_dict_cleanup(&d, NULL);
/* if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
printf("# END testing owl_dict (%d failures)\n", numfailed);
return(numfailed);
}
int owl_variable_regtest(void) {
owl_vardict vd;
int numfailed=0;
char buf[1024];
const void *v;
printf("# BEGIN testing owl_variable\n");
FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping"));
FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"rxping", buf, 1024));
FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off"));
FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0));
FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping"));
FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0));
FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping"));
FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024));
FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8"));
FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0));
FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0));
FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0));
FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar", OWL_VARIABLE_STRING)));
FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar")));
owl_variable_set_string(&vd, "stringvar", "new val");
FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar")));
owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar", OWL_VARIABLE_INT)));
FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar"));
owl_variable_set_int(&vd, "intvar", 17);
FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar"));
owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar", OWL_VARIABLE_BOOL)));
FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar"));
owl_variable_set_bool_off(&vd, "boolvar");
FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar"));
owl_variable_dict_cleanup(&vd);
/* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
printf("# END testing owl_variable (%d failures)\n", numfailed);
return(numfailed);
}
static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
{
owl_filter *f;
int ok;
int failed = 0;
if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
printf("not ok can't parse %s\n", filt);
failed = 1;
goto out;
}
ok = owl_filter_message_match(f, m);
if((shouldmatch && !ok) || (!shouldmatch && ok)) {
printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
failed = 1;
}
out:
owl_filter_delete(f);
if(!failed) {
printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
}
return failed;
}
int owl_filter_regtest(void) {
int numfailed=0;
owl_message m;
owl_filter *f1, *f2, *f3, *f4, *f5;
owl_dict_create(&g.filters);
g.filterlist = NULL;
owl_message_init(&m);
owl_message_set_type_zephyr(&m);
owl_message_set_direction_in(&m);
owl_message_set_class(&m, "owl");
owl_message_set_instance(&m, "tester");
owl_message_set_sender(&m, "owl-user");
owl_message_set_recipient(&m, "joe");
owl_message_set_attribute(&m, "foo", "bar");
#define TEST_FILTER(f, e) do { \
numtests++; \
numfailed += owl_filter_test_string(f, &m, e); \
} while(0)
TEST_FILTER("true", 1);
TEST_FILTER("false", 0);
TEST_FILTER("( true )", 1);
TEST_FILTER("not false", 1);
TEST_FILTER("( true ) or ( false )", 1);
TEST_FILTER("true and false", 0);
TEST_FILTER("( true or true ) or ( ( false ) )", 1);
TEST_FILTER("class owl", 1);
TEST_FILTER("class ^owl$", 1);
TEST_FILTER("instance test", 1);
TEST_FILTER("instance ^test$", 0);
TEST_FILTER("instance ^tester$", 1);
TEST_FILTER("foo bar", 1);
TEST_FILTER("class owl and instance tester", 1);
TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
/* Order of operations and precedence */
TEST_FILTER("not true or false", 0);
TEST_FILTER("true or true and false", 0);
TEST_FILTER("true and true and false or true", 1);
TEST_FILTER("false and false or true", 1);
TEST_FILTER("true and false or false", 0);
f1 = owl_filter_new_fromstring("f1", "class owl");
owl_global_add_filter(&g, f1);
TEST_FILTER("filter f1", 1);
owl_global_remove_filter(&g, "f1");
/* Test recursion prevention */
FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
owl_filter_delete(f2);
/* mutual recursion */
f3 = owl_filter_new_fromstring("f3", "filter f4");
owl_global_add_filter(&g, f3);
FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
owl_global_remove_filter(&g, "f3");
owl_filter_delete(f4);
/* support referencing a filter several times */
FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
owl_filter_delete(f5);
return 0;
}
int owl_editwin_regtest(void) {
int numfailed = 0;
const char *p;
printf("# BEGIN testing owl_editwin\n");
owl_editwin *oe;
oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
/* TODO: make the strings a little more lenient w.r.t trailing whitespace */
/* check paragraph fill */
owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
owl_editwin_move_to_top(oe);
owl_editwin_fill_paragraph(oe);
p = owl_editwin_get_text(oe);
FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
"blah blah blah.\n"
"\n"
"blah"));
owl_editwin_delete(oe); oe = NULL;
oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
/* check that lines ending with ". " correctly fill */
owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
owl_editwin_move_to_top(oe);
owl_editwin_fill_paragraph(oe);
p = owl_editwin_get_text(oe);
FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
"blah blah blah. \n"
"\n"
"blah"));
owl_editwin_delete(oe); oe = NULL;
printf("# END testing owl_editwin (%d failures)\n", numfailed);
return numfailed;
}