Built win.arm64 against r3658
[kbuild-mirror.git] / src / oldsed / testsuite / tst-regex2.c
blob6f487f84da6a94324d0bafc9344ecde3de1679aa
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <regex.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
10 int
11 main (int argc, char *argv[])
13 struct stat st;
14 static const char *pat[] = {
15 ".?.?.?.?.?.?.?argc",
16 "(.?)(.?)(.?)(.?)(.?)(.?)(.?)argc",
17 "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))"
18 "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))"
19 "((((((((((.?))))))))))argc" };
21 size_t len;
22 int fd;
23 int testno, i, j, k, l;
24 char *string;
25 char *buf;
27 if (argc < 2)
28 abort ();
30 fd = open (argv[1], O_RDONLY);
31 if (fd < 0)
33 printf ("Couldn't open %s: %s\n", argv[1], strerror (errno));
34 abort ();
37 if (fstat (fd, &st) < 0)
39 printf ("Couldn't fstat %s: %s\n", argv[1], strerror (errno));
40 abort ();
43 buf = malloc (st.st_size + 1);
44 if (buf == NULL)
46 printf ("Couldn't allocate buffer: %s\n", strerror (errno));
47 abort ();
50 if (read (fd, buf, st.st_size) != (ssize_t) st.st_size)
52 printf ("Couldn't read %s", argv[1]);
53 abort ();
56 close (fd);
57 buf[st.st_size] = '\0';
59 string = buf;
60 len = st.st_size;
62 for (testno = 0; testno < 4; ++testno)
63 for (i = 0; i < sizeof (pat) / sizeof (pat[0]); ++i)
65 regex_t rbuf;
66 struct re_pattern_buffer rpbuf;
67 int err;
69 printf ("test %d pattern %d", testno, i);
70 if (testno < 2)
72 err = regcomp (&rbuf, pat[i],
73 REG_EXTENDED | (testno ? REG_NOSUB : 0));
74 if (err != 0)
76 char errstr[300];
77 putchar ('\n');
78 regerror (err, &rbuf, errstr, sizeof (errstr));
79 puts (errstr);
80 return err;
83 else
85 const char *s;
86 re_set_syntax (RE_SYNTAX_POSIX_EGREP
87 | (testno == 3 ? RE_NO_SUB : 0));
89 memset (&rpbuf, 0, sizeof (rpbuf));
90 s = re_compile_pattern (pat[i], strlen (pat[i]), &rpbuf);
91 if (s != NULL)
93 printf ("\n%s\n", s);
94 abort ();
97 /* Just so that this can be tested with earlier glibc as well. */
98 if (testno == 3)
99 rpbuf.no_sub = 1;
102 if (testno < 2)
104 regmatch_t pmatch[71];
105 err = regexec (&rbuf, string, 71, pmatch, 0);
106 if (err == REG_NOMATCH)
108 puts ("\nregexec failed");
109 abort ();
112 if (testno == 0)
114 if (pmatch[0].rm_eo != pmatch[0].rm_so + 11
115 || pmatch[0].rm_eo > len
116 || string + pmatch[0].rm_so >= strchr (string, 'R')
117 || strncmp (string + pmatch[0].rm_so,
118 "n (int argc",
119 sizeof "n (int argc" - 1)
120 != 0)
122 puts ("\nregexec without REG_NOSUB did not find the correct match");
123 abort ();
126 if (i > 0)
127 for (j = 0, l = 1; j < 7; ++j)
128 for (k = 0; k < (i == 1 ? 1 : 10); ++k, ++l)
129 if (pmatch[l].rm_so != pmatch[0].rm_so + j
130 || pmatch[l].rm_eo != pmatch[l].rm_so + 1)
132 printf ("\npmatch[%d] incorrect\n", l);
133 abort ();
137 else
139 struct re_registers regs;
140 int match;
142 memset (&regs, 0, sizeof (regs));
143 match = re_search (&rpbuf, string, len, 0, len,
144 &regs);
145 if (match < 0)
147 puts ("\nre_search failed");
148 abort ();
151 if (match + 11 > len
152 || string + match >= strchr (string, 'R')
153 || strncmp (string + match,
154 "n (int argc",
155 sizeof "n (int argc" - 1)
156 != 0)
158 puts ("\nre_search did not find the correct match");
159 abort ();
162 if (testno == 2)
164 if (regs.num_regs != 2 + (i == 0 ? 0 : i == 1 ? 7 : 70))
166 printf ("\nincorrect num_regs %d\n", regs.num_regs);
167 abort ();
170 if (regs.start[0] != match || regs.end[0] != match + 11)
172 printf ("\nincorrect regs.{start,end}[0] = { %d, %d}\n",
173 regs.start[0], regs.end[0]);
174 abort ();
177 if (regs.start[regs.num_regs - 1] != -1
178 || regs.end[regs.num_regs - 1] != -1)
180 puts ("\nincorrect regs.{start,end}[num_regs - 1]");
181 abort ();
184 if (i > 0)
185 for (j = 0, l = 1; j < 7; ++j)
186 for (k = 0; k < (i == 1 ? 1 : 10); ++k, ++l)
187 if (regs.start[l] != match + j
188 || regs.end[l] != regs.start[l] + 1)
190 printf ("\nregs.{start,end}[%d] incorrect\n", l);
191 abort ();
196 putchar ('\n');
198 if (testno < 2)
199 regfree (&rbuf);
200 else
201 regfree (&rpbuf);
204 exit (0);