Blame posix/runtests.c

Packit 6c4009
/***********************************************************
Packit 6c4009
Packit 6c4009
Copyright 1995 by Tom Lord
Packit 6c4009
Packit 6c4009
                        All Rights Reserved
Packit 6c4009
Packit 6c4009
Permission to use, copy, modify, and distribute this software and its
Packit 6c4009
documentation for any purpose and without fee is hereby granted,
Packit 6c4009
provided that the above copyright notice appear in all copies and that
Packit 6c4009
both that copyright notice and this permission notice appear in
Packit 6c4009
supporting documentation, and that the name of the copyright holder not be
Packit 6c4009
used in advertising or publicity pertaining to distribution of the
Packit 6c4009
software without specific, written prior permission.
Packit 6c4009
Packit 6c4009
Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
Packit 6c4009
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
Packit 6c4009
EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Packit 6c4009
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
Packit 6c4009
USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
Packit 6c4009
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
Packit 6c4009
PERFORMANCE OF THIS SOFTWARE.
Packit 6c4009
Packit 6c4009
******************************************************************/
Packit 6c4009
Packit 6c4009
Packit 6c4009

Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <regex.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009

Packit 6c4009
Packit 6c4009
struct a_test
Packit 6c4009
{
Packit 6c4009
  int expected;
Packit 6c4009
  const char * pattern;
Packit 6c4009
  const char * data;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static const struct a_test the_tests[] =
Packit 6c4009
{
Packit 6c4009
#include "testcases.h"
Packit 6c4009
  {-1, 0, 0}
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009

Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
run_a_test (int id, const struct a_test * t)
Packit 6c4009
{
Packit 6c4009
  static const char * last_pattern = 0;
Packit 6c4009
  static regex_t r;
Packit 6c4009
  int err;
Packit 6c4009
  char errmsg[100];
Packit 6c4009
  int x;
Packit 6c4009
  regmatch_t regs[10];
Packit 6c4009
Packit 6c4009
  if (!last_pattern || strcmp (last_pattern, t->pattern))
Packit 6c4009
    {
Packit 6c4009
      if (last_pattern)
Packit 6c4009
	regfree (&r);
Packit 6c4009
      last_pattern = t->pattern;
Packit 6c4009
      err = regcomp (&r, t->pattern, REG_EXTENDED);
Packit 6c4009
      if (err)
Packit 6c4009
	{
Packit 6c4009
	  if (t->expected == 2)
Packit 6c4009
	    {
Packit 6c4009
	      puts (" OK.");
Packit 6c4009
	      return 0;
Packit 6c4009
	    }
Packit 6c4009
	  if (last_pattern)
Packit 6c4009
	    regfree (&r);
Packit 6c4009
	  last_pattern = NULL;
Packit 6c4009
	  regerror (err, &r, errmsg, 100);
Packit 6c4009
	  printf (" FAIL: %s.\n", errmsg);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      else if (t->expected == 2)
Packit 6c4009
	{
Packit 6c4009
	  printf ("test %d\n", id);
Packit 6c4009
	  printf ("pattern \"%s\" successfull compilation not expected\n",
Packit 6c4009
		  t->pattern);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  err = regexec (&r, t->data, 10, regs, 0);
Packit 6c4009
Packit 6c4009
  if (err != t->expected)
Packit 6c4009
    {
Packit 6c4009
      printf ("test %d\n", id);
Packit 6c4009
      printf ("pattern \"%s\" data \"%s\" wanted %d got %d\n",
Packit 6c4009
	      t->pattern, t->data, t->expected, err);
Packit 6c4009
      for (x = 0; x < 10; ++x)
Packit 6c4009
	printf ("reg %d == (%d, %d) %.*s\n",
Packit 6c4009
		x,
Packit 6c4009
		regs[x].rm_so,
Packit 6c4009
		regs[x].rm_eo,
Packit 6c4009
		regs[x].rm_eo - regs[x].rm_so,
Packit 6c4009
		t->data + regs[x].rm_so);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  puts (" OK.");
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009

Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char * argv[])
Packit 6c4009
{
Packit 6c4009
  int x;
Packit 6c4009
  int lo;
Packit 6c4009
  int hi;
Packit 6c4009
  int res = 0;
Packit 6c4009
Packit 6c4009
  lo = 0;
Packit 6c4009
  hi = (sizeof (the_tests) / sizeof (the_tests[0])) - 1;
Packit 6c4009
Packit 6c4009
  if (argc > 1)
Packit 6c4009
    {
Packit 6c4009
      lo = atoi (argv[1]);
Packit 6c4009
      hi = lo + 1;
Packit 6c4009
Packit 6c4009
      if (argc > 2)
Packit 6c4009
	hi = atoi (argv[2]);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (x = lo; x < hi; ++x)
Packit 6c4009
    {
Packit 6c4009
      printf ("#%d:", x);
Packit 6c4009
      res |= run_a_test (x, &the_tests[x]);
Packit 6c4009
    }
Packit 6c4009
  return res != 0;
Packit 6c4009
}