wildtest.c

説明を見る。
00001 /*
00002 **  wildmatch test suite.
00003 */
00004 
00005 /*#define COMPARE_WITH_FNMATCH*/
00006 
00007 #define WILD_TEST_ITERATIONS
00008 #include "lib/wildmatch.c"
00009 
00010 #include <popt.h>
00011 
00012 #ifdef COMPARE_WITH_FNMATCH
00013 #include <fnmatch.h>
00014 
00015 int fnmatch_errors = 0;
00016 #endif
00017 
00018 int wildmatch_errors = 0;
00019 
00020 typedef char bool;
00021 
00022 int output_iterations = 0;
00023 int explode_mod = 0;
00024 int empties_mod = 0;
00025 int empty_at_start = 0;
00026 int empty_at_end = 0;
00027 
00028 static struct poptOption long_options[] = {
00029   /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
00030   {"iterations",     'i', POPT_ARG_NONE,   &output_iterations, 0, 0, 0},
00031   {"empties",        'e', POPT_ARG_STRING, 0, 'e', 0, 0},
00032   {"explode",        'x', POPT_ARG_INT,    &explode_mod, 0, 0, 0},
00033   {0,0,0,0, 0, 0, 0}
00034 };
00035 
00036 /* match just at the start of string (anchored tests) */
00037 static void
00038 run_test(int line, bool matches, bool same_as_fnmatch,
00039          const char *text, const char *pattern)
00040 {
00041     bool matched;
00042 #ifdef COMPARE_WITH_FNMATCH
00043     bool fn_matched;
00044     int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
00045 #else
00046     same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
00047 #endif
00048 
00049     if (explode_mod) {
00050         char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
00051         int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
00052 
00053         if (empty_at_start)
00054             texts[ndx++] = "";
00055         /* An empty string must turn into at least one empty array item. */
00056         while (1) {
00057             texts[ndx] = buf + ndx * (explode_mod + 1);
00058             strlcpy(texts[ndx++], text + pos, explode_mod + 1);
00059             if (pos + explode_mod >= len)
00060                 break;
00061             pos += explode_mod;
00062             if (!(++cnt % empties_mod))
00063                 texts[ndx++] = "";
00064         }
00065         if (empty_at_end)
00066             texts[ndx++] = "";
00067         texts[ndx] = NULL;
00068         matched = wildmatch_array(pattern, (const char**)texts, 0);
00069     } else
00070         matched = wildmatch(pattern, text);
00071 #ifdef COMPARE_WITH_FNMATCH
00072     fn_matched = !fnmatch(pattern, text, flags);
00073 #endif
00074     if (matched != matches) {
00075         printf("wildmatch failure on line %d:\n  %s\n  %s\n  expected %s match\n",
00076                line, text, pattern, matches? "a" : "NO");
00077         wildmatch_errors++;
00078     }
00079 #ifdef COMPARE_WITH_FNMATCH
00080     if (fn_matched != (matches ^ !same_as_fnmatch)) {
00081         printf("fnmatch disagreement on line %d:\n  %s\n  %s\n  expected %s match\n",
00082                line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
00083         fnmatch_errors++;
00084     }
00085 #endif
00086     if (output_iterations) {
00087         printf("%d: \"%s\" iterations = %d\n", line, pattern,
00088                wildmatch_iteration_count);
00089     }
00090 }
00091 
00092 int
00093 main(int argc, char **argv)
00094 {
00095     char buf[2048], *s, *string[2], *end[2];
00096     const char *arg;
00097     FILE *fp;
00098     int opt, line, i, flag[2];
00099     poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
00100                                     long_options, 0);
00101 
00102     while ((opt = poptGetNextOpt(pc)) != -1) {
00103         switch (opt) {
00104           case 'e':
00105             arg = poptGetOptArg(pc);
00106             empties_mod = atoi(arg);
00107             if (strchr(arg, 's'))
00108                 empty_at_start = 1;
00109             if (strchr(arg, 'e'))
00110                 empty_at_end = 1;
00111             if (!explode_mod)
00112                 explode_mod = 1024;
00113             break;
00114           default:
00115             fprintf(stderr, "%s: %s\n",
00116                     poptBadOption(pc, POPT_BADOPTION_NOALIAS),
00117                     poptStrerror(opt));
00118             exit(1);
00119         }
00120     }
00121 
00122     if (explode_mod && !empties_mod)
00123         empties_mod = 1024;
00124 
00125     argv = (char**)poptGetArgs(pc);
00126     if (!argv || argv[1]) {
00127         fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
00128         exit(1);
00129     }
00130 
00131     if ((fp = fopen(*argv, "r")) == NULL) {
00132         fprintf(stderr, "Unable to open %s\n", *argv);
00133         exit(1);
00134     }
00135 
00136     line = 0;
00137     while (fgets(buf, sizeof buf, fp)) {
00138         line++;
00139         if (*buf == '#' || *buf == '\n')
00140             continue;
00141         for (s = buf, i = 0; i <= 1; i++) {
00142             if (*s == '1')
00143                 flag[i] = 1;
00144             else if (*s == '0')
00145                 flag[i] = 0;
00146             else
00147                 flag[i] = -1;
00148             if (*++s != ' ' && *s != '\t')
00149                 flag[i] = -1;
00150             if (flag[i] < 0) {
00151                 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
00152                         line, *argv, buf);
00153                 exit(1);
00154             }
00155             while (*++s == ' ' || *s == '\t') {}
00156         }
00157         for (i = 0; i <= 1; i++) {
00158             if (*s == '\'' || *s == '"' || *s == '`') {
00159                 char quote = *s++;
00160                 string[i] = s;
00161                 while (*s && *s != quote) s++;
00162                 if (!*s) {
00163                     fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
00164                             line, *argv, buf);
00165                     exit(1);
00166                 }
00167                 end[i] = s;
00168             }
00169             else {
00170                 if (!*s || *s == '\n') {
00171                     fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
00172                             line, *argv, buf);
00173                     exit(1);
00174                 }
00175                 string[i] = s;
00176                 while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
00177                 end[i] = s;
00178             }
00179             while (*++s == ' ' || *s == '\t') {}
00180         }
00181         *end[0] = *end[1] = '\0';
00182         run_test(line, flag[0], flag[1], string[0], string[1]);
00183     }
00184 
00185     if (!wildmatch_errors)
00186         fputs("No", stdout);
00187     else
00188         printf("%d", wildmatch_errors);
00189     printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
00190 
00191 #ifdef COMPARE_WITH_FNMATCH
00192     if (!fnmatch_errors)
00193         fputs("No", stdout);
00194     else
00195         printf("%d", fnmatch_errors);
00196     printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
00197 
00198 #endif
00199 
00200     return 0;
00201 }

rsyncに対してSat Dec 5 19:45:42 2009に生成されました。  doxygen 1.4.7