123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- /*
- * Testing various regex-patterns
- */
- #include <stdio.h>
- #include <string.h>
- #include "re.h"
- #define OK ((char*) 1)
- #define NOK ((char*) 0)
- char* test_vector[][3] =
- {
- { OK, "\\d", "5" },
- { OK, "\\w+", "hej" },
- { OK, "\\s", "\t \n" },
- { NOK, "\\S", "\t \n" },
- { OK, "[\\s]", "\t \n" },
- { NOK, "[\\S]", "\t \n" },
- { NOK, "\\D", "5" },
- { NOK, "\\W+", "hej" },
- { OK, "[0-9]+", "12345" },
- { OK, "\\D", "hej" },
- { NOK, "\\d", "hej" },
- { OK, "[^\\w]", "\\" },
- { OK, "[\\W]", "\\" },
- { NOK, "[\\w]", "\\" },
- { OK, "[^\\d]", "d" },
- { NOK, "[\\d]", "d" },
- { NOK, "[^\\D]", "d" },
- { OK, "[\\D]", "d" },
- { OK, "^.*\\\\.*$", "c:\\Tools" },
- { OK, "^[\\+-]*[\\d]+$", "+27" },
- { OK, "[abc]", "1c2" },
- { NOK, "[abc]", "1C2" },
- { OK, "[1-5]+", "0123456789" },
- { OK, "[.2]", "1C2" },
- { OK, "a*$", "Xaa" },
- { OK, "a*$", "Xaa" },
- { OK, "[a-h]+", "abcdefghxxx" },
- { NOK, "[a-h]+", "ABCDEFGH" },
- { OK, "[A-H]+", "ABCDEFGH" },
- { NOK, "[A-H]+", "abcdefgh" },
- { OK, "[^\\s]+", "abc def" },
- { OK, "[^fc]+", "abc def" },
- { OK, "[^d\\sf]+", "abc def" },
- { OK, "\n", "abc\ndef" },
- { OK, "b.\\s*\n", "aa\r\nbb\r\ncc\r\n\r\n" },
- { OK, ".*c", "abcabc" },
- { OK, ".+c", "abcabc" },
- { OK, "[b-z].*", "ab" },
- { OK, "b[k-z]*", "ab" },
- { NOK, "[0-9]", " - " },
- { OK, "[^0-9]", " - " },
- { OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello world !" },
- { OK, "[Hh]ello [Ww]orld\\s*[!]?", "hello world !" },
- { OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello World !" },
- { OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello world! " },
- { OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello world !" },
- { OK, "[Hh]ello [Ww]orld\\s*[!]?", "hello World !" },
- /*
- { OK, "[^\\w][^-1-4]", ")T" },
- { OK, "[^\\w][^-1-4]", ")^" },
- { OK, "[^\\w][^-1-4]", "*)" },
- { OK, "[^\\w][^-1-4]", "!." },
- { OK, "[^\\w][^-1-4]", " x" },
- { OK, "[^\\w][^-1-4]", "$b" },
- */
- };
- void re_print(re_t);
- int main()
- {
- char* text;
- char* pattern;
- int should_fail;
- size_t ntests = sizeof(test_vector) / sizeof(*test_vector);
- size_t nfailed = 0;
- size_t i;
- for (i = 0; i < ntests; ++i)
- {
- pattern = test_vector[i][1];
- text = test_vector[i][2];
- should_fail = (test_vector[i][0] == NOK);
- int m = re_match(pattern, text);
- if (should_fail)
- {
- if (m != (-1))
- {
- printf("\n");
- re_print(re_compile(pattern));
- fprintf(stderr, "[%lu/%lu]: pattern '%s' matched '%s' unexpectedly. \n", (i+1), ntests, pattern, text);
- nfailed += 1;
- }
- }
- else
- {
- if (m == (-1))
- {
- printf("\n");
- re_print(re_compile(pattern));
- fprintf(stderr, "[%lu/%lu]: pattern '%s' didn't match '%s' as expected. \n", (i+1), ntests, pattern, text);
- nfailed += 1;
- }
- }
- }
- // printf("\n");
- printf("%lu/%lu tests succeeded.\n", ntests - nfailed, ntests);
- printf("\n");
- printf("\n");
- printf("\n");
- return 0;
- }
|