1/* POSIX regex testsuite from IEEE 2003.2.
2   Copyright (C) 1998, 2003 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 Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the 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   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with the GNU C Library; if not, write to the Free
18   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19   02110-1301 USA.  */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <sys/types.h>
26#include <regex.h>
27#include <stdio.h>
28#include <string.h>
29
30/* Data structure to describe the tests.  */
31struct test
32{
33  int start;
34  int end;
35  const char *reg;
36  const char *str;
37  int options;
38} tests[] =
39{
40#include "ptestcases.h"
41};
42
43
44int
45main (int argc, char *argv[])
46{
47  size_t cnt;
48  int errors = 0;
49
50  for (cnt = 0; cnt < sizeof (tests) / sizeof (tests[0]); ++cnt)
51    if (tests[cnt].str == NULL)
52      {
53	printf ("\n%s\n%.*s\n", tests[cnt].reg,
54		(int) strlen (tests[cnt].reg),
55		"-----------------------------------------------------");
56      }
57    else if (tests[cnt].reg == NULL)
58	printf ("!!! %s\n", tests[cnt].str);
59    else
60      {
61	regex_t re;
62	regmatch_t match[20];
63	int err;
64
65	printf ("regexp: \"%s\", string: \"%s\" -> ", tests[cnt].reg,
66		tests[cnt].str);
67
68	/* Compile the expression.  */
69	err = regcomp (&re, tests[cnt].reg, tests[cnt].options);
70	if (err != 0)
71	  {
72	    if (tests[cnt].start == -2)
73	      puts ("compiling failed, OK");
74	    else
75	      {
76		char buf[100];
77		regerror (err, &re, buf, sizeof (buf));
78		printf ("FAIL: %s\n", buf);
79		++errors;
80	      }
81
82	    continue;
83	  }
84	else if (tests[cnt].start == -2)
85	  {
86	    puts ("compiling suceeds, FAIL");
87	    errors++;
88	    continue;
89	  }
90
91	/* Run the actual test.  */
92	err = regexec (&re, tests[cnt].str, 20, match, 0);
93
94	if (err != 0)
95	  {
96	    if (tests[cnt].start == -1)
97	      puts ("no match, OK");
98	    else
99	      {
100		puts ("no match, FAIL");
101		++errors;
102	      }
103	  }
104	else
105	  {
106	    if (match[0].rm_so == 0 && tests[cnt].start == 0
107		&& match[0].rm_eo == 0 && tests[cnt].end == 0)
108	      puts ("match, OK");
109	    else if (match[0].rm_so + 1 == tests[cnt].start
110		     && match[0].rm_eo == tests[cnt].end)
111	      puts ("match, OK");
112	    else
113	      {
114		printf ("wrong match (%d to %d): FAIL\n",
115			match[0].rm_so, match[0].rm_eo);
116		++errors;
117	      }
118	  }
119
120	/* Free all resources.  */
121	regfree (&re);
122      }
123
124  printf ("\n%u tests, %d errors\n", (int) cnt, errors);
125
126  return errors != 0;
127}
128