| 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 |
|
|---|
| 28 | #include <sys/types.h>
|
|---|
| 29 | #include <regex.h>
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 | #include <stdlib.h>
|
|---|
| 32 | #include <string.h>
|
|---|
| 33 |
|
|---|
| 34 | |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | struct a_test
|
|---|
| 38 | {
|
|---|
| 39 | int expected;
|
|---|
| 40 | const char * pattern;
|
|---|
| 41 | const unsigned char * data;
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | static const struct a_test the_tests[] =
|
|---|
| 45 | {
|
|---|
| 46 | #include "testcases.h"
|
|---|
| 47 | {-1, 0, 0}
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | static int
|
|---|
| 55 | run_a_test (int id, const struct a_test * t)
|
|---|
| 56 | {
|
|---|
| 57 | static const char * last_pattern = 0;
|
|---|
| 58 | static regex_t r;
|
|---|
| 59 | int err;
|
|---|
| 60 | char errmsg[100];
|
|---|
| 61 | int x;
|
|---|
| 62 | regmatch_t regs[10];
|
|---|
| 63 |
|
|---|
| 64 | if (!last_pattern || strcmp (last_pattern, t->pattern))
|
|---|
| 65 | {
|
|---|
| 66 | if (last_pattern)
|
|---|
| 67 | regfree (&r);
|
|---|
| 68 | last_pattern = t->pattern;
|
|---|
| 69 | err = regcomp (&r, t->pattern, REG_EXTENDED);
|
|---|
| 70 | if (err)
|
|---|
| 71 | {
|
|---|
| 72 | if (t->expected == 2)
|
|---|
| 73 | {
|
|---|
| 74 | puts (" OK.");
|
|---|
| 75 | return 0;
|
|---|
| 76 | }
|
|---|
| 77 | if (last_pattern)
|
|---|
| 78 | regfree (&r);
|
|---|
| 79 | last_pattern = NULL;
|
|---|
| 80 | regerror (err, &r, errmsg, 100);
|
|---|
| 81 | printf (" FAIL: %s.\n", errmsg);
|
|---|
| 82 | return 1;
|
|---|
| 83 | }
|
|---|
| 84 | else if (t->expected == 2)
|
|---|
| 85 | {
|
|---|
| 86 | printf ("test %d\n", id);
|
|---|
| 87 | printf ("pattern \"%s\" successfull compilation not expected\n",
|
|---|
| 88 | t->pattern);
|
|---|
| 89 | return 1;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | err = regexec (&r, t->data, 10, regs, 0);
|
|---|
| 94 |
|
|---|
| 95 | if (err != t->expected)
|
|---|
| 96 | {
|
|---|
| 97 | printf ("test %d\n", id);
|
|---|
| 98 | printf ("pattern \"%s\" data \"%s\" wanted %d got %d\n",
|
|---|
| 99 | t->pattern, t->data, t->expected, err);
|
|---|
| 100 | for (x = 0; x < 10; ++x)
|
|---|
| 101 | printf ("reg %d == (%d, %d) %.*s\n",
|
|---|
| 102 | x,
|
|---|
| 103 | regs[x].rm_so,
|
|---|
| 104 | regs[x].rm_eo,
|
|---|
| 105 | regs[x].rm_eo - regs[x].rm_so,
|
|---|
| 106 | t->data + regs[x].rm_so);
|
|---|
| 107 | return 1;
|
|---|
| 108 | }
|
|---|
| 109 | puts (" OK.");
|
|---|
| 110 | return 0;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 | int
|
|---|
| 117 | main (int argc, char * argv[])
|
|---|
| 118 | {
|
|---|
| 119 | int x;
|
|---|
| 120 | int lo;
|
|---|
| 121 | int hi;
|
|---|
| 122 | int res = 0;
|
|---|
| 123 |
|
|---|
| 124 | lo = 0;
|
|---|
| 125 | hi = (sizeof (the_tests) / sizeof (the_tests[0])) - 1;
|
|---|
| 126 |
|
|---|
| 127 | if (argc > 1)
|
|---|
| 128 | {
|
|---|
| 129 | lo = atoi (argv[1]);
|
|---|
| 130 | hi = lo + 1;
|
|---|
| 131 |
|
|---|
| 132 | if (argc > 2)
|
|---|
| 133 | hi = atoi (argv[2]);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | for (x = lo; x < hi; ++x)
|
|---|
| 137 | {
|
|---|
| 138 | printf ("#%d:", x);
|
|---|
| 139 | res |= run_a_test (x, &the_tests[x]);
|
|---|
| 140 | }
|
|---|
| 141 | return res != 0;
|
|---|
| 142 | }
|
|---|