Back to index

glibc  2.9
runtests.c
Go to the documentation of this file.
00001 /***********************************************************
00002 
00003 Copyright 1995 by Tom Lord
00004 
00005                         All Rights Reserved
00006 
00007 Permission to use, copy, modify, and distribute this software and its
00008 documentation for any purpose and without fee is hereby granted,
00009 provided that the above copyright notice appear in all copies and that
00010 both that copyright notice and this permission notice appear in
00011 supporting documentation, and that the name of the copyright holder not be
00012 used in advertising or publicity pertaining to distribution of the
00013 software without specific, written prior permission.
00014 
00015 Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00016 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
00017 EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
00018 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
00019 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
00020 OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00021 PERFORMANCE OF THIS SOFTWARE.
00022 
00023 ******************************************************************/
00024 
00025 
00026 
00027 #include <sys/types.h>
00028 #include <regex.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 
00033 
00034 
00035 struct a_test
00036 {
00037   int expected;
00038   const char * pattern;
00039   const char * data;
00040 };
00041 
00042 static const struct a_test the_tests[] =
00043 {
00044 #include "testcases.h"
00045   {-1, 0, 0}
00046 };
00047 
00048 
00049 
00050 
00051 static int
00052 run_a_test (int id, const struct a_test * t)
00053 {
00054   static const char * last_pattern = 0;
00055   static regex_t r;
00056   int err;
00057   char errmsg[100];
00058   int x;
00059   regmatch_t regs[10];
00060 
00061   if (!last_pattern || strcmp (last_pattern, t->pattern))
00062     {
00063       if (last_pattern)
00064        regfree (&r);
00065       last_pattern = t->pattern;
00066       err = regcomp (&r, t->pattern, REG_EXTENDED);
00067       if (err)
00068        {
00069          if (t->expected == 2)
00070            {
00071              puts (" OK.");
00072              return 0;
00073            }
00074          if (last_pattern)
00075            regfree (&r);
00076          last_pattern = NULL;
00077          regerror (err, &r, errmsg, 100);
00078          printf (" FAIL: %s.\n", errmsg);
00079          return 1;
00080        }
00081       else if (t->expected == 2)
00082        {
00083          printf ("test %d\n", id);
00084          printf ("pattern \"%s\" successfull compilation not expected\n",
00085                 t->pattern);
00086          return 1;
00087        }
00088     }
00089 
00090   err = regexec (&r, t->data, 10, regs, 0);
00091 
00092   if (err != t->expected)
00093     {
00094       printf ("test %d\n", id);
00095       printf ("pattern \"%s\" data \"%s\" wanted %d got %d\n",
00096              t->pattern, t->data, t->expected, err);
00097       for (x = 0; x < 10; ++x)
00098        printf ("reg %d == (%d, %d) %.*s\n",
00099               x,
00100               regs[x].rm_so,
00101               regs[x].rm_eo,
00102               regs[x].rm_eo - regs[x].rm_so,
00103               t->data + regs[x].rm_so);
00104       return 1;
00105     }
00106   puts (" OK.");
00107   return 0;
00108 }
00109 
00110 
00111 
00112 int
00113 main (int argc, char * argv[])
00114 {
00115   int x;
00116   int lo;
00117   int hi;
00118   int res = 0;
00119 
00120   lo = 0;
00121   hi = (sizeof (the_tests) / sizeof (the_tests[0])) - 1;
00122 
00123   if (argc > 1)
00124     {
00125       lo = atoi (argv[1]);
00126       hi = lo + 1;
00127 
00128       if (argc > 2)
00129        hi = atoi (argv[2]);
00130     }
00131 
00132   for (x = lo; x < hi; ++x)
00133     {
00134       printf ("#%d:", x);
00135       res |= run_a_test (x, &the_tests[x]);
00136     }
00137   return res != 0;
00138 }