Home | History | Annotate | Line # | Download | only in regex
      1 /*	$NetBSD: t_regex_att.c,v 1.4 2021/02/23 16:00:37 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: t_regex_att.c,v 1.4 2021/02/23 16:00:37 christos Exp $");
     41 
     42 #include <sys/param.h>
     43 
     44 #include <atf-c.h>
     45 #include <ctype.h>
     46 #include <regex.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <util.h>
     51 #include <vis.h>
     52 
     53 static const char sep[] = "\r\n\t";
     54 static const char delim[3] = "\\\\\0";
     55 
     56 
     57 static void
     58 fail(const char *pattern, const char *input, size_t lineno) {
     59 	fprintf(stderr,
     60 	    "skipping failed test at line %zu (pattern=%s, input=%s)\n",
     61 	    lineno, pattern, input);
     62 }
     63 
     64 static int
     65 bug(const char *pattern, const char *input, size_t lineno) {
     66 	static const struct {
     67 		const char *p;
     68 		const char *i;
     69 	} b[] = {
     70 #if defined(REGEX_SPENCER)
     71 		/*
     72 		 * The default libc implementation by Henry Spencer
     73 		 */
     74 		{ "a[-]?c", "ac" },			// basic.dat
     75 		{ "(a*)*", "a" },			// categorization.dat
     76 		{ "(aba|a*b)*", "ababa" },		// categorization.dat
     77 		{ "\\(a\\(b\\)*\\)*\\2", "abab" },	// categorization.dat
     78 		{ "(a*)*", "aaaaaa" },			// nullsubexpression.dat
     79 		{ "(a*)*", "aaaaaax" },			// nullsubexpression.dat
     80 		{ "(a*)+", "a" },			// nullsubexpression.dat
     81 		{ "(a*)+", "aaaaaa" },			// nullsubexpression.dat
     82 		{ "(a*)+", "aaaaaax" },			// nullsubexpression.dat
     83 		{ "([a]*)*", "a" },			// nullsubexpression.dat
     84 		{ "([a]*)*", "aaaaaa" },		// nullsubexpression.dat
     85 		{ "([a]*)*", "aaaaaax" },		// nullsubexpression.dat
     86 		{ "([a]*)+", "a" },			// nullsubexpression.dat
     87 		{ "([a]*)+", "aaaaaa" },		// nullsubexpression.dat
     88 		{ "([a]*)+", "aaaaaax" },		// nullsubexpression.dat
     89 		{ "([^b]*)*", "a" },			// nullsubexpression.dat
     90 		{ "([^b]*)*", "aaaaaa" },		// nullsubexpression.dat
     91 		{ "([^b]*)*", "aaaaaab" },		// nullsubexpression.dat
     92 		{ "([ab]*)*", "a" },			// nullsubexpression.dat
     93 		{ "([ab]*)*", "aaaaaa" },		// nullsubexpression.dat
     94 		{ "([ab]*)*", "ababab" },		// nullsubexpression.dat
     95 		{ "([ab]*)*", "bababa" },		// nullsubexpression.dat
     96 		{ "([ab]*)*", "b" },			// nullsubexpression.dat
     97 		{ "([ab]*)*", "bbbbbb" },		// nullsubexpression.dat
     98 		{ "([ab]*)*", "aaaabcde" },		// nullsubexpression.dat
     99 		{ "([^a]*)*", "b" },			// nullsubexpression.dat
    100 		{ "([^a]*)*", "bbbbbb" },		// nullsubexpression.dat
    101 		{ "([^ab]*)*", "ccccxx" },		// nullsubexpression.dat
    102 		{ "\\(a*\\)*\\(x\\)", "ax" },		// nullsubexpression.dat
    103 		{ "\\(a*\\)*\\(x\\)", "axa" },		// nullsubexpression.dat
    104 		{ "\\(a*\\)*\\(x\\)\\(\\1\\)", "x" },	// nullsubexpression.dat
    105 /* crash! */	{ "\\(a*\\)*\\(x\\)\\(\\1\\)", "ax" },	// nullsubexpression.dat
    106 /* crash! */	{ "\\(a*\\)*\\(x\\)\\(\\1\\)\\(x\\)", "axxa" },	// ""
    107 		{ "(a*)*(x)",  "ax" },			// nullsubexpression.dat
    108 		{ "(a*)*(x)",  "axa" },			// nullsubexpression.dat
    109 		{ "(a*)+(x)",  "ax" },			// nullsubexpression.dat
    110 		{ "(a*)+(x)",  "axa" },			// nullsubexpression.dat
    111 		{ "((a|ab)(c|bcd))(d*)", "abcd" },	// forcedassoc.dat
    112 		{ "((a|ab)(bcd|c))(d*)", "abcd" },	// forcedassoc.dat
    113 		{ "((ab|a)(c|bcd))(d*)", "abcd" },	// forcedassoc.dat
    114 		{ "((ab|a)(bcd|c))(d*)", "abcd" },	// forcedassoc.dat
    115 		{ "((a*)(b|abc))(c*)", "abc" },		// forcedassoc.dat
    116 		{ "((a*)(abc|b))(c*)", "abc" },		// forcedassoc.dat
    117 		{ "((..)|(.)){2}", "aaa" },		// repetition.dat
    118 		{ "((..)|(.)){3}", "aaa" },		// repetition.dat
    119 		{ "((..)|(.)){3}", "aaaa" },		// repetition.dat
    120 		{ "((..)|(.)){3}", "aaaaa" },		// repetition.dat
    121 		{ "X(.?){0,}Y", "X1234567Y" },		// repetition.dat
    122 		{ "X(.?){1,}Y", "X1234567Y" },		// repetition.dat
    123 		{ "X(.?){2,}Y", "X1234567Y" },		// repetition.dat
    124 		{ "X(.?){3,}Y", "X1234567Y" },		// repetition.dat
    125 		{ "X(.?){4,}Y", "X1234567Y" },		// repetition.dat
    126 		{ "X(.?){5,}Y", "X1234567Y" },		// repetition.dat
    127 		{ "X(.?){6,}Y", "X1234567Y" },		// repetition.dat
    128 		{ "X(.?){7,}Y", "X1234567Y" },		// repetition.dat
    129 		{ "X(.?){0,8}Y", "X1234567Y" },		// repetition.dat
    130 		{ "X(.?){1,8}Y", "X1234567Y" },		// repetition.dat
    131 		{ "X(.?){2,8}Y", "X1234567Y" },		// repetition.dat
    132 		{ "X(.?){3,8}Y", "X1234567Y" },		// repetition.dat
    133 		{ "X(.?){4,8}Y", "X1234567Y" },		// repetition.dat
    134 		{ "X(.?){5,8}Y", "X1234567Y" },		// repetition.dat
    135 		{ "X(.?){6,8}Y", "X1234567Y" },		// repetition.dat
    136 		{ "X(.?){7,8}Y", "X1234567Y" },		// repetition.dat
    137 		{ "(a|ab|c|bcd){0,}(d*)", "ababcd" },	// repetition.dat
    138 		{ "(a|ab|c|bcd){1,}(d*)", "ababcd" },	// repetition.dat
    139 		{ "(a|ab|c|bcd){2,}(d*)", "ababcd" },	// repetition.dat
    140 		{ "(a|ab|c|bcd){3,}(d*)", "ababcd" },	// repetition.dat
    141 		{ "(a|ab|c|bcd){1,10}(d*)", "ababcd" },	// repetition.dat
    142 		{ "(a|ab|c|bcd){2,10}(d*)", "ababcd" },	// repetition.dat
    143 		{ "(a|ab|c|bcd){3,10}(d*)", "ababcd" },	// repetition.dat
    144 		{ "(a|ab|c|bcd)*(d*)", "ababcd" },	// repetition.dat
    145 		{ "(a|ab|c|bcd)+(d*)", "ababcd" },	// repetition.dat
    146 		{ "(ab|a|c|bcd){0,}(d*)", "ababcd" },	// repetition.dat
    147 		{ "(ab|a|c|bcd){1,}(d*)", "ababcd" },	// repetition.dat
    148 		{ "(ab|a|c|bcd){2,}(d*)", "ababcd" },	// repetition.dat
    149 		{ "(ab|a|c|bcd){3,}(d*)", "ababcd" },	// repetition.dat
    150 		{ "(ab|a|c|bcd){1,10}(d*)", "ababcd" },	// repetition.dat
    151 		{ "(ab|a|c|bcd){2,10}(d*)", "ababcd" },	// repetition.dat
    152 		{ "(ab|a|c|bcd){3,10}(d*)", "ababcd" },	// repetition.dat
    153 		{ "(ab|a|c|bcd)*(d*)", "ababcd" },	// repetition.dat
    154 		{ "(ab|a|c|bcd)+(d*)", "ababcd" },	// repetition.dat
    155 #elif defined(REGEX_TRE)
    156 		{ "a[-]?c", "ac" },			// basic.dat
    157 		{ "a\\(b\\)*\\1", "a" },		// categorization.dat
    158 		{ "a\\(b\\)*\\1", "abab" },		// categorization.dat
    159 		{ "\\(a\\(b\\)*\\)*\\2", "abab" },	// categorization.dat
    160 		{ "\\(a*\\)*\\(x\\)\\(\\1\\)", "ax" },	// categorization.dat
    161 		{ "\\(a*\\)*\\(x\\)\\(\\1\\)\\(x\\)", "axxa" },	// ""
    162 		{ "((..)|(.))*", "aa" },		// repetition.dat
    163 		{ "((..)|(.))*", "aaa" },		// repetition.dat
    164 		{ "((..)|(.))*", "aaaaa" },		// repetition.dat
    165 		{ "X(.?){7,}Y", "X1234567Y" },		// repetition.dat
    166 #else
    167 		{ "", "" }
    168 #endif
    169 	};
    170 
    171 	for (size_t i = 0; i < __arraycount(b); i++) {
    172 		if (strcmp(pattern, b[i].p) == 0 &&
    173 		    strcmp(input, b[i].i) == 0) {
    174 			fail(pattern, input, lineno);
    175 			return 1;
    176 		}
    177 	}
    178 	return 0;
    179 }
    180 
    181 #ifdef REGEX_SPENCER
    182 #define HAVE_BRACES	1
    183 #define HAVE_MINIMAL	0
    184 #endif
    185 #ifndef HAVE_BRACES
    186 #define HAVE_BRACES	1
    187 #endif
    188 #ifndef HAVE_MINIMAL
    189 #define HAVE_MINIMAL	1
    190 #endif
    191 
    192 static int
    193 optional(const char *s)
    194 {
    195 	static const struct{
    196 		const char *n;
    197 		int v;
    198 	} nv[]= {
    199 		{ "[[<element>]] not supported", HAVE_BRACES },
    200 		{ "no *? +? mimimal match ops", HAVE_MINIMAL },
    201 	};
    202 
    203 	for (size_t i = 0; i < __arraycount(nv); i++)
    204 		if (strcmp(nv[i].n, s) == 0) {
    205 			if (nv[i].v)
    206 				return 0;
    207 			fprintf(stderr, "skipping unsupported [%s] tests\n", s);
    208 			return 1;
    209 		}
    210 
    211 	ATF_REQUIRE_MSG(0, "Unknown feature: %s", s);
    212 	return 0;
    213 }
    214 
    215 static int
    216 unsupported(const char *s)
    217 {
    218 	static const char *we[] = {
    219 #if defined(REGEX_SPENCER)
    220 		"ASSOCIATIVITY=left",		// have right associativity
    221 		"SUBEXPRESSION=precedence",	// have grouping subexpression
    222 		"REPEAT_LONGEST=last",		// have first repeat longest
    223 		"BUG=alternation-order",	// don't have it
    224 		"BUG=first-match",		// don't have it
    225 		"BUG=nomatch-match",		// don't have it
    226 		"BUG=repeat-any",		// don't have it
    227 		"BUG=range-null",		// don't have it
    228 		"BUG=repeat-null-unknown",	// don't have it
    229 		"BUG=repeat-null",		// don't have it
    230 		"BUG=repeat-artifact",		// don't have it
    231 		"BUG=subexpression-first",	// don't have it
    232 #elif defined(REGEX_TRE)
    233 		"ASSOCIATIVITY=right",		// have left associativity
    234 		"SUBEXPRESSION=grouping",	// have precedence subexpression
    235 		"REPEAT_LONGEST=first",		// have last repeat longest
    236 		"LENGTH=first",			// have last length
    237 		"BUG=alternation-order",	// don't have it
    238 		"BUG=first-match",		// don't have it
    239 		"BUG=range-null",		// don't have it
    240 		"BUG=repeat-null",		// don't have it
    241 		"BUG=repeat-artifact",		// don't have it
    242 		"BUG=subexpression-first",	// don't have it
    243 		"BUG=repeat-short",		// don't have it
    244 #endif
    245 	};
    246 
    247 	if (s == NULL)
    248 		return 0;
    249 
    250 	while (*s == '#' || isspace((unsigned char)*s))
    251 		s++;
    252 
    253 	for (size_t i = 0; i < __arraycount(we); i++)
    254 		if (strcmp(we[i], s) == 0)
    255 			return 1;
    256 	return 0;
    257 }
    258 
    259 static void
    260 geterror(const char *s, int *comp, int *exec)
    261 {
    262 	static const struct {
    263 		const char *n;
    264 		int v;
    265 		int ce;
    266 	} nv[] = {
    267 #define COMP 1
    268 #define EXEC 2
    269 		{ "OK", 0, COMP|EXEC },
    270 #define _DO(a, b)	{ # a, REG_ ## a, b },
    271 		_DO(NOMATCH, EXEC)
    272 		_DO(BADPAT, COMP)
    273 		_DO(ECOLLATE, COMP)
    274 		_DO(ECTYPE, COMP)
    275 		_DO(EESCAPE, COMP)
    276 		_DO(ESUBREG, COMP)
    277 		_DO(EBRACK, COMP)
    278 		_DO(EPAREN, COMP)
    279 		_DO(EBRACE, COMP)
    280 		_DO(BADBR, COMP)
    281 		_DO(ERANGE, COMP)
    282 		_DO(ESPACE, EXEC)
    283 		_DO(BADRPT, COMP)
    284 		_DO(EMPTY, COMP)
    285 		_DO(ASSERT, COMP)
    286 		_DO(INVARG, COMP)
    287 #ifdef REG_ENOSYS
    288 		_DO(ENOSYS, COMP)
    289 #endif
    290 #ifdef REG_ILLSEQ
    291 		_DO(ILLSEQ, COMP)
    292 #endif
    293 #undef _DO
    294 	};
    295 	*comp = 0;
    296 	*exec = 0;
    297 	for (size_t i = 0; i < __arraycount(nv); i++)
    298 		if (strcmp(s, nv[i].n) == 0) {
    299 			if (nv[i].ce & COMP)
    300 				*comp = nv[i].v;
    301 			if (nv[i].ce & EXEC)
    302 				*exec = nv[i].v;
    303 			return;
    304 		}
    305 	ATF_REQUIRE_MSG(0, "Unknown error %s", s);
    306 	return;
    307 }
    308 
    309 static int
    310 getflags(char *s)
    311 {
    312 	int flags = 0;
    313 
    314 	for (;; s++)
    315 		switch (*s) {
    316 		case '0': case '1': case '2': case '3': case '4':
    317 		case '5': case '6': case '7': case '8': case '9':
    318 			*s = '\0';
    319 			break;
    320 		case '\0':
    321 			return flags;
    322 		case 'B':
    323 		case 'E':
    324 		case 'F':
    325 		case 'L':
    326 			break;
    327 		case 'i':
    328 			flags |= REG_ICASE;
    329 			*s = '\0';
    330 			break;
    331 		case '$':
    332 			*s = '\0';
    333 			break;
    334 		case 'n':
    335 			*s = '\0';
    336 			break;
    337 		default:
    338 			ATF_REQUIRE_MSG(0, "Unknown char %c", *s);
    339 			break;
    340 		}
    341 }
    342 
    343 static size_t
    344 getmatches(const char *s)
    345 {
    346 	size_t i;
    347 	char *q;
    348 	for (i = 0; (q = strchr(s, '(')) != NULL; i++, s = q + 1)
    349 		continue;
    350 	ATF_REQUIRE_MSG(i != 0, "No parentheses found");
    351 	return i;
    352 }
    353 
    354 static void
    355 checkcomment(const char *s, size_t lineno)
    356 {
    357 	if (s && strstr(s, "BUG") != NULL)
    358 		fprintf(stderr, "Expected %s at line %zu\n", s, lineno);
    359 }
    360 
    361 static void
    362 checkmatches(const char *matches, size_t nm, const regmatch_t *pm,
    363     size_t lineno)
    364 {
    365 	if (nm == 0)
    366 		return;
    367 
    368 	char *res;
    369 	size_t len = strlen(matches) + 1, off = 0;
    370 
    371 	ATF_REQUIRE((res = strdup(matches)) != NULL);
    372 	for (size_t i = 0; i < nm; i++) {
    373 		int l;
    374 		if (pm[i].rm_so == -1 && pm[i].rm_eo == -1)
    375 			l = snprintf(res + off, len - off, "(?,?)");
    376 		else
    377 			l = snprintf(res + off, len - off, "(%lld,%lld)",
    378 			    (long long)pm[i].rm_so, (long long)pm[i].rm_eo);
    379 		ATF_REQUIRE_MSG((size_t) l < len - off, "String too long %s"
    380 		    " cur=%d, max=%zu", res, l, len - off);
    381 		off += l;
    382 	}
    383 	ATF_CHECK_STREQ_MSG(res, matches, " at line %zu", lineno);
    384 	free(res);
    385 }
    386 
    387 static void
    388 att_test(const struct atf_tc *tc, const char *data_name)
    389 {
    390 	regex_t re;
    391 	char *line, *lastpattern = NULL, data_path[MAXPATHLEN];
    392 	size_t len, lineno = 0;
    393 	int skipping = 0;
    394 	FILE *input_file;
    395 
    396 	snprintf(data_path, sizeof(data_path), "%s/data/%s.dat",
    397 	    atf_tc_get_config_var(tc, "srcdir"), data_name);
    398 
    399 	input_file = fopen(data_path, "r");
    400 	if (input_file == NULL)
    401 		atf_tc_fail("Failed to open input file %s", data_path);
    402 
    403 	for (; (line = fparseln(input_file, &len, &lineno, delim, 0))
    404 	    != NULL; free(line)) {
    405 		char *name, *pattern, *input, *matches, *comment;
    406 		regmatch_t *pm;
    407 		size_t nm;
    408 #ifdef DEBUG
    409 		fprintf(stderr, "[%s]\n", line);
    410 #endif
    411 		if ((name = strtok(line, sep)) == NULL)
    412 			continue;
    413 
    414 		/*
    415 		 * We check these early so that we skip the lines quickly
    416 		 * in order to do more strict testing on the other arguments
    417 		 * The same characters are also tested in the switch below
    418 		 */
    419 		if (*name == '}') {
    420 			skipping = 0;
    421 			continue;
    422 		}
    423 		if (skipping)
    424 			continue;
    425 		if (*name == ';' || *name == '#' || strcmp(name, "NOTE") == 0)
    426 			continue;
    427 		if (*name == ':') {
    428 			/* Skip ":HA#???:" prefix */
    429 			while (*++name && *name != ':')
    430 				continue;
    431 			if (*name)
    432 				name++;
    433 		}
    434 
    435 		ATF_REQUIRE_MSG((pattern = strtok(NULL, sep)) != NULL,
    436 			"Missing pattern at line %zu", lineno);
    437 		ATF_REQUIRE_MSG((input = strtok(NULL, sep)) != NULL,
    438 			"Missing input at line %zu", lineno);
    439 
    440 		if (strchr(name, '$')) {
    441 			ATF_REQUIRE(strunvis(pattern, pattern) != -1);
    442 			ATF_REQUIRE(strunvis(input, input) != -1);
    443 		}
    444 
    445 
    446 		if (strcmp(input, "NULL") == 0)
    447 			*input = '\0';
    448 
    449 		if (strcmp(pattern, "SAME") == 0) {
    450 			ATF_REQUIRE(lastpattern != NULL);
    451 			pattern = lastpattern;
    452 		} else {
    453 			free(lastpattern);
    454 			ATF_REQUIRE((lastpattern = strdup(pattern)) != NULL);
    455 		}
    456 
    457 		ATF_REQUIRE_MSG((matches = strtok(NULL, sep)) != NULL,
    458 		    "Missing matches at line %zu", lineno);
    459 
    460 		comment = strtok(NULL, sep);
    461 		switch (*name) {
    462 		case '{':	/* Begin optional implementation */
    463 			if (optional(comment)) {
    464 				skipping++;
    465 				continue;
    466 			}
    467 			name++;	/* We have it, so ignore */
    468 			break;
    469 		case '}':	/* End optional implementation */
    470 			skipping = 0;
    471 			continue;
    472 		case '?':	/* Optional */
    473 		case '|':	/* Alternative */
    474 			if (unsupported(comment))
    475 				continue;
    476 			name++;	/* We have it, so ignore */
    477 			break;
    478 		case '#':	/* Comment */
    479 		case ';':	/* Skip */
    480 			continue;
    481 		default:
    482 			break;
    483 		}
    484 
    485 		/* XXX: Our bug */
    486 		if (bug(pattern, input, lineno))
    487 			continue;
    488 
    489 		int comp, exec;
    490 		if (*matches != '(') {
    491 			geterror(matches, &comp, &exec);
    492 			pm = NULL;
    493 			nm = 0;
    494 		} else {
    495 			comp = exec = 0;
    496 			nm = getmatches(matches);
    497 			ATF_REQUIRE((pm = calloc(nm, sizeof(*pm))) != NULL);
    498 		}
    499 
    500 
    501 
    502 		int iflags = getflags(name);
    503 		for (; *name; name++) {
    504 			int flags;
    505 			switch (*name) {
    506 			case 'B':
    507 				flags = REG_BASIC;
    508 				break;
    509 			case 'E':
    510 				flags = REG_EXTENDED;
    511 				break;
    512 			case 'L':
    513 				flags = REG_NOSPEC;
    514 				break;
    515 			default:
    516 				ATF_REQUIRE_MSG(0, "Bad name %c", *name);
    517 				continue;
    518 			}
    519 			int c = regcomp(&re, pattern, flags | iflags);
    520 			ATF_REQUIRE_MSG(c == comp,
    521 			    "regcomp returned %d for pattern %s at line %zu",
    522 			    c, pattern, lineno);
    523 			if (c)
    524 				continue;
    525 			int e = regexec(&re, input, nm, pm, 0);
    526 			ATF_REQUIRE_MSG(e == exec, "Expected error %d,"
    527 			    " got %d at line %zu", exec, e, lineno);
    528 			checkmatches(matches, nm, pm, lineno);
    529 			checkcomment(comment, lineno);
    530 			regfree(&re);
    531 		}
    532 		free(pm);
    533 	}
    534 
    535 	fclose(input_file);
    536 }
    537 
    538 ATF_TC(basic);
    539 ATF_TC_HEAD(basic, tc)
    540 {
    541 	atf_tc_set_md_var(tc, "descr", "Tests basic functionality");
    542 }
    543 ATF_TC_BODY(basic, tc)
    544 {
    545 	att_test(tc, "basic");
    546 }
    547 
    548 ATF_TC(categorization);
    549 ATF_TC_HEAD(categorization, tc)
    550 {
    551 	atf_tc_set_md_var(tc, "descr", "Tests implementation categorization");
    552 }
    553 ATF_TC_BODY(categorization, tc)
    554 {
    555 	att_test(tc, "categorization");
    556 }
    557 
    558 ATF_TC(nullsubexpr);
    559 ATF_TC_HEAD(nullsubexpr, tc)
    560 {
    561 	atf_tc_set_md_var(tc, "descr", "Tests (...)*");
    562 }
    563 ATF_TC_BODY(nullsubexpr, tc)
    564 {
    565 	att_test(tc, "nullsubexpr");
    566 }
    567 
    568 ATF_TC(leftassoc);
    569 ATF_TC_HEAD(leftassoc, tc)
    570 {
    571 	atf_tc_set_md_var(tc, "descr", "Tests left-associative "
    572 	    "implementations");
    573 }
    574 ATF_TC_BODY(leftassoc, tc)
    575 {
    576 #if SKIP_LEFTASSOC
    577 	/* jmmv: I converted the original shell-based tests to C and they
    578 	 * disabled this test in a very unconventional way without giving
    579 	 * any explation.  Mark as broken here, but I don't know why. */
    580 	atf_tc_expect_fail("Reason for breakage unknown");
    581 #endif
    582 	att_test(tc, "leftassoc");
    583 }
    584 
    585 ATF_TC(rightassoc);
    586 ATF_TC_HEAD(rightassoc, tc)
    587 {
    588 	atf_tc_set_md_var(tc, "descr", "Tests right-associative "
    589 	    "implementations");
    590 }
    591 ATF_TC_BODY(rightassoc, tc)
    592 {
    593 #if SKIP_RIGHTASSOC
    594 	/* jmmv: I converted the original shell-based tests to C and they
    595 	 * disabled this test in a very unconventional way without giving
    596 	 * any explation.  Mark as broken here, but I don't know why. */
    597 	atf_tc_expect_fail("Reason for breakage unknown");
    598 #endif
    599 	att_test(tc, "rightassoc");
    600 }
    601 
    602 ATF_TC(forcedassoc);
    603 ATF_TC_HEAD(forcedassoc, tc)
    604 {
    605 	atf_tc_set_md_var(tc, "descr", "Tests subexpression grouping to "
    606 	    "force association");
    607 }
    608 ATF_TC_BODY(forcedassoc, tc)
    609 {
    610 	att_test(tc, "forcedassoc");
    611 }
    612 
    613 ATF_TC(repetition);
    614 ATF_TC_HEAD(repetition, tc)
    615 {
    616 	atf_tc_set_md_var(tc, "descr", "Tests implicit vs. explicit "
    617 	    "repetition");
    618 }
    619 ATF_TC_BODY(repetition, tc)
    620 {
    621 	att_test(tc, "repetition");
    622 }
    623 
    624 ATF_TP_ADD_TCS(tp)
    625 {
    626 
    627 	ATF_TP_ADD_TC(tp, basic);
    628 	ATF_TP_ADD_TC(tp, categorization);
    629 	ATF_TP_ADD_TC(tp, nullsubexpr);
    630 	ATF_TP_ADD_TC(tp, leftassoc);
    631 	ATF_TP_ADD_TC(tp, rightassoc);
    632 	ATF_TP_ADD_TC(tp, forcedassoc);
    633 	ATF_TP_ADD_TC(tp, repetition);
    634 	return atf_no_error();
    635 }
    636