(Hash Search Function): Describe that memory must be cleared before calling hcreate_r.
[glibc.git] / posix / tst-regex.c
blob3f42181dd747b0723211561f7f8849e136ce237e
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <spawn.h>
20 #include "spawn_int.h"
22 #include <assert.h>
23 #include <errno.h>
24 #include <error.h>
25 #include <fcntl.h>
26 #include <iconv.h>
27 #include <locale.h>
28 #include <mcheck.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <regex.h>
39 #ifdef _POSIX_CPUTIME
40 static clockid_t cl;
41 static int use_clock;
42 #endif
43 static iconv_t cd;
44 static char *mem;
45 static char *umem;
46 static size_t memlen;
48 static int test_expr (const char *expr, int expected);
49 static int run_test (const char *expr, const char *mem, size_t memlen,
50 int expected);
53 int
54 main (void)
56 const char *file;
57 int fd;
58 struct stat st;
59 int result;
60 char *inmem;
61 char *outmem;
62 size_t inlen;
63 size_t outlen;
65 mtrace ();
67 /* Make the content of the file available in memory. */
68 file = "../ChangeLog.8";
69 fd = open (file, O_RDONLY);
70 if (fd == -1)
71 error (EXIT_FAILURE, errno, "cannot open %s", basename (file));
73 if (fstat (fd, &st) != 0)
74 error (EXIT_FAILURE, errno, "cannot stat %s", basename (file));
75 memlen = st.st_size;
77 mem = (char *) malloc (memlen + 1);
78 if (mem == NULL)
79 error (EXIT_FAILURE, errno, "while allocating buffer");
81 if (read (fd, mem, memlen) != memlen)
82 error (EXIT_FAILURE, 0, "cannot read entire file");
83 mem[memlen] = '\0';
85 close (fd);
87 /* We have to convert a few things from Latin-1 to UTF-8. */
88 cd = iconv_open ("UTF-8", "ISO-8859-1");
89 if (cd == (iconv_t) -1)
90 error (EXIT_FAILURE, errno, "cannot get conversion descriptor");
92 /* For the second test we have to convert the file content to UTF-8.
93 Since the text is mostly ASCII it should be enough to allocate
94 twice as much memory for the UTF-8 text than for the Latin-1
95 text. */
96 umem = (char *) calloc (2, memlen);
97 if (umem == NULL)
98 error (EXIT_FAILURE, errno, "while allocating buffer");
100 inmem = mem;
101 inlen = memlen;
102 outmem = umem;
103 outlen = 2 * memlen - 1;
104 iconv (cd, &inmem, &inlen, &outmem, &outlen);
105 if (inlen != 0)
106 error (EXIT_FAILURE, errno, "cannot convert buffer");
108 #ifdef _POSIX_CPUTIME
109 /* See whether we can use the CPU clock. */
110 use_clock = clock_getcpuclockid (0, &cl) == 0;
111 #endif
113 #ifdef DEBUG
114 re_set_syntax (RE_DEBUG);
115 #endif
117 /* Run the actual tests. All tests are run in a single-byte and a
118 multi-byte locale. */
119 result = test_expr ("[äáàâéèêíìîñöóòôüúùû]", 2);
120 result |= test_expr ("G.ran", 2);
121 result |= test_expr ("G.\\{1\\}ran", 2);
122 result |= test_expr ("G.*ran", 3);
123 result |= test_expr ("[äáàâ]", 0);
125 /* Free the resources. */
126 free (umem);
127 iconv_close (cd);
128 free (mem);
130 return result;
134 static int
135 test_expr (const char *expr, int expected)
137 int result;
138 char *inmem;
139 char *outmem;
140 size_t inlen;
141 size_t outlen;
142 char *uexpr;
144 /* First test: search with an ISO-8859-1 locale. */
145 if (setlocale (LC_ALL, "de_DE.ISO-8859-1") == NULL)
146 error (EXIT_FAILURE, 0, "cannot set locale de_DE.ISO-8859-1");
148 printf ("\nTest \"%s\" with 8-bit locale\n", expr);
149 result = run_test (expr, mem, memlen, expected);
151 /* Second test: search with an UTF-8 locale. */
152 if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
153 error (EXIT_FAILURE, 0, "cannot set locale de_DE.UTF-8");
155 inmem = (char *) expr;
156 inlen = strlen (expr);
157 outlen = inlen * MB_CUR_MAX;
158 outmem = uexpr = alloca (outlen + 1);
159 memset (outmem, '\0', outlen + 1);
160 iconv (cd, &inmem, &inlen, &outmem, &outlen);
161 if (inlen != 0)
162 error (EXIT_FAILURE, errno, "cannot convert expression");
164 /* Run the tests. */
165 printf ("\nTest \"%s\" with multi-byte locale\n", expr);
166 result |= run_test (uexpr, umem, 2 * memlen - outlen, expected);
168 return result;
172 static int
173 run_test (const char *expr, const char *mem, size_t memlen, int expected)
175 #ifdef _POSIX_CPUTIME
176 struct timespec start;
177 struct timespec finish;
178 #endif
179 regex_t re;
180 int err;
181 size_t offset;
182 int cnt;
184 #ifdef _POSIX_CPUTIME
185 if (use_clock)
186 use_clock = clock_gettime (cl, &start) == 0;
187 #endif
189 err = regcomp (&re, expr, REG_NEWLINE);
190 if (err != REG_NOERROR)
192 char buf[200];
193 regerror (err, &re, buf, sizeof buf);
194 error (EXIT_FAILURE, 0, "cannot compile expression: %s", buf);
197 cnt = 0;
198 offset = 0;
199 assert (mem[memlen] == '\0');
200 while (offset < memlen)
202 regmatch_t ma[1];
203 const char *sp;
204 const char *ep;
206 err = regexec (&re, mem + offset, 1, ma, 0);
207 if (err == REG_NOMATCH)
208 break;
210 if (err != REG_NOERROR)
212 char buf[200];
213 regerror (err, &re, buf, sizeof buf);
214 error (EXIT_FAILURE, 0, "cannot use expression: %s", buf);
217 assert (ma[0].rm_so >= 0);
218 sp = mem + offset + ma[0].rm_so;
219 while (sp > mem && sp[-1] != '\n')
220 --sp;
222 ep = mem + offset + ma[0].rm_so;
223 while (*ep != '\0' && *ep != '\n')
224 ++ep;
226 printf ("match %d: \"%.*s\"\n", ++cnt, (int) (ep - sp), sp);
228 offset = ep + 1 - mem;
231 regfree (&re);
233 #ifdef _POSIX_CPUTIME
234 if (use_clock)
236 use_clock = clock_gettime (cl, &finish) == 0;
237 if (use_clock)
239 if (finish.tv_nsec < start.tv_nsec)
241 finish.tv_nsec -= start.tv_nsec - 1000000000;
242 finish.tv_sec -= 1 + start.tv_sec;
244 else
246 finish.tv_nsec -= start.tv_nsec;
247 finish.tv_sec -= start.tv_sec;
250 printf ("elapsed time: %ld.%09ld sec\n",
251 finish.tv_sec, finish.tv_nsec);
254 #endif
256 /* Return an error if the number of matches found is not match we
257 expect. */
258 return cnt != expected;