test-fnmatch.c revision 290572
1
2#include <sys/cdefs.h>
3__FBSDID("$FreeBSD: head/lib/libc/tests/gen/test-fnmatch.c 290572 2015-11-09 06:24:11Z ngie $");
4
5#include <sys/param.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10
11#include "fnmatch_testcases.h"
12
13static int
14write_sh_tests(const char *progname, int num)
15{
16	size_t i;
17	struct testcase *t;
18
19	printf("# Generated by %s -s %d, do not edit.\n", progname, num);
20	printf("# $" "FreeBSD$\n");
21	printf("failures=\n");
22	printf("failed() { printf '%%s\\n' \"Failed: $1 '$2' '$3'\"; failures=x$failures; }\n");
23	if (num == 1) {
24		printf("testmatch() { eval \"case \\$2 in ''$1) ;; *) failed testmatch \\\"\\$@\\\";; esac\"; }\n");
25		printf("testnomatch() { eval \"case \\$2 in ''$1) failed testnomatch \\\"\\$@\\\";; esac\"; }\n");
26	} else if (num == 2) {
27		printf("# We do not treat a backslash specially in this case,\n");
28		printf("# but this is not the case in all shells.\n");
29		printf("netestmatch() { case $2 in $1) ;; *) failed netestmatch \"$@\";; esac; }\n");
30		printf("netestnomatch() { case $2 in $1) failed netestnomatch \"$@\";; esac; }\n");
31	}
32
33	for (i = 0; i < nitems(testcases); i++) {
34		t = &testcases[i];
35		if (strchr(t->pattern, '\'') != NULL ||
36		    strchr(t->string, '\'') != NULL)
37			continue;
38		if (t->flags == 0 && strcmp(t->pattern, "\\") == 0)
39			continue;
40		if (num == 1 && t->flags == 0)
41			printf("test%smatch '%s' '%s'\n",
42			    t->result == FNM_NOMATCH ? "no" : "",
43			    t->pattern, t->string);
44		if (num == 2 && (t->flags == FNM_NOESCAPE ||
45		    (t->flags == 0 && strchr(t->pattern, '\\') == NULL)))
46			printf("netest%smatch '%s' '%s'\n",
47			    t->result == FNM_NOMATCH ? "no" : "",
48			    t->pattern, t->string);
49	}
50	printf("[ -z \"$failures\" ]\n");
51	return 0;
52}
53
54static void
55usage(char *progname)
56{
57	fprintf(stderr, "usage: %s [-s num]\n", progname);
58	fprintf(stderr, "-s option writes tests for sh(1), num is 1 or 2\n");
59}
60
61int
62main(int argc, char *argv[])
63{
64	int opt;
65
66	while ((opt = getopt(argc, argv, "s:")) != -1) {
67		switch (opt) {
68		case 's':
69			return (write_sh_tests(argv[0], atoi(optarg)));
70		default:
71			usage(argv[0]);
72			exit(1);
73		}
74	}
75	usage(argv[0]);
76	exit(1);
77}
78