wildtest.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Test suite for the wildmatch code.
  3. *
  4. * Copyright (C) 2003-2009 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,
  44. #ifdef COMPARE_WITH_FNMATCH
  45. bool same_as_fnmatch,
  46. #endif
  47. const char *text, const char *pattern)
  48. {
  49. bool matched;
  50. #ifdef COMPARE_WITH_FNMATCH
  51. bool fn_matched;
  52. int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
  53. #endif
  54. if (explode_mod) {
  55. char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
  56. int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
  57. if (empty_at_start)
  58. texts[ndx++] = "";
  59. /* An empty string must turn into at least one empty array item. */
  60. while (1) {
  61. texts[ndx] = buf + ndx * (explode_mod + 1);
  62. strlcpy(texts[ndx++], text + pos, explode_mod + 1);
  63. if (pos + explode_mod >= len)
  64. break;
  65. pos += explode_mod;
  66. if (!(++cnt % empties_mod))
  67. texts[ndx++] = "";
  68. }
  69. if (empty_at_end)
  70. texts[ndx++] = "";
  71. texts[ndx] = NULL;
  72. matched = wildmatch_array(pattern, (const char**)texts, 0);
  73. } else
  74. matched = wildmatch(pattern, text);
  75. #ifdef COMPARE_WITH_FNMATCH
  76. fn_matched = !fnmatch(pattern, text, flags);
  77. #endif
  78. if (matched != matches) {
  79. printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n",
  80. line, text, pattern, matches? "a" : "NO");
  81. wildmatch_errors++;
  82. }
  83. #ifdef COMPARE_WITH_FNMATCH
  84. if (fn_matched != (matches ^ !same_as_fnmatch)) {
  85. printf("fnmatch disagreement on line %d:\n %s\n %s\n expected %s match\n",
  86. line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
  87. fnmatch_errors++;
  88. }
  89. #endif
  90. if (output_iterations) {
  91. printf("%d: \"%s\" iterations = %d\n", line, pattern,
  92. wildmatch_iteration_count);
  93. }
  94. }
  95. int
  96. main(int argc, char **argv)
  97. {
  98. char buf[2048], *s, *string[2], *end[2];
  99. const char *arg;
  100. FILE *fp;
  101. int opt, line, i, flag[2];
  102. poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
  103. long_options, 0);
  104. while ((opt = poptGetNextOpt(pc)) != -1) {
  105. switch (opt) {
  106. case 'e':
  107. arg = poptGetOptArg(pc);
  108. empties_mod = atoi(arg);
  109. if (strchr(arg, 's'))
  110. empty_at_start = 1;
  111. if (strchr(arg, 'e'))
  112. empty_at_end = 1;
  113. if (!explode_mod)
  114. explode_mod = 1024;
  115. break;
  116. default:
  117. fprintf(stderr, "%s: %s\n",
  118. poptBadOption(pc, POPT_BADOPTION_NOALIAS),
  119. poptStrerror(opt));
  120. exit(1);
  121. }
  122. }
  123. if (explode_mod && !empties_mod)
  124. empties_mod = 1024;
  125. argv = (char**)poptGetArgs(pc);
  126. if (!argv || argv[1]) {
  127. fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
  128. exit(1);
  129. }
  130. if ((fp = fopen(*argv, "r")) == NULL) {
  131. fprintf(stderr, "Unable to open %s\n", *argv);
  132. exit(1);
  133. }
  134. line = 0;
  135. while (fgets(buf, sizeof buf, fp)) {
  136. line++;
  137. if (*buf == '#' || *buf == '\n')
  138. continue;
  139. for (s = buf, i = 0; i <= 1; i++) {
  140. if (*s == '1')
  141. flag[i] = 1;
  142. else if (*s == '0')
  143. flag[i] = 0;
  144. else
  145. flag[i] = -1;
  146. if (*++s != ' ' && *s != '\t')
  147. flag[i] = -1;
  148. if (flag[i] < 0) {
  149. fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
  150. line, *argv, buf);
  151. exit(1);
  152. }
  153. while (*++s == ' ' || *s == '\t') {}
  154. }
  155. for (i = 0; i <= 1; i++) {
  156. if (*s == '\'' || *s == '"' || *s == '`') {
  157. char quote = *s++;
  158. string[i] = s;
  159. while (*s && *s != quote) s++;
  160. if (!*s) {
  161. fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
  162. line, *argv, buf);
  163. exit(1);
  164. }
  165. end[i] = s;
  166. }
  167. else {
  168. if (!*s || *s == '\n') {
  169. fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
  170. line, *argv, buf);
  171. exit(1);
  172. }
  173. string[i] = s;
  174. while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
  175. end[i] = s;
  176. }
  177. while (*++s == ' ' || *s == '\t') {}
  178. }
  179. *end[0] = *end[1] = '\0';
  180. run_test(line, flag[0],
  181. #ifdef COMPARE_WITH_FNMATCH
  182. flag[1],
  183. #endif
  184. string[0], string[1]);
  185. }
  186. if (!wildmatch_errors)
  187. fputs("No", stdout);
  188. else
  189. printf("%d", wildmatch_errors);
  190. printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
  191. #ifdef COMPARE_WITH_FNMATCH
  192. if (!fnmatch_errors)
  193. fputs("No", stdout);
  194. else
  195. printf("%d", fnmatch_errors);
  196. printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
  197. #endif
  198. return 0;
  199. }