wildtest.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Test suite for the wildmatch code.
  3. *
  4. * Copyright (C) 2003-2008 Wayne Davison
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, visit the http://fsf.org website.
  18. */
  19. /*#define COMPARE_WITH_FNMATCH*/
  20. #define WILD_TEST_ITERATIONS
  21. #include "lib/wildmatch.c"
  22. #include <popt.h>
  23. #ifdef COMPARE_WITH_FNMATCH
  24. #include <fnmatch.h>
  25. int fnmatch_errors = 0;
  26. #endif
  27. int wildmatch_errors = 0;
  28. typedef char bool;
  29. int output_iterations = 0;
  30. int explode_mod = 0;
  31. int empties_mod = 0;
  32. int empty_at_start = 0;
  33. int empty_at_end = 0;
  34. static struct poptOption long_options[] = {
  35. /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
  36. {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
  37. {"empties", 'e', POPT_ARG_STRING, 0, 'e', 0, 0},
  38. {"explode", 'x', POPT_ARG_INT, &explode_mod, 0, 0, 0},
  39. {0,0,0,0, 0, 0, 0}
  40. };
  41. /* match just at the start of string (anchored tests) */
  42. static void
  43. run_test(int line, bool matches, bool same_as_fnmatch,
  44. const char *text, const char *pattern)
  45. {
  46. bool matched;
  47. #ifdef COMPARE_WITH_FNMATCH
  48. bool fn_matched;
  49. int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
  50. #else
  51. same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
  52. #endif
  53. if (explode_mod) {
  54. char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
  55. int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
  56. if (empty_at_start)
  57. texts[ndx++] = "";
  58. /* An empty string must turn into at least one empty array item. */
  59. while (1) {
  60. texts[ndx] = buf + ndx * (explode_mod + 1);
  61. strlcpy(texts[ndx++], text + pos, explode_mod + 1);
  62. if (pos + explode_mod >= len)
  63. break;
  64. pos += explode_mod;
  65. if (!(++cnt % empties_mod))
  66. texts[ndx++] = "";
  67. }
  68. if (empty_at_end)
  69. texts[ndx++] = "";
  70. texts[ndx] = NULL;
  71. matched = wildmatch_array(pattern, (const char**)texts, 0);
  72. } else
  73. matched = wildmatch(pattern, text);
  74. #ifdef COMPARE_WITH_FNMATCH
  75. fn_matched = !fnmatch(pattern, text, flags);
  76. #endif
  77. if (matched != matches) {
  78. printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n",
  79. line, text, pattern, matches? "a" : "NO");
  80. wildmatch_errors++;
  81. }
  82. #ifdef COMPARE_WITH_FNMATCH
  83. if (fn_matched != (matches ^ !same_as_fnmatch)) {
  84. printf("fnmatch disagreement on line %d:\n %s\n %s\n expected %s match\n",
  85. line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
  86. fnmatch_errors++;
  87. }
  88. #endif
  89. if (output_iterations) {
  90. printf("%d: \"%s\" iterations = %d\n", line, pattern,
  91. wildmatch_iteration_count);
  92. }
  93. }
  94. int
  95. main(int argc, char **argv)
  96. {
  97. char buf[2048], *s, *string[2], *end[2];
  98. const char *arg;
  99. FILE *fp;
  100. int opt, line, i, flag[2];
  101. poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
  102. long_options, 0);
  103. while ((opt = poptGetNextOpt(pc)) != -1) {
  104. switch (opt) {
  105. case 'e':
  106. arg = poptGetOptArg(pc);
  107. empties_mod = atoi(arg);
  108. if (strchr(arg, 's'))
  109. empty_at_start = 1;
  110. if (strchr(arg, 'e'))
  111. empty_at_end = 1;
  112. if (!explode_mod)
  113. explode_mod = 1024;
  114. break;
  115. default:
  116. fprintf(stderr, "%s: %s\n",
  117. poptBadOption(pc, POPT_BADOPTION_NOALIAS),
  118. poptStrerror(opt));
  119. exit(1);
  120. }
  121. }
  122. if (explode_mod && !empties_mod)
  123. empties_mod = 1024;
  124. argv = (char**)poptGetArgs(pc);
  125. if (!argv || argv[1]) {
  126. fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
  127. exit(1);
  128. }
  129. if ((fp = fopen(*argv, "r")) == NULL) {
  130. fprintf(stderr, "Unable to open %s\n", *argv);
  131. exit(1);
  132. }
  133. line = 0;
  134. while (fgets(buf, sizeof buf, fp)) {
  135. line++;
  136. if (*buf == '#' || *buf == '\n')
  137. continue;
  138. for (s = buf, i = 0; i <= 1; i++) {
  139. if (*s == '1')
  140. flag[i] = 1;
  141. else if (*s == '0')
  142. flag[i] = 0;
  143. else
  144. flag[i] = -1;
  145. if (*++s != ' ' && *s != '\t')
  146. flag[i] = -1;
  147. if (flag[i] < 0) {
  148. fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
  149. line, *argv, buf);
  150. exit(1);
  151. }
  152. while (*++s == ' ' || *s == '\t') {}
  153. }
  154. for (i = 0; i <= 1; i++) {
  155. if (*s == '\'' || *s == '"' || *s == '`') {
  156. char quote = *s++;
  157. string[i] = s;
  158. while (*s && *s != quote) s++;
  159. if (!*s) {
  160. fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
  161. line, *argv, buf);
  162. exit(1);
  163. }
  164. end[i] = s;
  165. }
  166. else {
  167. if (!*s || *s == '\n') {
  168. fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
  169. line, *argv, buf);
  170. exit(1);
  171. }
  172. string[i] = s;
  173. while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
  174. end[i] = s;
  175. }
  176. while (*++s == ' ' || *s == '\t') {}
  177. }
  178. *end[0] = *end[1] = '\0';
  179. run_test(line, flag[0], flag[1], string[0], string[1]);
  180. }
  181. if (!wildmatch_errors)
  182. fputs("No", stdout);
  183. else
  184. printf("%d", wildmatch_errors);
  185. printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
  186. #ifdef COMPARE_WITH_FNMATCH
  187. if (!fnmatch_errors)
  188. fputs("No", stdout);
  189. else
  190. printf("%d", fnmatch_errors);
  191. printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
  192. #endif
  193. return 0;
  194. }