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
20 #include "spawn_int.h"
35 #include <sys/types.h>
48 static int test_expr (const char *expr
, int expected
);
49 static int run_test (const char *expr
, const char *mem
, size_t memlen
,
67 /* Make the content of the file available in memory. */
68 file
= "../ChangeLog.8";
69 fd
= open (file
, O_RDONLY
);
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
));
77 mem
= (char *) malloc (memlen
+ 1);
79 error (EXIT_FAILURE
, errno
, "while allocating buffer");
81 if (read (fd
, mem
, memlen
) != memlen
)
82 error (EXIT_FAILURE
, 0, "cannot read entire file");
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
96 umem
= (char *) calloc (2, memlen
);
98 error (EXIT_FAILURE
, errno
, "while allocating buffer");
103 outlen
= 2 * memlen
- 1;
104 iconv (cd
, &inmem
, &inlen
, &outmem
, &outlen
);
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;
114 re_set_syntax (RE_DEBUG
);
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. */
135 test_expr (const char *expr
, int expected
)
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
);
162 error (EXIT_FAILURE
, errno
, "cannot convert expression");
165 printf ("\nTest \"%s\" with multi-byte locale\n", expr
);
166 result
|= run_test (uexpr
, umem
, 2 * memlen
- outlen
, expected
);
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
;
184 #ifdef _POSIX_CPUTIME
186 use_clock
= clock_gettime (cl
, &start
) == 0;
189 err
= regcomp (&re
, expr
, REG_NEWLINE
);
190 if (err
!= REG_NOERROR
)
193 regerror (err
, &re
, buf
, sizeof buf
);
194 error (EXIT_FAILURE
, 0, "cannot compile expression: %s", buf
);
199 assert (mem
[memlen
] == '\0');
200 while (offset
< memlen
)
206 err
= regexec (&re
, mem
+ offset
, 1, ma
, 0);
207 if (err
== REG_NOMATCH
)
210 if (err
!= REG_NOERROR
)
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')
222 ep
= mem
+ offset
+ ma
[0].rm_so
;
223 while (*ep
!= '\0' && *ep
!= '\n')
226 printf ("match %d: \"%.*s\"\n", ++cnt
, (int) (ep
- sp
), sp
);
228 offset
= ep
+ 1 - mem
;
233 #ifdef _POSIX_CPUTIME
236 use_clock
= clock_gettime (cl
, &finish
) == 0;
239 if (finish
.tv_nsec
< start
.tv_nsec
)
241 finish
.tv_nsec
-= start
.tv_nsec
- 1000000000;
242 finish
.tv_sec
-= 1 + start
.tv_sec
;
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
);
256 /* Return an error if the number of matches found is not match we
258 return cnt
!= expected
;