-
Notifications
You must be signed in to change notification settings - Fork 7
/
testre.cpp
541 lines (506 loc) · 9.61 KB
/
testre.cpp
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
* regex tester
*
* testre [-n] [-tN] [-v] < testre.dat
*
* -n repeat each test with REG_NOSUB
* -tN time limit, N sec per test (default=10, no limit=0)
* -v list each test line
*
* see comments in testre.dat for description of format
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#include "regex.h"
#include <setjmp.h>
#include <signal.h>
#ifndef REG_NULL
#define REG_NULL 0
#endif
#ifndef REG_ANCH
#define REG_ANCH 0
#endif
#ifndef REG_LITERAL
#define REG_LITERAL 0
#endif
#ifndef REG_AUGMENTED
#define REG_AUGMENTED 0
#endif
#ifdef DEBUG /* tied to MDM's regex package */
#define MSTAT 1
#else
#define MSTAT 0
#endif
#define elementsof(x) (sizeof(x)/sizeof(x[0]))
#define streq(a,b) !strcmp(a,b)
#define NOTEST ~0
// constants for reading fields
#define SPEC_LEN 10
#define RE_LEN 1000
#define S_LEN 100000
#define ANS_LEN 500
struct codes {
int code;
const char *name;
} codes[] = {
{REG_NOMATCH, "NOMATCH"},
{REG_BADPAT, "BADPAT"},
{REG_ECOLLATE, "ECOLLATE"},
{REG_ECTYPE, "ECTYPE"},
{REG_EESCAPE, "EESCAPE"},
{REG_ESUBREG, "ESUBREG"},
{REG_EBRACK, "EBRACK"},
{REG_EPAREN, "EPAREN"},
{REG_EBRACE, "EBRACE"},
{REG_BADBR, "BADBR"},
{REG_ERANGE, "ERANGE"},
{REG_ESPACE, "ESPACE"},
{REG_BADRPT, "BADRPT"}
};
int errors;
int lineno;
const char *which;
int prog;
int nflag;
int verbose;
int timelim = 10;
const char *nosubmsg = "";
regmatch_t NOMATCH = {-2, -2};
static const char*
null(const char* s)
{
return s && *s ? s : "NULL";
}
static void
report(const char *comment, const char *re, const char *s)
{
errors++;
printf("%d:%s versus %s %s %s %s",
lineno, null(re), null(s), which, nosubmsg, comment);
}
static void
bad(const char *comment, const char *re, const char *s)
{
nosubmsg = "";
report(comment, re, s);
printf(", test run abandoned\n");
exit(1);
}
static void
doregerror(int code, regex_t *preg)
{
char buf[200];
const char *msg = buf;
switch(code) {
case -SIGBUS:
msg = "bus error";
break;
case -SIGSEGV:
msg = "memory fault";
break;
case -SIGALRM:
msg = "did not terminate";
break;
default:
regerror(code, preg, msg, sizeof buf);
break;
}
printf("%s\n", msg);
}
static int
readfield(int flen, char *f, char end)
{
int c;
int i;
for(i=0; i<flen; i++) {
*f = 0;
c = getc(stdin);
if(c == EOF)
return 1;
if(c == end)
break;
if(c == '\n')
return 1;
*f++ = c;
}
if(!i || i==flen)
return 1;
if(c == '\t') {
while(c == end)
c = getc(stdin);
ungetc(c, stdin);
}
return 0;
}
static int
hex(int c)
{
return isdigit(c)? c-'0':
isupper(c)? c-'A'+10:
c-'a'+10;
}
void escape(char *s)
{
char *t;
for(t=s; (*t=*s) != 0; s++, t++) {
if(*s != '\\')
continue;
switch(*++s) {
case 0:
*++t = 0;
break;
case 'n':
*t = '\n';
break;
case 'x':
if(!isxdigit(s[1]) || !isxdigit(s[2]))
bad("bad \\x\n", 0, 0);
*t = hex(*++s) << 4;
*t |= hex(*++s);
break;
default:
s--;
}
}
}
static void
getprog(char *ans)
{
char *s = ans + strlen(ans);
while(isdigit(s[-1]))
s--;
if(*s == 0)
prog = -1;
else
prog = atoi(s);
}
static int
readline(char *spec, char *re, char *s, char *ans)
{
int c = getchar();
switch(c) {
case EOF:
return 0;
case '#':
while(c != '\n')
c = getchar();
case '\n':
*spec = 0;
return 1;
}
ungetc(c, stdin);
if(readfield(SPEC_LEN, spec, '\t')) return 0;
if(readfield(RE_LEN, re, '\t')) return 0;
if(readfield(S_LEN, s, '\t')) return 0;
if(readfield(ANS_LEN, ans, '\n')) return 0;
escape(re);
escape(s);
getprog(ans);
return 1;
}
static void
matchprint(regmatch_t *match, int nmatch, int m)
{
int i;
for( ; nmatch>m; nmatch--)
if(match[nmatch-1].rm_so != -1)
break;
for(i=0; i<nmatch; i++) {
printf("(");
if(match[i].rm_so == -1)
printf("?");
else
printf("%ld", match[i].rm_so);
printf(",");
if(match[i].rm_eo == -1)
printf("?");
else
printf("%ld", match[i].rm_eo);
printf(")");
}
printf("\n");
fflush(stdout);
}
static void
matchcheck(int nmatch, regmatch_t *match, char *ans, char *re, char *s)
{
char *p;
int i, m, n;
int fail = 0;
for(i=0, p=ans; i<nmatch && *p; i++) {
if(*p++ != '(')
bad("improper answer\n", re, s);
if(*p == '?') {
m = -1;
p++;
} else
m = strtol(p, &p, 10);
if(*p++ != ',')
bad("improper answer\n", re, s);
if(*p == '?') {
n = -1;
p++;
} else
n = strtol(p, &p, 10);
if(*p++ != ')')
bad("improper answer\n", re, s);
if(m!=match[i].rm_so || n!=match[i].rm_eo)
fail++;
}
m = i;
if(fail) {
report("match was: ", re, s);
matchprint(match, nmatch, m);
return;
}
for( ; i<nmatch; i++) {
if(match[i].rm_so!=-1 || match[i].rm_eo!=-1) {
report("match was: ", re, s);
matchprint(match, nmatch, m);
return;
}
}
if(match[nmatch].rm_so != NOMATCH.rm_so) {
report("extra assignments to match array: ", re, s);
matchprint(match, nmatch+1, m);
}
}
int codeval(char *s)
{
size_t i;
for(i=0; i<elementsof(codes); i++)
if(streq(s, codes[i].name))
return codes[i].code;
return -1;
}
jmp_buf jbuf;
void gotcha(int sig)
{
alarm(0);
signal(sig, gotcha);
longjmp(jbuf, sig);
}
int alarmcomp(regex_t *preg, const char *re, int cflags)
{
int sig, ret;
sig = setjmp(jbuf);
if(sig == 0) {
alarm(timelim);
ret = regcomp(preg, re, cflags);
alarm(0);
} else
ret = -sig;
return ret;
}
int alarmexec(const regex_t *preg, const char *s, size_t nmatch, regmatch_t *match, int eflags)
{
int sig, ret;
sig = setjmp(jbuf);
if(sig == 0) {
alarm(timelim);
ret = regexec(preg, s, nmatch, match, eflags);
alarm(0);
} else
ret = -sig;
return ret;
}
#define nonstd(flag) (flag? flag: NOTEST)
int main(int argc, const char **argv)
{
int testno = 0;
int flags, cflags, eflags, are, bre, ere, lre;
char spec[SPEC_LEN];
char re[RE_LEN];
char s[S_LEN];
char ans[ANS_LEN];
regmatch_t match[100];
regex_t preg;
const char *p;
char *ep;
long int nmatch;
int cret, eret;
int i;
printf("TEST <regex>");
while((p = *++argv) && *p == '-')
for(;;)
{
switch(*++p)
{
case 0:
break;
case 'n':
nflag = 1;
printf(", NOSUB");
continue;
case 't':
if(*++p == 0)
p = "0";
timelim = atoi(p);
break;
case 'v':
verbose = 1;
printf(", verbose");
continue;
default:
printf(", invalid option %c", *p);
continue;
}
break;
}
if(p)
printf(", argument(s) ignored");
printf("\n");
signal(SIGALRM, gotcha);
// signal(SIGBUS, gotcha);
// signal(SIGSEGV, gotcha);
while(readline(spec, re, s, ans)) {
lineno++;
if(*spec == 0)
continue;
// if (verbose)
// printf("'%s' '%s' '%s' '%s'\n", spec, re, s, ans);
/* interpret: */
cflags = eflags = are = bre = ere = lre = 0;
nmatch = 20;
for(p=spec; *p; p++) {
if(isdigit(*p)) {
errno = 0;
nmatch = strtol(p, &ep, 10);
if((size_t)nmatch > elementsof(match) ||
(errno == ERANGE &&
(nmatch == LONG_MAX || nmatch == LONG_MIN)) ||
(errno != 0 && nmatch == 0))
bad("invalid nmatch", p, "strtol");
p = ep;
p--;
continue;
}
switch(*p) {
case 'A':
are = REG_AUGMENTED;
continue;
case 'B':
bre = 1;
continue;
case 'E':
ere = 1;
continue;
case 'L':
lre = REG_LITERAL;
continue;
case 'N':
cflags |= nonstd(REG_NOSUB);
continue;
case 'I':
cflags |= nonstd(REG_ICASE);
continue;
case 'W':
cflags |= nonstd(REG_NEWLINE);
continue;
case 'U':
cflags |= nonstd(REG_NULL);
continue;
case 'C':
cflags |= nonstd(REG_ANCH);
continue;
case 'b':
eflags |= nonstd(REG_NOTBOL);
continue;
case 'e':
eflags |= nonstd(REG_NOTEOL);
continue;
default:
bad("bad spec\n", re, s);
}
}
if(streq(re, "NULL"))
re[0] = 0;
if((cflags|eflags) == NOTEST)
continue;
compile:
fflush(stdout);
if(bre) {
which = "BRE";
bre = 0;
flags = cflags;
} else if(ere) {
which = "ERE";
ere = 0;
flags = cflags | REG_EXTENDED;
} else if(are) {
which = "ARE";
are = 0;
flags= cflags | REG_AUGMENTED;
} else if (lre) {
which = "LRE";
lre = 0;
flags = cflags | REG_LITERAL;
} else
continue;
nosub:
nosubmsg = (char*)((flags^cflags)®_NOSUB? " (NOSUB)": "");
testno++;
cret = alarmcomp(&preg, re, flags);
if(cret == 0) {
if(!streq(ans, "NULL") &&
!streq(ans,"NOMATCH") &&
ans[0]!='(') {
report("regcomp should fail and didn't", re, ans);
printf("\n");
continue;
}
} else if(streq(ans,"NULL") || ans[0]=='(') {
report("regcomp failed: ", re, ans);
doregerror(cret, &preg);
goto next;
} else if(cret==REG_BADPAT || cret==codeval(ans))
goto next;
else if(streq(ans, "BADPAT"))
goto next;
else {
report("regcomp failed with unexpected answer: ", re, ans);
errors--;
doregerror(cret, &preg);
goto next;
}
/* execute: */
for(i=0; i<elementsof(match); i++)
match[i] = NOMATCH;
if(streq(s, "NULL"))
s[0] = 0;
eret = alarmexec(&preg, s, nmatch, match, eflags);
if(prog >= 0) {
if(eret == prog)
eret = 0;
else
report("wrong progress mark:", re, s);
}
if(eret != 0) {
if(!streq(ans, "NOMATCH")) {
report("regexec failed: ", re, s);
doregerror(eret, &preg);
}
} else if(streq(ans,"NOMATCH")) {
report("regexec should fail and didn't: " ,re, s);
matchprint(match, nmatch, 0);
} else if(streq(ans,"NULL") || flags®_NOSUB)
matchcheck(0, match, ans, re, s);
else
matchcheck(nmatch, match, ans, re, s);
regfree(&preg);
next:
if(nflag && (flags®_NOSUB)==0) {
flags |= REG_NOSUB;
goto nosub;
}
goto compile;
}
printf("%d lines, ", lineno);
printf("%d tests, %d errors\n", testno, errors);
return 0;
}