1 /*********************************************************** 2 3 Copyright 1995 by Tom Lord 4 5 All Rights Reserved 6 7 Permission to use, copy, modify, and distribute this software and its 8 documentation for any purpose and without fee is hereby granted, 9 provided that the above copyright notice appear in all copies and that 10 both that copyright notice and this permission notice appear in 11 supporting documentation, and that the name of the copyright holder not be 12 used in advertising or publicity pertaining to distribution of the 13 software without specific, written prior permission. 14 15 Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF 19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 20 OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 21 PERFORMANCE OF THIS SOFTWARE. 22 23 ******************************************************************/ 24 25 26 27 #ifdef HAVE_CONFIG_H 29 #include "config.h" 30 #endif 31 32 #include <sys/types.h> 33 #include <regex.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 38 39 41 struct a_test 42 { 43 int expected; 44 const char * pattern; 45 const unsigned char * data; 46 }; 47 48 static const struct a_test the_tests[] = 49 { 50 #include "testcases.h" 51 {-1, 0, 0} 52 }; 53 54 55 56 58 static int 59 run_a_test (int id, const struct a_test * t) 60 { 61 static const char * last_pattern = 0; 62 static regex_t r; 63 int err; 64 char errmsg[100]; 65 int x; 66 regmatch_t regs[10]; 67 68 if (!last_pattern || strcmp (last_pattern, t->pattern)) 69 { 70 if (last_pattern) 71 regfree (&r); 72 last_pattern = t->pattern; 73 err = regcomp (&r, t->pattern, REG_EXTENDED); 74 if (err) 75 { 76 if (t->expected == 2) 77 { 78 puts (" OK."); 79 return 0; 80 } 81 if (last_pattern) 82 regfree (&r); 83 last_pattern = NULL; 84 regerror (err, &r, errmsg, 100); 85 printf (" FAIL: %s.\n", errmsg); 86 return 1; 87 } 88 else if (t->expected == 2) 89 { 90 printf ("test %d\n", id); 91 printf ("pattern \"%s\" successfull compilation not expected\n", 92 t->pattern); 93 return 1; 94 } 95 } 96 97 for (x = 0; x < 10; ++x) 98 regs[x].rm_so = regs[x].rm_eo = -1; 99 100 err = regexec (&r, t->data, 10, regs, 0); 101 102 if (err != t->expected) 103 { 104 printf ("test %d\n", id); 105 printf ("pattern \"%s\" data \"%s\" wanted %d got %d\n", 106 t->pattern, t->data, t->expected, err); 107 for (x = 0; x < 10; ++x) 108 if (regs[x].rm_so != -1) 109 printf ("reg %d == (%d, %d) %.*s\n", 110 x, 111 regs[x].rm_so, 112 regs[x].rm_eo, 113 regs[x].rm_eo - regs[x].rm_so, 114 t->data + regs[x].rm_so); 115 return 1; 116 } 117 puts (" OK."); 118 return 0; 119 } 120 121 122 124 int 125 main (int argc, char * argv[]) 126 { 127 int x; 128 int lo; 129 int hi; 130 int res = 0; 131 132 lo = 0; 133 hi = (sizeof (the_tests) / sizeof (the_tests[0])) - 1; 134 135 if (argc > 1) 136 { 137 lo = atoi (argv[1]); 138 hi = lo + 1; 139 140 if (argc > 2) 141 hi = atoi (argv[2]); 142 } 143 144 for (x = lo; x < hi; ++x) 145 { 146 printf ("#%d:", x); 147 res |= run_a_test (x, &the_tests[x]); 148 } 149 return res != 0; 150 } 151