summary refs log tree commit diff
path: root/posix/runtests.c
blob: b6a292e6f5b1457eea56cc2fc84aa6f865841bd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/***********************************************************

Copyright 1995 by Tom Lord

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the name of the copyright holder not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

******************************************************************/



#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>



struct a_test
{
  int expected;
  const char * pattern;
  const unsigned char * data;
};

static const struct a_test the_tests[] = 
{
#include "testcases.h"
  {-1, 0, 0}
};




int
run_a_test (int id, const struct a_test * t)
{
  static const char * last_pattern = 0;
  static regex_t r;
  int err;
  char errmsg[100];
  int x;
  regmatch_t regs[10];

  if (!last_pattern || strcmp (last_pattern, t->pattern))
    {
      if (last_pattern)
	regfree (&r);
      last_pattern = t->pattern;
      err = regcomp (&r, t->pattern, REG_EXTENDED);
      if (err)
	{
	  if (t->expected)
	    {
	      puts (" OK.");
	      return 0;
	    }
	  regerror (err, &r, errmsg, 100);
	  printf ("test %d\n", id);
	  puts (errmsg);
	  return 1;
	}
    }
      
  err = regexec (&r, t->data, 10, regs, 0);

  if (err != t->expected)
    {
      printf ("test %d\n", id);
      printf ("pattern \"%s\" data \"%s\" wanted %d got %d\n",
	      t->pattern, t->data, t->expected, err);
      for (x = 0; x < 10; ++x)
	printf ("reg %d == (%d, %d) %.*s\n",
		x,
		regs[x].rm_so,
		regs[x].rm_eo,
		regs[x].rm_eo - regs[x].rm_so,
		t->data + regs[x].rm_so);
      return 1;
    }
  puts (" OK.");
  return 0;
}



int
main (int argc, char * argv[])
{
  int x;
  int lo;
  int hi;
  int res = 0;

  lo = 0;
  hi = (sizeof (the_tests) / sizeof (the_tests[0])) - 1;

  if (argc > 1)
    {
      lo = atoi (argv[1]);
      hi = lo + 1;

      if (argc > 2)
	hi = atoi (argv[2]);
    }

  for (x = lo; x < hi; ++x)
    {
      printf ("#%d:", x);
      res |= run_a_test (x, &the_tests[x]);
    }
  {
    exit (0);
  }
}