50b157b76f3a760951274722cc0753b5f9564454
[platform/upstream/glibc.git] / posix / runptests.c
1 /* POSIX regex testsuite from IEEE 2003.2.
2    Copyright (C) 1998 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <sys/types.h>
22 #include <regex.h>
23 #include <stdio.h>
24
25 /* Data structure to describe the tests.  */
26 struct test
27 {
28   int start;
29   int end;
30   const char *reg;
31   const char *str;
32   int options;
33 } tests[] =
34 {
35 #include "ptestcases.h"
36 };
37
38
39 int
40 main (int argc, char *argv[])
41 {
42   size_t cnt;
43   int errors = 0;
44
45   for (cnt = 0; cnt < sizeof (tests) / sizeof (tests[0]); ++cnt)
46     if (tests[cnt].str == NULL)
47       {
48         printf ("\n%s\n%.*s\n", tests[cnt].reg,
49                 (int) strlen (tests[cnt].reg),
50                 "-----------------------------------------------------");
51       }
52     else
53       {
54         regex_t re;
55         regmatch_t match[20];
56         int err;
57
58         printf ("regexp: \"%s\", string: \"%s\" -> ", tests[cnt].reg,
59                 tests[cnt].str);
60
61         /* Compile the expression.  */
62         err = regcomp (&re, tests[cnt].reg, tests[cnt].options);
63         if (err != 0)
64           {
65             if (tests[cnt].start == -1)
66               puts ("failed, OK");
67             else
68               {
69                 char buf[100];
70                 regerror (err, &re, buf, sizeof (buf));
71                 printf ("FAIL: %s\n", buf);
72                 ++errors;
73               }
74
75             continue;
76           }
77
78         /* Run the actual test.  */
79         err = regexec (&re, tests[cnt].str, 20, match, 0);
80
81         if (err != 0)
82           {
83             if (tests[cnt].start == -1)
84               puts ("no match, OK");
85             else
86               {
87                 puts ("no match, FAIL");
88                 ++errors;
89               }
90           }
91         else
92           {
93             if (match[0].rm_so == 0 && tests[cnt].start == 0
94                 && match[0].rm_eo == 0 && tests[cnt].end == 0)
95               puts ("match, OK");
96             else if (match[0].rm_so + 1 == tests[cnt].start
97                      && match[0].rm_eo == tests[cnt].end)
98               puts ("match, OK");
99             else
100               {
101                 printf ("wrong match (%d to %d): FAIL\n",
102                         match[0].rm_so, match[0].rm_eo);
103                 ++errors;
104               }
105           }
106
107         /* Free all resources.  */
108         regfree (&re);
109       }
110
111   printf ("\n%u tests, %d errors\n", cnt, errors);
112
113   /* We should return here the error status but since some tests are known
114      to fail this would only cause the libc testsuite to fail.  */
115   //return errors != 0;
116   return 0;
117 }