test1.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Testing various regex-patterns
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "re.h"
  7. #define OK ((char*) 1)
  8. #define NOK ((char*) 0)
  9. char* test_vector[][3] =
  10. {
  11. { OK, "\\d", "5" },
  12. { OK, "\\w+", "hej" },
  13. { OK, "\\s", "\t \n" },
  14. { NOK, "\\S", "\t \n" },
  15. { OK, "[\\s]", "\t \n" },
  16. { NOK, "[\\S]", "\t \n" },
  17. { NOK, "\\D", "5" },
  18. { NOK, "\\W+", "hej" },
  19. { OK, "[0-9]+", "12345" },
  20. { OK, "\\D", "hej" },
  21. { NOK, "\\d", "hej" },
  22. { OK, "[^\\w]", "\\" },
  23. { OK, "[\\W]", "\\" },
  24. { NOK, "[\\w]", "\\" },
  25. { OK, "[^\\d]", "d" },
  26. { NOK, "[\\d]", "d" },
  27. { NOK, "[^\\D]", "d" },
  28. { OK, "[\\D]", "d" },
  29. { OK, "^.*\\\\.*$", "c:\\Tools" },
  30. { OK, "^[\\+-]*[\\d]+$", "+27" },
  31. { OK, "[abc]", "1c2" },
  32. { NOK, "[abc]", "1C2" },
  33. { OK, "[1-5]+", "0123456789" },
  34. { OK, "[.2]", "1C2" },
  35. { OK, "a*$", "Xaa" },
  36. { OK, "a*$", "Xaa" },
  37. { OK, "[a-h]+", "abcdefghxxx" },
  38. { NOK, "[a-h]+", "ABCDEFGH" },
  39. { OK, "[A-H]+", "ABCDEFGH" },
  40. { NOK, "[A-H]+", "abcdefgh" },
  41. { OK, "[^\\s]+", "abc def" },
  42. { OK, "[^fc]+", "abc def" },
  43. { OK, "[^d\\sf]+", "abc def" },
  44. { OK, "\n", "abc\ndef" },
  45. { OK, "b.\\s*\n", "aa\r\nbb\r\ncc\r\n\r\n" },
  46. { OK, ".*c", "abcabc" },
  47. { OK, ".+c", "abcabc" },
  48. { OK, "[b-z].*", "ab" },
  49. { OK, "b[k-z]*", "ab" },
  50. { NOK, "[0-9]", " - " },
  51. { OK, "[^0-9]", " - " },
  52. { OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello world !" },
  53. { OK, "[Hh]ello [Ww]orld\\s*[!]?", "hello world !" },
  54. { OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello World !" },
  55. { OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello world! " },
  56. { OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello world !" },
  57. { OK, "[Hh]ello [Ww]orld\\s*[!]?", "hello World !" },
  58. /*
  59. { OK, "[^\\w][^-1-4]", ")T" },
  60. { OK, "[^\\w][^-1-4]", ")^" },
  61. { OK, "[^\\w][^-1-4]", "*)" },
  62. { OK, "[^\\w][^-1-4]", "!." },
  63. { OK, "[^\\w][^-1-4]", " x" },
  64. { OK, "[^\\w][^-1-4]", "$b" },
  65. */
  66. };
  67. void re_print(re_t);
  68. int main()
  69. {
  70. char* text;
  71. char* pattern;
  72. int should_fail;
  73. size_t ntests = sizeof(test_vector) / sizeof(*test_vector);
  74. size_t nfailed = 0;
  75. size_t i;
  76. for (i = 0; i < ntests; ++i)
  77. {
  78. pattern = test_vector[i][1];
  79. text = test_vector[i][2];
  80. should_fail = (test_vector[i][0] == NOK);
  81. int m = re_match(pattern, text);
  82. if (should_fail)
  83. {
  84. if (m != (-1))
  85. {
  86. printf("\n");
  87. re_print(re_compile(pattern));
  88. fprintf(stderr, "[%lu/%lu]: pattern '%s' matched '%s' unexpectedly. \n", (i+1), ntests, pattern, text);
  89. nfailed += 1;
  90. }
  91. }
  92. else
  93. {
  94. if (m == (-1))
  95. {
  96. printf("\n");
  97. re_print(re_compile(pattern));
  98. fprintf(stderr, "[%lu/%lu]: pattern '%s' didn't match '%s' as expected. \n", (i+1), ntests, pattern, text);
  99. nfailed += 1;
  100. }
  101. }
  102. }
  103. // printf("\n");
  104. printf("%lu/%lu tests succeeded.\n", ntests - nfailed, ntests);
  105. printf("\n");
  106. printf("\n");
  107. printf("\n");
  108. return 0;
  109. }