Blame posix/runptests.c

Packit 6c4009
/* POSIX regex testsuite from IEEE 2003.2.
Packit 6c4009
   Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <regex.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
/* Data structure to describe the tests.  */
Packit 6c4009
struct test
Packit 6c4009
{
Packit 6c4009
  int start;
Packit 6c4009
  int end;
Packit 6c4009
  const char *reg;
Packit 6c4009
  const char *str;
Packit 6c4009
  int options;
Packit 6c4009
} tests[] =
Packit 6c4009
{
Packit 6c4009
#include "ptestcases.h"
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  size_t cnt;
Packit 6c4009
  int errors = 0;
Packit 6c4009
Packit 6c4009
  for (cnt = 0; cnt < sizeof (tests) / sizeof (tests[0]); ++cnt)
Packit 6c4009
    if (tests[cnt].str == NULL)
Packit 6c4009
      {
Packit 6c4009
	printf ("\n%s\n%.*s\n", tests[cnt].reg,
Packit 6c4009
		(int) strlen (tests[cnt].reg),
Packit 6c4009
		"-----------------------------------------------------");
Packit 6c4009
      }
Packit 6c4009
    else if (tests[cnt].reg == NULL)
Packit 6c4009
	printf ("!!! %s\n", tests[cnt].str);
Packit 6c4009
    else
Packit 6c4009
      {
Packit 6c4009
	regex_t re;
Packit 6c4009
	regmatch_t match[20];
Packit 6c4009
	int err;
Packit 6c4009
Packit 6c4009
	printf ("regexp: \"%s\", string: \"%s\" -> ", tests[cnt].reg,
Packit 6c4009
		tests[cnt].str);
Packit 6c4009
Packit 6c4009
	/* Compile the expression.  */
Packit 6c4009
	err = regcomp (&re, tests[cnt].reg, tests[cnt].options);
Packit 6c4009
	if (err != 0)
Packit 6c4009
	  {
Packit 6c4009
	    if (tests[cnt].start == -2)
Packit 6c4009
	      puts ("compiling failed, OK");
Packit 6c4009
	    else
Packit 6c4009
	      {
Packit 6c4009
		char buf[100];
Packit 6c4009
		regerror (err, &re, buf, sizeof (buf));
Packit 6c4009
		printf ("FAIL: %s\n", buf);
Packit 6c4009
		++errors;
Packit 6c4009
	      }
Packit 6c4009
Packit 6c4009
	    continue;
Packit 6c4009
	  }
Packit 6c4009
	else if (tests[cnt].start == -2)
Packit 6c4009
	  {
Packit 6c4009
	    puts ("compiling suceeds, FAIL");
Packit 6c4009
	    errors++;
Packit 6c4009
	    continue;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	/* Run the actual test.  */
Packit 6c4009
	err = regexec (&re, tests[cnt].str, 20, match, 0);
Packit 6c4009
Packit 6c4009
	if (err != 0)
Packit 6c4009
	  {
Packit 6c4009
	    if (tests[cnt].start == -1)
Packit 6c4009
	      puts ("no match, OK");
Packit 6c4009
	    else
Packit 6c4009
	      {
Packit 6c4009
		puts ("no match, FAIL");
Packit 6c4009
		++errors;
Packit 6c4009
	      }
Packit 6c4009
	  }
Packit 6c4009
	else
Packit 6c4009
	  {
Packit 6c4009
	    if (match[0].rm_so == 0 && tests[cnt].start == 0
Packit 6c4009
		&& match[0].rm_eo == 0 && tests[cnt].end == 0)
Packit 6c4009
	      puts ("match, OK");
Packit 6c4009
	    else if (match[0].rm_so + 1 == tests[cnt].start
Packit 6c4009
		     && match[0].rm_eo == tests[cnt].end)
Packit 6c4009
	      puts ("match, OK");
Packit 6c4009
	    else
Packit 6c4009
	      {
Packit 6c4009
		printf ("wrong match (%d to %d): FAIL\n",
Packit 6c4009
			match[0].rm_so, match[0].rm_eo);
Packit 6c4009
		++errors;
Packit 6c4009
	      }
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	/* Free all resources.  */
Packit 6c4009
	regfree (&re);
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  printf ("\n%Zu tests, %d errors\n", cnt, errors);
Packit 6c4009
Packit 6c4009
  return errors != 0;
Packit 6c4009
}