tests.c revision f14f4646
1/* 2 * Copyright (c) 2002 by The XFree86 Project, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 * SOFTWARE. 21 * 22 * Except as contained in this notice, the name of the XFree86 Project shall 23 * not be used in advertising or otherwise to promote the sale, use or other 24 * dealings in this Software without prior written authorization from the 25 * XFree86 Project. 26 * 27 * Author: Paulo César Pereira de Andrade 28 */ 29 30/* $XFree86$ */ 31 32/* 33 * Compile with: cc -o tests tests.c -L. -lre 34 */ 35 36#include <stdio.h> 37#include <string.h> 38#include "re.h" 39 40int 41main(int argc, char *argv[]) 42{ 43 re_cod cod; 44 re_mat mat[10]; 45 int line, ecode, i, len, group, failed; 46 long eo, so; 47 char buf[8192]; 48 char str[8192]; 49 FILE *fp = fopen("tests.txt", "r"); 50 51 if (fp == NULL) { 52 fprintf(stderr, "failed to open tests.txt\n"); 53 exit(1); 54 } 55 56 ecode = line = group = failed = 0; 57 cod.cod = NULL; 58 while (fgets(buf, sizeof(buf), fp)) { 59 ++line; 60 if (buf[0] == '#' || buf[0] == '\n') 61 continue; 62 else if (buf[0] == '/') { 63 char *ptr = strrchr(buf, '/'); 64 65 if (ptr == buf) { 66 fprintf(stderr, "syntax error at line %d\n", line); 67 break; 68 } 69 else { 70 int flags = 0; 71 72 refree(&cod); 73 for (*ptr++ = '\0'; *ptr; ptr++) { 74 if (*ptr == 'i') 75 flags |= RE_ICASE; 76 else if (*ptr == 'n') 77 flags |= RE_NEWLINE; 78 } 79 ecode = recomp(&cod, buf + 1, flags); 80 failed = ecode; 81 } 82 } 83 else if (buf[0] == '>') { 84 if (cod.cod == NULL) { 85 fprintf(stderr, "no previous pattern at line %d\n", line); 86 break; 87 } 88 len = strlen(buf) - 1; 89 buf[len] = '\0'; 90 strcpy(str, buf + 1); 91 for (i = 0, --len; i < len - 1; i++) { 92 if (str[i] == '\\') { 93 memmove(str + i, str + i + 1, len); 94 --len; 95 switch (str[i]) { 96 case 'a': 97 str[i] = '\a'; 98 break; 99 case 'b': 100 str[i] = '\b'; 101 break; 102 case 'f': 103 str[i] = '\f'; 104 break; 105 case 'n': 106 str[i] = '\n'; 107 break; 108 case 'r': 109 str[i] = '\r'; 110 break; 111 case 't': 112 str[i] = '\t'; 113 break; 114 case 'v': 115 str[i] = '\v'; 116 break; 117 default: 118 break; 119 } 120 } 121 } 122 group = 0; 123 ecode = reexec(&cod, str, 10, &mat[0], 0); 124 if (ecode && ecode != RE_NOMATCH) { 125 reerror(failed, &cod, buf, sizeof(buf)); 126 fprintf(stderr, "%s, at line %d\n", buf, line); 127 break; 128 } 129 } 130 else if (buf[0] == ':') { 131 if (failed) { 132 len = strlen(buf) - 1; 133 buf[len] = '\0'; 134 if (failed == RE_EESCAPE && strcmp(buf, ":EESCAPE") == 0) 135 continue; 136 if (failed == RE_ESUBREG && strcmp(buf, ":ESUBREG") == 0) 137 continue; 138 if (failed == RE_EBRACK && strcmp(buf, ":EBRACK") == 0) 139 continue; 140 if (failed == RE_EPAREN && strcmp(buf, ":EPAREN") == 0) 141 continue; 142 if (failed == RE_EBRACE && strcmp(buf, ":EBRACE") == 0) 143 continue; 144 if (failed == RE_EBADBR && strcmp(buf, ":EBADBR") == 0) 145 continue; 146 if (failed == RE_ERANGE && strcmp(buf, ":ERANGE") == 0) 147 continue; 148 if (failed == RE_ESPACE && strcmp(buf, ":ESPACE") == 0) 149 continue; 150 if (failed == RE_BADRPT && strcmp(buf, ":BADRPT") == 0) 151 continue; 152 if (failed == RE_EMPTY && strcmp(buf, ":EMPTY") == 0) 153 continue; 154 reerror(failed, &cod, buf, sizeof(buf)); 155 fprintf(stderr, "Error value %d doesn't match: %s, at line %d\n", 156 failed, buf, line); 157 break; 158 } 159 else if (!ecode) { 160 fprintf(stderr, "found match when shoudn't, at line %d\n", line); 161 break; 162 } 163 } 164 else { 165 if (failed) { 166 reerror(failed, &cod, buf, sizeof(buf)); 167 fprintf(stderr, "%s, at line %d\n", buf, line); 168 break; 169 } 170 if (sscanf(buf, "%ld,%ld:", &so, &eo) != 2) { 171 fprintf(stderr, "expecting match offsets at line %d\n", line); 172 break; 173 } 174 else if (ecode) { 175 fprintf(stderr, "didn't match, at line %d\n", line); 176 break; 177 } 178 else if (group >= 10) { 179 fprintf(stderr, "syntax error at line %d (too many groups)\n", 180 line); 181 break; 182 } 183 else if (so != mat[group].rm_so || eo != mat[group].rm_eo) { 184 fprintf(stderr, "match failed at line %d, got %ld,%ld: ", 185 line, mat[group].rm_so, mat[group].rm_eo); 186 if (mat[group].rm_so < mat[group].rm_eo) 187 fwrite(str + mat[group].rm_so, 188 mat[group].rm_eo - mat[group].rm_so, 1, stderr); 189 fputc('\n', stderr); 190 break; 191 } 192 ++group; 193 } 194 } 195 196 fclose(fp); 197 198 return (ecode); 199} 200