Back to index

glibc  2.9
runptests.c
Go to the documentation of this file.
00001 /* POSIX regex testsuite from IEEE 2003.2.
00002    Copyright (C) 1998, 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <sys/types.h>
00022 #include <regex.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 
00026 /* Data structure to describe the tests.  */
00027 struct test
00028 {
00029   int start;
00030   int end;
00031   const char *reg;
00032   const char *str;
00033   int options;
00034 } tests[] =
00035 {
00036 #include "ptestcases.h"
00037 };
00038 
00039 
00040 int
00041 main (int argc, char *argv[])
00042 {
00043   size_t cnt;
00044   int errors = 0;
00045 
00046   for (cnt = 0; cnt < sizeof (tests) / sizeof (tests[0]); ++cnt)
00047     if (tests[cnt].str == NULL)
00048       {
00049        printf ("\n%s\n%.*s\n", tests[cnt].reg,
00050               (int) strlen (tests[cnt].reg),
00051               "-----------------------------------------------------");
00052       }
00053     else if (tests[cnt].reg == NULL)
00054        printf ("!!! %s\n", tests[cnt].str);
00055     else
00056       {
00057        regex_t re;
00058        regmatch_t match[20];
00059        int err;
00060 
00061        printf ("regexp: \"%s\", string: \"%s\" -> ", tests[cnt].reg,
00062               tests[cnt].str);
00063 
00064        /* Compile the expression.  */
00065        err = regcomp (&re, tests[cnt].reg, tests[cnt].options);
00066        if (err != 0)
00067          {
00068            if (tests[cnt].start == -2)
00069              puts ("compiling failed, OK");
00070            else
00071              {
00072               char buf[100];
00073               regerror (err, &re, buf, sizeof (buf));
00074               printf ("FAIL: %s\n", buf);
00075               ++errors;
00076              }
00077 
00078            continue;
00079          }
00080        else if (tests[cnt].start == -2)
00081          {
00082            puts ("compiling suceeds, FAIL");
00083            errors++;
00084            continue;
00085          }
00086 
00087        /* Run the actual test.  */
00088        err = regexec (&re, tests[cnt].str, 20, match, 0);
00089 
00090        if (err != 0)
00091          {
00092            if (tests[cnt].start == -1)
00093              puts ("no match, OK");
00094            else
00095              {
00096               puts ("no match, FAIL");
00097               ++errors;
00098              }
00099          }
00100        else
00101          {
00102            if (match[0].rm_so == 0 && tests[cnt].start == 0
00103               && match[0].rm_eo == 0 && tests[cnt].end == 0)
00104              puts ("match, OK");
00105            else if (match[0].rm_so + 1 == tests[cnt].start
00106                    && match[0].rm_eo == tests[cnt].end)
00107              puts ("match, OK");
00108            else
00109              {
00110               printf ("wrong match (%d to %d): FAIL\n",
00111                      match[0].rm_so, match[0].rm_eo);
00112               ++errors;
00113              }
00114          }
00115 
00116        /* Free all resources.  */
00117        regfree (&re);
00118       }
00119 
00120   printf ("\n%Zu tests, %d errors\n", cnt, errors);
00121 
00122   return errors != 0;
00123 }