1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2020 Keith Packard
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <regex.h>
39 #include <stdbool.h>
40 
41 #define MAX_MATCH	10
42 
43 struct test {
44 	const char	*pattern;
45 	const char	*string;
46 	int		ret;
47 	int		nmatch;
48 	regmatch_t matches[MAX_MATCH];
49 };
50 
51 struct test tests[] = {
52 	{ .pattern = ".", .string = "xxx",
53 	  .ret = 0, .nmatch = 1, .matches = { { .rm_so = 0, .rm_eo = 1 } }
54 	},
55 	{ .pattern = ".*", .string = "xxx",
56 	  .ret = 0, .nmatch = 1, .matches = { { .rm_so = 0, .rm_eo = 3 } }
57 	},
58 	{ .pattern = "(.)(.)(.)", .string = "xxx",
59 	  .ret = 0, .nmatch = 4, .matches = {
60 			{ .rm_so = 0, .rm_eo = 3 },
61 			{ .rm_so = 0, .rm_eo = 1 },
62 			{ .rm_so = 1, .rm_eo = 2 },
63 			{ .rm_so = 2, .rm_eo = 3 },
64 		},
65 	},
66 	{ .pattern = "x[a-c]*y", .string = "fooxaccabybar",
67 	  .ret = 0, .nmatch = 1, .matches = { { .rm_so = 3, .rm_eo = 10 } }
68 	},
69 };
70 
71 #define NTEST (sizeof(tests)/sizeof(tests[0]))
72 
73 int
main(void)74 main(void)
75 {
76 	unsigned	t;
77 	int	m;
78 	int	errors = 0;
79 	regmatch_t matches[MAX_MATCH];
80 	regex_t regex;
81 	int	ret;
82 	int	run = 0;
83 
84 	for (t = 0; t < NTEST; t++) {
85 		ret = regcomp(&regex, tests[t].pattern, REG_EXTENDED);
86 		if (ret != 0) {
87 			printf("expression \"%s\" failed to compile: %d\n", tests[t].pattern, ret);
88 			errors++;
89 			continue;
90 		}
91 		ret = regexec(&regex, tests[t].string, MAX_MATCH, matches, 0);
92 		regfree(&regex);
93 		if (ret != tests[t].ret) {
94 			printf("match \"%s\" with \"%s\" bad result got %d != expect %d\n",
95 			       tests[t].pattern, tests[t].string, ret, tests[t].ret);
96 			errors++;
97 			continue;
98 		}
99 		if (ret == 0) {
100 			for (m = 0; m < MAX_MATCH; m++) {
101 				if (m < tests[t].nmatch) {
102 					if (matches[m].rm_so != tests[t].matches[m].rm_so ||
103 					    matches[m].rm_eo != tests[t].matches[m].rm_eo) {
104 						printf("match %d wrong range got (%td,%td) expect (%td,%td)\n",
105 						       m,
106 						       matches[m].rm_so,
107 						       matches[m].rm_eo,
108 						       tests[t].matches[m].rm_so,
109 						       tests[t].matches[m].rm_eo);
110 						errors++;
111 					}
112 				} else {
113 					if (matches[m].rm_so != -1) {
114 						printf("match %d should be unused\n",
115 						       m);
116 						errors++;
117 					}
118 				}
119 			}
120 		}
121 		++run;
122 	}
123 	printf("regex: %d tests %d errors\n", run, errors);
124 	return errors;
125 }
126