fnmatch.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <fnmatch.h>
  2. #include <stdio.h>
  3. #include "test_helpers.h"
  4. void test(char* pattern, char* input, int flags) {
  5. if (!fnmatch(pattern, input, flags)) {
  6. printf("\"%s\" matches \"%s\"\n", pattern, input);
  7. } else {
  8. printf("\"%s\" doesn't match \"%s\"\n", pattern, input);
  9. }
  10. }
  11. int main(void) {
  12. puts("Should succeed:");
  13. test("*World", "Hello World", 0);
  14. test("*World", "World", 0);
  15. test("Hello*", "Hello World", 0);
  16. test("H[ae]llo?World", "Hallo+World", 0);
  17. test("H[ae]llo?World", "Hello_World", 0);
  18. test("[0-9][!a]", "1b", 0);
  19. test("/a/*/d", "/a/b/c/d", 0);
  20. test("/a/*/d", "/a/bc/d", FNM_PATHNAME);
  21. test("*hello", ".hello", 0);
  22. test("/*hello", "/.hello", FNM_PERIOD);
  23. test("[a!][a!]", "!a", 0);
  24. test("[\\]]", "]", 0);
  25. test("[\\\\]", "\\", 0);
  26. test("hello[/+]world", "hello/world", 0);
  27. test("hello world", "HELLO WORLD", FNM_CASEFOLD);
  28. puts("");
  29. puts("Should fail:");
  30. test("*World", "Hello Potato", 0);
  31. test("*World", "Potato", 0);
  32. test("H[ae]llo?World", "Hillo+World", 0);
  33. test("H[ae]llo?World", "Hello__World", 0);
  34. test("[0-9][!a]", "ab", 0);
  35. test("[0-9][!a]", "2a", 0);
  36. test("/a/*/d", "/a/b/c/d", FNM_PATHNAME);
  37. test("/a/*/d", "/a/bc/d/", FNM_PATHNAME);
  38. test("*hello", ".hello", FNM_PERIOD);
  39. test("/*hello", "/.hello", FNM_PERIOD | FNM_PATHNAME);
  40. test("[a!][a!]", "ab", 0);
  41. test("hello[/+]world", "hello/world", FNM_PATHNAME);
  42. test("hello world", "HELLO WORLD", 0);
  43. }