summary refs log tree commit diff
path: root/posix/tst-regex.c
blob: a6684a7744e8843424813e5d4c91e854596dd4d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* Copyright (C) 2001-2023 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <assert.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <getopt.h>
#include <iconv.h>
#include <locale.h>
#include <mcheck.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <regex.h>
#include <support/support.h>


#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
static clockid_t cl;
static int use_clock;
#endif
static iconv_t cd;
static char *mem;
static char *umem;
static size_t memlen;
static size_t umemlen;
static int timing;

static int test_expr (const char *expr, int expected, int expectedicase);
static int run_test (const char *expr, const char *mem, size_t memlen,
		     int icase, int expected);
static int run_test_backwards (const char *expr, const char *mem,
			       size_t memlen, int icase, int expected);


static int
do_test (void)
{
  const char *file;
  int fd;
  struct stat st;
  int result = 0;
  char *inmem;
  char *outmem;
  size_t inlen;
  size_t outlen;

  mtrace ();

  /* Make the content of the file available in memory.  */
  file = "./tst-regex.input";
  fd = open (file, O_RDONLY);
  if (fd == -1)
    error (EXIT_FAILURE, errno, "cannot open %s", basename (file));

  if (fstat (fd, &st) != 0)
    error (EXIT_FAILURE, errno, "cannot stat %s", basename (file));
  memlen = st.st_size;

  mem = (char *) malloc (memlen + 1);
  if (mem == NULL)
    error (EXIT_FAILURE, errno, "while allocating buffer");

  if ((size_t) read (fd, mem, memlen) != memlen)
    error (EXIT_FAILURE, 0, "cannot read entire file");
  mem[memlen] = '\0';

  close (fd);

  /* We have to convert a few things from UTF-8 to Latin-1.  */
  cd = iconv_open ("ISO-8859-1", "UTF-8");
  if (cd == (iconv_t) -1)
    error (EXIT_FAILURE, errno, "cannot get conversion descriptor");

  /* For the second test we have to convert the file content to Latin-1.
     This cannot grow the data.  */
  umem = (char *) malloc (memlen + 1);
  if (umem == NULL)
    error (EXIT_FAILURE, errno, "while allocating buffer");

  inmem = mem;
  inlen = memlen;
  outmem = umem;
  outlen = memlen;
  iconv (cd, &inmem, &inlen, &outmem, &outlen);
  umemlen = outmem - umem;
  if (inlen != 0)
    error (EXIT_FAILURE, errno, "cannot convert buffer");
  umem[umemlen] = '\0';

#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
# if _POSIX_CPUTIME == 0
  if (sysconf (_SC_CPUTIME) < 0)
    use_clock = 0;
  else
# endif
    /* See whether we can use the CPU clock.  */
    use_clock = clock_getcpuclockid (0, &cl) == 0;
#endif

#ifdef DEBUG
  re_set_syntax (RE_DEBUG);
#endif

  /* Run the actual tests.  All tests are run in a single-byte and a
     multi-byte locale.  */
  result |= test_expr ("[äáàâéèêíìîñöóòôüúùû]", 4, 4);
  result |= test_expr ("G.ran", 2, 3);
  result |= test_expr ("G.\\{1\\}ran", 2, 3);
  result |= test_expr ("G.*ran", 3, 43);
  result |= test_expr ("[äáàâ]", 0, 0);
  result |= test_expr ("Uddeborg", 2, 2);
  result |= test_expr (".Uddeborg", 2, 2);

  /* Free the resources.  */
  free (umem);
  iconv_close (cd);
  free (mem);

  return result;
}


static int
test_expr (const char *expr, int expected, int expectedicase)
{
  int result = 0;
  char *inmem;
  char *outmem;
  size_t inlen;
  size_t outlen;
  char *uexpr;

  /* First test: search with basic C.UTF-8 locale.  */
  printf ("INFO: Testing C.UTF-8.\n");
  xsetlocale (LC_ALL, "C.UTF-8");

  printf ("\nTest \"%s\" with multi-byte locale\n", expr);
  result |= run_test (expr, mem, memlen, 0, expected);
  printf ("\nTest \"%s\" with multi-byte locale, case insensitive\n", expr);
  result |= run_test (expr, mem, memlen, 1, expectedicase);
  printf ("\nTest \"%s\" backwards with multi-byte locale\n", expr);
  result |= run_test_backwards (expr, mem, memlen, 0, expected);
  printf ("\nTest \"%s\" backwards with multi-byte locale, case insensitive\n",
	  expr);
  result |= run_test_backwards (expr, mem, memlen, 1, expectedicase);

  /* Second test: search with an UTF-8 locale.  */
  printf ("INFO: Testing de_DE.UTF-8.\n");
  xsetlocale (LC_ALL, "de_DE.UTF-8");

  printf ("\nTest \"%s\" with multi-byte locale\n", expr);
  result |= run_test (expr, mem, memlen, 0, expected);
  printf ("\nTest \"%s\" with multi-byte locale, case insensitive\n", expr);
  result |= run_test (expr, mem, memlen, 1, expectedicase);
  printf ("\nTest \"%s\" backwards with multi-byte locale\n", expr);
  result |= run_test_backwards (expr, mem, memlen, 0, expected);
  printf ("\nTest \"%s\" backwards with multi-byte locale, case insensitive\n",
	  expr);
  result |= run_test_backwards (expr, mem, memlen, 1, expectedicase);

  /* Second test: search with an ISO-8859-1 locale.  */
  printf ("INFO: Testing de_DE.ISO-8859-1.\n");
  xsetlocale (LC_ALL, "de_DE.ISO-8859-1");

  inmem = (char *) expr;
  inlen = strlen (expr);
  outlen = inlen;
  outmem = uexpr = alloca (outlen + 1);
  memset (outmem, '\0', outlen + 1);
  iconv (cd, &inmem, &inlen, &outmem, &outlen);
  if (inlen != 0)
    error (EXIT_FAILURE, errno, "cannot convert expression");

  /* Run the tests.  */
  printf ("\nTest \"%s\" with 8-bit locale\n", expr);
  result |= run_test (uexpr, umem, umemlen, 0, expected);
  printf ("\nTest \"%s\" with 8-bit locale, case insensitive\n", expr);
  result |= run_test (uexpr, umem, umemlen, 1, expectedicase);
  printf ("\nTest \"%s\" backwards with 8-bit locale\n", expr);
  result |= run_test_backwards (uexpr, umem, umemlen, 0, expected);
  printf ("\nTest \"%s\" backwards with 8-bit locale, case insensitive\n",
	  expr);
  result |= run_test_backwards (uexpr, umem, umemlen, 1, expectedicase);

  return result;
}


static int
run_test (const char *expr, const char *mem, size_t memlen, int icase,
	  int expected)
{
#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
  struct timespec start;
  struct timespec finish;
#endif
  regex_t re;
  int err;
  size_t offset;
  int cnt;

#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
  if (use_clock && !timing)
    use_clock = clock_gettime (cl, &start) == 0;
#endif

  err = regcomp (&re, expr, REG_NEWLINE | (icase ? REG_ICASE : 0));
  if (err != REG_NOERROR)
    {
      char buf[200];
      regerror (err, &re, buf, sizeof buf);
      error (EXIT_FAILURE, 0, "cannot compile expression: %s", buf);
    }

  cnt = 0;
  offset = 0;
  assert (mem[memlen] == '\0');
  while (offset < memlen)
    {
      regmatch_t ma[1];
      const char *sp;
      const char *ep;

      err = regexec (&re, mem + offset, 1, ma, 0);
      if (err == REG_NOMATCH)
	break;

      if (err != REG_NOERROR)
	{
	  char buf[200];
	  regerror (err, &re, buf, sizeof buf);
	  error (EXIT_FAILURE, 0, "cannot use expression: %s", buf);
	}

      assert (ma[0].rm_so >= 0);
      sp = mem + offset + ma[0].rm_so;
      while (sp > mem && sp[-1] != '\n')
	--sp;

      ep = mem + offset + ma[0].rm_so;
      while (*ep != '\0' && *ep != '\n')
	++ep;

      printf ("match %d: \"%.*s\"\n", ++cnt, (int) (ep - sp), sp);

      offset = ep + 1 - mem;
    }

  regfree (&re);

#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
  if (use_clock && !timing)
    {
      use_clock = clock_gettime (cl, &finish) == 0;
      if (use_clock)
	{
	  if (finish.tv_nsec < start.tv_nsec)
	    {
	      finish.tv_nsec -= start.tv_nsec - 1000000000;
	      finish.tv_sec -= 1 + start.tv_sec;
	    }
	  else
	    {
	      finish.tv_nsec -= start.tv_nsec;
	      finish.tv_sec -= start.tv_sec;
	    }

	  printf ("elapsed time: %jd.%09jd sec\n",
		  (intmax_t) finish.tv_sec, (intmax_t) finish.tv_nsec);
	}
    }

  if (use_clock && timing)
    {
      struct timespec mintime = { .tv_sec = 24 * 60 * 60 };

      for (int i = 0; i < 10; ++i)
	{
	  offset = 0;
	  use_clock = clock_gettime (cl, &start) == 0;

	  if (!use_clock)
	    continue;

	  err = regcomp (&re, expr, REG_NEWLINE | (icase ? REG_ICASE : 0));
	  if (err != REG_NOERROR)
	    continue;

	  while (offset < memlen)
	    {
	      regmatch_t ma[1];

	      err = regexec (&re, mem + offset, 1, ma, 0);
	      if (err != REG_NOERROR)
		break;

	      offset += ma[0].rm_eo;
	    }

	  regfree (&re);

	  use_clock = clock_gettime (cl, &finish) == 0;
	  if (use_clock)
	    {
	      if (finish.tv_nsec < start.tv_nsec)
		{
		  finish.tv_nsec -= start.tv_nsec - 1000000000;
		  finish.tv_sec -= 1 + start.tv_sec;
		}
	      else
		{
		  finish.tv_nsec -= start.tv_nsec;
		  finish.tv_sec -= start.tv_sec;
		}
	      if (finish.tv_sec < mintime.tv_sec
		  || (finish.tv_sec == mintime.tv_sec
		      && finish.tv_nsec < mintime.tv_nsec))
		mintime = finish;
	    }
	}
      printf ("elapsed time: %jd.%09jd sec\n",
	      (intmax_t) mintime.tv_sec, (intmax_t) mintime.tv_nsec);
    }
#endif

  /* Return an error if the number of matches found is not match we
     expect.  */
  return cnt != expected;
}


static int
run_test_backwards (const char *expr, const char *mem, size_t memlen,
		    int icase, int expected)
{
#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
  struct timespec start;
  struct timespec finish;
#endif
  struct re_pattern_buffer re;
  const char *err;
  size_t offset;
  int cnt;

#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
  if (use_clock && !timing)
    use_clock = clock_gettime (cl, &start) == 0;
#endif

  re_set_syntax ((RE_SYNTAX_POSIX_BASIC & ~RE_DOT_NEWLINE)
		 | RE_HAT_LISTS_NOT_NEWLINE
		 | (icase ? RE_ICASE : 0));

  memset (&re, 0, sizeof (re));
  re.fastmap = malloc (256);
  if (re.fastmap == NULL)
    error (EXIT_FAILURE, errno, "cannot allocate fastmap");

  err = re_compile_pattern (expr, strlen (expr), &re);
  if (err != NULL)
    error (EXIT_FAILURE, 0, "cannot compile expression: %s", err);

  if (re_compile_fastmap (&re))
    error (EXIT_FAILURE, 0, "couldn't compile fastmap");

  cnt = 0;
  offset = memlen;
  assert (mem[memlen] == '\0');
  while (offset <= memlen)
    {
      int start;
      const char *sp;
      const char *ep;

      start = re_search (&re, mem, memlen, offset, -offset, NULL);
      if (start == -1)
	break;

      if (start == -2)
	error (EXIT_FAILURE, 0, "internal error in re_search");

      sp = mem + start;
      while (sp > mem && sp[-1] != '\n')
	--sp;

      ep = mem + start;
      while (*ep != '\0' && *ep != '\n')
	++ep;

      printf ("match %d: \"%.*s\"\n", ++cnt, (int) (ep - sp), sp);

      offset = sp - 1 - mem;
    }

  regfree (&re);

#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
  if (use_clock && !timing)
    {
      use_clock = clock_gettime (cl, &finish) == 0;
      if (use_clock)
	{
	  if (finish.tv_nsec < start.tv_nsec)
	    {
	      finish.tv_nsec -= start.tv_nsec - 1000000000;
	      finish.tv_sec -= 1 + start.tv_sec;
	    }
	  else
	    {
	      finish.tv_nsec -= start.tv_nsec;
	      finish.tv_sec -= start.tv_sec;
	    }

	  printf ("elapsed time: %jd.%09jd sec\n",
		  (intmax_t) finish.tv_sec, (intmax_t) finish.tv_nsec);
	}
    }

  if (use_clock && timing)
    {
      struct timespec mintime = { .tv_sec = 24 * 60 * 60 };

      for (int i = 0; i < 10; ++i)
	{
	  offset = memlen;
	  use_clock = clock_gettime (cl, &start) == 0;

	  if (!use_clock)
	    continue;

	  memset (&re, 0, sizeof (re));
	  re.fastmap = malloc (256);
	  if (re.fastmap == NULL)
	    continue;

	  err = re_compile_pattern (expr, strlen (expr), &re);
	  if (err != NULL)
	    continue;

	  if (re_compile_fastmap (&re))
	    {
	      regfree (&re);
	      continue;
	    }

	  while (offset <= memlen)
	    {
	      int start;
	      const char *sp;

	      start = re_search (&re, mem, memlen, offset, -offset, NULL);
	      if (start < -1)
		break;

	      sp = mem + start;
	      while (sp > mem && sp[-1] != '\n')
		--sp;

	      offset = sp - 1 - mem;
	    }

	  regfree (&re);

	  use_clock = clock_gettime (cl, &finish) == 0;
	  if (use_clock)
	    {
	      if (finish.tv_nsec < start.tv_nsec)
		{
		  finish.tv_nsec -= start.tv_nsec - 1000000000;
		  finish.tv_sec -= 1 + start.tv_sec;
		}
	      else
		{
		  finish.tv_nsec -= start.tv_nsec;
		  finish.tv_sec -= start.tv_sec;
		}
	      if (finish.tv_sec < mintime.tv_sec
		  || (finish.tv_sec == mintime.tv_sec
		      && finish.tv_nsec < mintime.tv_nsec))
		mintime = finish;
	    }
	}
      printf ("elapsed time: %jd.%09jd sec\n",
	      (intmax_t) mintime.tv_sec, (intmax_t) mintime.tv_nsec);
    }
#endif

  /* Return an error if the number of matches found is not match we
     expect.  */
  return cnt != expected;
}

/* If --timing is used we will need a larger timeout.  */
#define TIMEOUT 50
#define CMDLINE_OPTIONS \
   {"timing", no_argument, &timing, 1 },
#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"