* sysdeps/gnu/Makefile ($(objpfx)errlist-compat.c):
[glibc.git] / posix / tst-regex2.c
bloba107f73ba47ad9c2ac0dda512a81d0f88b366c7c
1 #include <fcntl.h>
2 #include <locale.h>
3 #include <regex.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/stat.h>
8 #include <time.h>
9 #include <unistd.h>
11 #ifdef _POSIX_CPUTIME
12 static clockid_t cl;
13 static int use_clock;
14 #endif
16 static int
17 do_test (void)
19 #ifdef _POSIX_CPUTIME
20 /* See whether we can use the CPU clock. */
21 use_clock = clock_getcpuclockid (0, &cl) == 0;
22 #endif
24 static const char *pat[] = {
25 ".?.?.?.?.?.?.?Log\\.13",
26 "(.?)(.?)(.?)(.?)(.?)(.?)(.?)Log\\.13",
27 "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))"
28 "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))"
29 "((((((((((.?))))))))))Log\\.13" };
31 int fd = open ("../ChangeLog.14", O_RDONLY);
32 if (fd < 0)
34 printf ("Couldn't open ChangeLog.14: %m\n");
35 return 1;
38 struct stat64 st;
39 if (fstat64 (fd, &st) < 0)
41 printf ("Couldn't fstat ChangeLog.14: %m\n");
42 return 1;
45 char *buf = malloc (st.st_size + 1);
46 if (buf == NULL)
48 printf ("Couldn't allocate buffer: %m\n");
49 return 1;
52 if (read (fd, buf, st.st_size) != (ssize_t) st.st_size)
54 puts ("Couldn't read ChangeLog.14");
55 return 1;
58 close (fd);
59 buf[st.st_size] = '\0';
61 setlocale (LC_ALL, "de_DE.UTF-8");
63 char *string = buf;
64 size_t len = st.st_size;
66 #ifndef WHOLE_FILE_TIMING
67 /* Don't search the whole file normally, it takes too long. */
68 if (len > 500000 + 64)
70 string += 500000;
71 len -= 500000;
73 #endif
75 for (int testno = 0; testno < 4; ++testno)
76 for (int i = 0; i < sizeof (pat) / sizeof (pat[0]); ++i)
78 printf ("test %d pattern %d", testno, i);
80 regex_t rbuf;
81 struct re_pattern_buffer rpbuf;
82 int err;
83 if (testno < 2)
85 err = regcomp (&rbuf, pat[i],
86 REG_EXTENDED | (testno ? REG_NOSUB : 0));
87 if (err != 0)
89 putchar ('\n');
90 char errstr[300];
91 regerror (err, &rbuf, errstr, sizeof (errstr));
92 puts (errstr);
93 return err;
96 else
98 re_set_syntax (RE_SYNTAX_POSIX_EGREP
99 | (testno == 3 ? RE_NO_SUB : 0));
101 memset (&rpbuf, 0, sizeof (rpbuf));
102 const char *s = re_compile_pattern (pat[i], strlen (pat[i]),
103 &rpbuf);
104 if (s != NULL)
106 printf ("\n%s\n", s);
107 return 1;
110 /* Just so that this can be tested with earlier glibc as well. */
111 if (testno == 3)
112 rpbuf.no_sub = 1;
115 #ifdef _POSIX_CPUTIME
116 struct timespec start, stop;
117 if (use_clock)
118 use_clock = clock_gettime (cl, &start) == 0;
119 #endif
121 if (testno < 2)
123 regmatch_t pmatch[71];
124 err = regexec (&rbuf, string, 71, pmatch, 0);
125 if (err == REG_NOMATCH)
127 puts ("\nregexec failed");
128 return 1;
131 if (testno == 0)
133 if (pmatch[0].rm_eo != pmatch[0].rm_so + 13
134 || pmatch[0].rm_eo > len
135 || pmatch[0].rm_so < len - 100
136 || strncmp (string + pmatch[0].rm_so,
137 " ChangeLog.13 for earlier changes",
138 sizeof " ChangeLog.13 for earlier changes" - 1)
139 != 0)
141 puts ("\nregexec without REG_NOSUB did not find the correct match");
142 return 1;
145 if (i > 0)
146 for (int j = 0, l = 1; j < 7; ++j)
147 for (int k = 0; k < (i == 1 ? 1 : 10); ++k, ++l)
148 if (pmatch[l].rm_so != pmatch[0].rm_so + j
149 || pmatch[l].rm_eo != pmatch[l].rm_so + 1)
151 printf ("\npmatch[%d] incorrect\n", l);
152 return 1;
156 else
158 struct re_registers regs;
160 memset (&regs, 0, sizeof (regs));
161 int match = re_search (&rpbuf, string, len, 0, len,
162 &regs);
163 if (match < 0)
165 puts ("\nre_search failed");
166 return 1;
169 if (match + 13 > len
170 || match < len - 100
171 || strncmp (string + match,
172 " ChangeLog.13 for earlier changes",
173 sizeof " ChangeLog.13 for earlier changes" - 1)
174 != 0)
176 puts ("\nre_search did not find the correct match");
177 return 1;
180 if (testno == 2)
182 if (regs.num_regs != 2 + (i == 0 ? 0 : i == 1 ? 7 : 70))
184 printf ("\nincorrect num_regs %d\n", regs.num_regs);
185 return 1;
188 if (regs.start[0] != match || regs.end[0] != match + 13)
190 printf ("\nincorrect regs.{start,end}[0] = { %d, %d}\n",
191 regs.start[0], regs.end[0]);
192 return 1;
195 if (regs.start[regs.num_regs - 1] != -1
196 || regs.end[regs.num_regs - 1] != -1)
198 puts ("\nincorrect regs.{start,end}[num_regs - 1]");
199 return 1;
202 if (i > 0)
203 for (int j = 0, l = 1; j < 7; ++j)
204 for (int k = 0; k < (i == 1 ? 1 : 10); ++k, ++l)
205 if (regs.start[l] != match + j
206 || regs.end[l] != regs.start[l] + 1)
208 printf ("\nregs.{start,end}[%d] incorrect\n", l);
209 return 1;
214 #ifdef _POSIX_CPUTIME
215 if (use_clock)
216 use_clock = clock_gettime (cl, &stop) == 0;
217 if (use_clock)
219 stop.tv_sec -= start.tv_sec;
220 if (stop.tv_nsec < start.tv_nsec)
222 stop.tv_sec--;
223 stop.tv_nsec += 1000000000 - start.tv_nsec;
225 else
226 stop.tv_nsec -= start.tv_nsec;
227 printf (": %ld.%09lds\n", (long) stop.tv_sec, (long) stop.tv_nsec);
229 else
230 #endif
231 putchar ('\n');
233 if (testno < 2)
234 regfree (&rbuf);
235 else
236 regfree (&rpbuf);
239 return 0;
242 #define TIMEOUT 20
243 #define TEST_FUNCTION do_test ()
244 #include "../test-skeleton.c"