Back to index

glibc  2.9
tst-regex.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001, 2003, 2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <spawn.h>
00020 #include "spawn_int.h"
00021 
00022 #include <assert.h>
00023 #include <errno.h>
00024 #include <error.h>
00025 #include <fcntl.h>
00026 #include <getopt.h>
00027 #include <iconv.h>
00028 #include <locale.h>
00029 #include <mcheck.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <time.h>
00034 #include <unistd.h>
00035 #include <sys/stat.h>
00036 #include <sys/types.h>
00037 #include <regex.h>
00038 
00039 
00040 #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
00041 static clockid_t cl;
00042 static int use_clock;
00043 #endif
00044 static iconv_t cd;
00045 static char *mem;
00046 static char *umem;
00047 static size_t memlen;
00048 static size_t umemlen;
00049 static int timing;
00050 
00051 static int test_expr (const char *expr, int expected, int expectedicase);
00052 static int run_test (const char *expr, const char *mem, size_t memlen,
00053                    int icase, int expected);
00054 static int run_test_backwards (const char *expr, const char *mem,
00055                             size_t memlen, int icase, int expected);
00056 
00057 
00058 static int
00059 do_test (void)
00060 {
00061   const char *file;
00062   int fd;
00063   struct stat st;
00064   int result;
00065   char *inmem;
00066   char *outmem;
00067   size_t inlen;
00068   size_t outlen;
00069 
00070   mtrace ();
00071 
00072   /* Make the content of the file available in memory.  */
00073   file = "../ChangeLog.8";
00074   fd = open (file, O_RDONLY);
00075   if (fd == -1)
00076     error (EXIT_FAILURE, errno, "cannot open %s", basename (file));
00077 
00078   if (fstat (fd, &st) != 0)
00079     error (EXIT_FAILURE, errno, "cannot stat %s", basename (file));
00080   memlen = st.st_size;
00081 
00082   mem = (char *) malloc (memlen + 1);
00083   if (mem == NULL)
00084     error (EXIT_FAILURE, errno, "while allocating buffer");
00085 
00086   if ((size_t) read (fd, mem, memlen) != memlen)
00087     error (EXIT_FAILURE, 0, "cannot read entire file");
00088   mem[memlen] = '\0';
00089 
00090   close (fd);
00091 
00092   /* We have to convert a few things from Latin-1 to UTF-8.  */
00093   cd = iconv_open ("UTF-8", "ISO-8859-1");
00094   if (cd == (iconv_t) -1)
00095     error (EXIT_FAILURE, errno, "cannot get conversion descriptor");
00096 
00097   /* For the second test we have to convert the file content to UTF-8.
00098      Since the text is mostly ASCII it should be enough to allocate
00099      twice as much memory for the UTF-8 text than for the Latin-1
00100      text.  */
00101   umem = (char *) calloc (2, memlen);
00102   if (umem == NULL)
00103     error (EXIT_FAILURE, errno, "while allocating buffer");
00104 
00105   inmem = mem;
00106   inlen = memlen;
00107   outmem = umem;
00108   outlen = 2 * memlen - 1;
00109   iconv (cd, &inmem, &inlen, &outmem, &outlen);
00110   umemlen = outmem - umem;
00111   if (inlen != 0)
00112     error (EXIT_FAILURE, errno, "cannot convert buffer");
00113 
00114 #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
00115 # if _POSIX_CPUTIME == 0
00116   if (sysconf (_SC_CPUTIME) < 0)
00117     use_clock = 0;
00118   else
00119 # endif
00120     /* See whether we can use the CPU clock.  */
00121     use_clock = clock_getcpuclockid (0, &cl) == 0;
00122 #endif
00123 
00124 #ifdef DEBUG
00125   re_set_syntax (RE_DEBUG);
00126 #endif
00127 
00128   /* Run the actual tests.  All tests are run in a single-byte and a
00129      multi-byte locale.  */
00130   result = test_expr ("[]", 2, 2);
00131   result |= test_expr ("G.ran", 2, 3);
00132   result |= test_expr ("G.\\{1\\}ran", 2, 3);
00133   result |= test_expr ("G.*ran", 3, 44);
00134   result |= test_expr ("[]", 0, 0);
00135   result |= test_expr ("Uddeborg", 2, 2);
00136   result |= test_expr (".Uddeborg", 2, 2);
00137 
00138   /* Free the resources.  */
00139   free (umem);
00140   iconv_close (cd);
00141   free (mem);
00142 
00143   return result;
00144 }
00145 
00146 
00147 static int
00148 test_expr (const char *expr, int expected, int expectedicase)
00149 {
00150   int result;
00151   char *inmem;
00152   char *outmem;
00153   size_t inlen;
00154   size_t outlen;
00155   char *uexpr;
00156 
00157   /* First test: search with an ISO-8859-1 locale.  */
00158   if (setlocale (LC_ALL, "de_DE.ISO-8859-1") == NULL)
00159     error (EXIT_FAILURE, 0, "cannot set locale de_DE.ISO-8859-1");
00160 
00161   printf ("\nTest \"%s\" with 8-bit locale\n", expr);
00162   result = run_test (expr, mem, memlen, 0, expected);
00163   printf ("\nTest \"%s\" with 8-bit locale, case insensitive\n", expr);
00164   result |= run_test (expr, mem, memlen, 1, expectedicase);
00165   printf ("\nTest \"%s\" backwards with 8-bit locale\n", expr);
00166   result |= run_test_backwards (expr, mem, memlen, 0, expected);
00167   printf ("\nTest \"%s\" backwards with 8-bit locale, case insensitive\n",
00168          expr);
00169   result |= run_test_backwards (expr, mem, memlen, 1, expectedicase);
00170 
00171   /* Second test: search with an UTF-8 locale.  */
00172   if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
00173     error (EXIT_FAILURE, 0, "cannot set locale de_DE.UTF-8");
00174 
00175   inmem = (char *) expr;
00176   inlen = strlen (expr);
00177   outlen = inlen * MB_CUR_MAX;
00178   outmem = uexpr = alloca (outlen + 1);
00179   memset (outmem, '\0', outlen + 1);
00180   iconv (cd, &inmem, &inlen, &outmem, &outlen);
00181   if (inlen != 0)
00182     error (EXIT_FAILURE, errno, "cannot convert expression");
00183 
00184   /* Run the tests.  */
00185   printf ("\nTest \"%s\" with multi-byte locale\n", expr);
00186   result |= run_test (uexpr, umem, umemlen, 0, expected);
00187   printf ("\nTest \"%s\" with multi-byte locale, case insensitive\n", expr);
00188   result |= run_test (uexpr, umem, umemlen, 1, expectedicase);
00189   printf ("\nTest \"%s\" backwards with multi-byte locale\n", expr);
00190   result |= run_test_backwards (uexpr, umem, umemlen, 0, expected);
00191   printf ("\nTest \"%s\" backwards with multi-byte locale, case insensitive\n",
00192          expr);
00193   result |= run_test_backwards (uexpr, umem, umemlen, 1, expectedicase);
00194 
00195   return result;
00196 }
00197 
00198 
00199 static int
00200 run_test (const char *expr, const char *mem, size_t memlen, int icase,
00201          int expected)
00202 {
00203 #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
00204   struct timespec start;
00205   struct timespec finish;
00206 #endif
00207   regex_t re;
00208   int err;
00209   size_t offset;
00210   int cnt;
00211 
00212 #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
00213   if (use_clock && !timing)
00214     use_clock = clock_gettime (cl, &start) == 0;
00215 #endif
00216 
00217   err = regcomp (&re, expr, REG_NEWLINE | (icase ? REG_ICASE : 0));
00218   if (err != REG_NOERROR)
00219     {
00220       char buf[200];
00221       regerror (err, &re, buf, sizeof buf);
00222       error (EXIT_FAILURE, 0, "cannot compile expression: %s", buf);
00223     }
00224 
00225   cnt = 0;
00226   offset = 0;
00227   assert (mem[memlen] == '\0');
00228   while (offset < memlen)
00229     {
00230       regmatch_t ma[1];
00231       const char *sp;
00232       const char *ep;
00233 
00234       err = regexec (&re, mem + offset, 1, ma, 0);
00235       if (err == REG_NOMATCH)
00236        break;
00237 
00238       if (err != REG_NOERROR)
00239        {
00240          char buf[200];
00241          regerror (err, &re, buf, sizeof buf);
00242          error (EXIT_FAILURE, 0, "cannot use expression: %s", buf);
00243        }
00244 
00245       assert (ma[0].rm_so >= 0);
00246       sp = mem + offset + ma[0].rm_so;
00247       while (sp > mem && sp[-1] != '\n')
00248        --sp;
00249 
00250       ep = mem + offset + ma[0].rm_so;
00251       while (*ep != '\0' && *ep != '\n')
00252        ++ep;
00253 
00254       printf ("match %d: \"%.*s\"\n", ++cnt, (int) (ep - sp), sp);
00255 
00256       offset = ep + 1 - mem;
00257     }
00258 
00259   regfree (&re);
00260 
00261 #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
00262   if (use_clock && !timing)
00263     {
00264       use_clock = clock_gettime (cl, &finish) == 0;
00265       if (use_clock)
00266        {
00267          if (finish.tv_nsec < start.tv_nsec)
00268            {
00269              finish.tv_nsec -= start.tv_nsec - 1000000000;
00270              finish.tv_sec -= 1 + start.tv_sec;
00271            }
00272          else
00273            {
00274              finish.tv_nsec -= start.tv_nsec;
00275              finish.tv_sec -= start.tv_sec;
00276            }
00277 
00278          printf ("elapsed time: %ld.%09ld sec\n",
00279                 finish.tv_sec, finish.tv_nsec);
00280        }
00281     }
00282 
00283   if (use_clock && timing)
00284     {
00285       struct timespec mintime = { .tv_sec = 24 * 60 * 60 };
00286 
00287       for (int i = 0; i < 10; ++i)
00288        {
00289          offset = 0;
00290          use_clock = clock_gettime (cl, &start) == 0;
00291 
00292          if (!use_clock)
00293            continue;
00294 
00295          err = regcomp (&re, expr, REG_NEWLINE | (icase ? REG_ICASE : 0));
00296          if (err != REG_NOERROR)
00297            continue;
00298 
00299          while (offset < memlen)
00300            {
00301              regmatch_t ma[1];
00302 
00303              err = regexec (&re, mem + offset, 1, ma, 0);
00304              if (err != REG_NOERROR)
00305               break;
00306 
00307              offset += ma[0].rm_eo;
00308            }
00309 
00310          regfree (&re);
00311 
00312          use_clock = clock_gettime (cl, &finish) == 0;
00313          if (use_clock)
00314            {
00315              if (finish.tv_nsec < start.tv_nsec)
00316               {
00317                 finish.tv_nsec -= start.tv_nsec - 1000000000;
00318                 finish.tv_sec -= 1 + start.tv_sec;
00319               }
00320              else
00321               {
00322                 finish.tv_nsec -= start.tv_nsec;
00323                 finish.tv_sec -= start.tv_sec;
00324               }
00325              if (finish.tv_sec < mintime.tv_sec
00326                 || (finish.tv_sec == mintime.tv_sec
00327                     && finish.tv_nsec < mintime.tv_nsec))
00328               mintime = finish;
00329            }
00330        }
00331       printf ("elapsed time: %ld.%09ld sec\n",
00332              mintime.tv_sec, mintime.tv_nsec);
00333     }
00334 #endif
00335 
00336   /* Return an error if the number of matches found is not match we
00337      expect.  */
00338   return cnt != expected;
00339 }
00340 
00341 
00342 static int
00343 run_test_backwards (const char *expr, const char *mem, size_t memlen,
00344                   int icase, int expected)
00345 {
00346 #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
00347   struct timespec start;
00348   struct timespec finish;
00349 #endif
00350   struct re_pattern_buffer re;
00351   const char *err;
00352   size_t offset;
00353   int cnt;
00354 
00355 #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
00356   if (use_clock && !timing)
00357     use_clock = clock_gettime (cl, &start) == 0;
00358 #endif
00359 
00360   re_set_syntax ((RE_SYNTAX_POSIX_BASIC & ~RE_DOT_NEWLINE)
00361                | RE_HAT_LISTS_NOT_NEWLINE
00362                | (icase ? RE_ICASE : 0));
00363 
00364   memset (&re, 0, sizeof (re));
00365   re.fastmap = malloc (256);
00366   if (re.fastmap == NULL)
00367     error (EXIT_FAILURE, errno, "cannot allocate fastmap");
00368 
00369   err = re_compile_pattern (expr, strlen (expr), &re);
00370   if (err != NULL)
00371     error (EXIT_FAILURE, 0, "cannot compile expression: %s", err);
00372 
00373   if (re_compile_fastmap (&re))
00374     error (EXIT_FAILURE, 0, "couldn't compile fastmap");
00375 
00376   cnt = 0;
00377   offset = memlen;
00378   assert (mem[memlen] == '\0');
00379   while (offset <= memlen)
00380     {
00381       int start;
00382       const char *sp;
00383       const char *ep;
00384 
00385       start = re_search (&re, mem, memlen, offset, -offset, NULL);
00386       if (start == -1)
00387        break;
00388 
00389       if (start == -2)
00390        error (EXIT_FAILURE, 0, "internal error in re_search");
00391 
00392       sp = mem + start;
00393       while (sp > mem && sp[-1] != '\n')
00394        --sp;
00395 
00396       ep = mem + start;
00397       while (*ep != '\0' && *ep != '\n')
00398        ++ep;
00399 
00400       printf ("match %d: \"%.*s\"\n", ++cnt, (int) (ep - sp), sp);
00401 
00402       offset = sp - 1 - mem;
00403     }
00404 
00405   regfree (&re);
00406 
00407 #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
00408   if (use_clock && !timing)
00409     {
00410       use_clock = clock_gettime (cl, &finish) == 0;
00411       if (use_clock)
00412        {
00413          if (finish.tv_nsec < start.tv_nsec)
00414            {
00415              finish.tv_nsec -= start.tv_nsec - 1000000000;
00416              finish.tv_sec -= 1 + start.tv_sec;
00417            }
00418          else
00419            {
00420              finish.tv_nsec -= start.tv_nsec;
00421              finish.tv_sec -= start.tv_sec;
00422            }
00423 
00424          printf ("elapsed time: %ld.%09ld sec\n",
00425                 finish.tv_sec, finish.tv_nsec);
00426        }
00427     }
00428 
00429   if (use_clock && timing)
00430     {
00431       struct timespec mintime = { .tv_sec = 24 * 60 * 60 };
00432 
00433       for (int i = 0; i < 10; ++i)
00434        {
00435          offset = memlen;
00436          use_clock = clock_gettime (cl, &start) == 0;
00437 
00438          if (!use_clock)
00439            continue;
00440 
00441          memset (&re, 0, sizeof (re));
00442          re.fastmap = malloc (256);
00443          if (re.fastmap == NULL)
00444            continue;
00445 
00446          err = re_compile_pattern (expr, strlen (expr), &re);
00447          if (err != NULL)
00448            continue;
00449 
00450          if (re_compile_fastmap (&re))
00451            {
00452              regfree (&re);
00453              continue;
00454            }
00455 
00456          while (offset <= memlen)
00457            {
00458              int start;
00459              const char *sp;
00460 
00461              start = re_search (&re, mem, memlen, offset, -offset, NULL);
00462              if (start < -1)
00463               break;
00464 
00465              sp = mem + start;
00466              while (sp > mem && sp[-1] != '\n')
00467               --sp;
00468 
00469              offset = sp - 1 - mem;
00470            }
00471 
00472          regfree (&re);
00473 
00474          use_clock = clock_gettime (cl, &finish) == 0;
00475          if (use_clock)
00476            {
00477              if (finish.tv_nsec < start.tv_nsec)
00478               {
00479                 finish.tv_nsec -= start.tv_nsec - 1000000000;
00480                 finish.tv_sec -= 1 + start.tv_sec;
00481               }
00482              else
00483               {
00484                 finish.tv_nsec -= start.tv_nsec;
00485                 finish.tv_sec -= start.tv_sec;
00486               }
00487              if (finish.tv_sec < mintime.tv_sec
00488                 || (finish.tv_sec == mintime.tv_sec
00489                     && finish.tv_nsec < mintime.tv_nsec))
00490               mintime = finish;
00491            }
00492        }
00493       printf ("elapsed time: %ld.%09ld sec\n",
00494              mintime.tv_sec, mintime.tv_nsec);
00495     }
00496 #endif
00497 
00498   /* Return an error if the number of matches found is not match we
00499      expect.  */
00500   return cnt != expected;
00501 }
00502 
00503 /* If --timing is used we will need a larger timout.  */
00504 #define TIMEOUT 50
00505 #define CMDLINE_OPTIONS \
00506    {"timing", no_argument, &timing, 1 },
00507 #define TEST_FUNCTION do_test ()
00508 #include "../test-skeleton.c"