2013-09-23 22:41:58 +01:00
|
|
|
#include "ptest.h"
|
2013-09-19 20:57:40 +01:00
|
|
|
#include "../mpc.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2013-09-23 22:41:58 +01:00
|
|
|
#include <stdlib.h>
|
2013-09-19 20:57:40 +01:00
|
|
|
|
2014-10-16 15:38:27 +01:00
|
|
|
static int string_eq(const void* x, const void* y) { return (strcmp(x, y) == 0); }
|
|
|
|
static void string_print(const void* x) { printf("'%s'", (char*)x); }
|
2013-09-19 20:57:40 +01:00
|
|
|
|
2014-10-16 15:38:27 +01:00
|
|
|
int regex_test_pass(mpc_parser_t *p, const char* value, const char* match) {
|
2014-04-16 19:19:25 +01:00
|
|
|
return mpc_test_pass(p, value, match, string_eq, free, string_print);
|
|
|
|
}
|
|
|
|
|
2014-10-16 15:38:27 +01:00
|
|
|
int regex_test_fail(mpc_parser_t *p, const char* value, const char* match) {
|
2014-04-16 19:19:25 +01:00
|
|
|
return mpc_test_fail(p, value, match, string_eq, free, string_print);
|
|
|
|
}
|
|
|
|
|
2013-09-23 22:41:58 +01:00
|
|
|
void test_regex_basic(void) {
|
|
|
|
|
2014-04-16 19:19:25 +01:00
|
|
|
mpc_parser_t *re0, *re1, *re2, *re3, *re4, *re5;
|
2013-09-30 20:55:57 +01:00
|
|
|
|
|
|
|
re0 = mpc_re("abc|bcd");
|
|
|
|
re1 = mpc_re("abc|bcd|e");
|
|
|
|
re2 = mpc_re("ab()c(ab)*");
|
|
|
|
re3 = mpc_re("abc(abdd)?");
|
|
|
|
re4 = mpc_re("ab|c(abdd)?");
|
|
|
|
re5 = mpc_re("abc(ab|dd)+g$");
|
2013-09-19 20:57:40 +01:00
|
|
|
|
2014-04-16 19:19:25 +01:00
|
|
|
PT_ASSERT(regex_test_pass(re0, "abc", "abc"));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "bcd", "bcd"));
|
|
|
|
PT_ASSERT(regex_test_fail(re0, "bc", "bc"));
|
|
|
|
PT_ASSERT(regex_test_fail(re0, "ab", "ab"));
|
|
|
|
PT_ASSERT(regex_test_pass(re1, "e", "e"));
|
|
|
|
PT_ASSERT(regex_test_pass(re2, "abc", "abc"));
|
|
|
|
PT_ASSERT(regex_test_pass(re2, "abcabab", "abcabab"));
|
|
|
|
PT_ASSERT(regex_test_pass(re2, "abcababd", "abcabab"));
|
|
|
|
PT_ASSERT(regex_test_pass(re5, "abcddg", "abcddg"));
|
2013-09-23 22:41:58 +01:00
|
|
|
|
|
|
|
mpc_delete(re0);
|
|
|
|
mpc_delete(re1);
|
|
|
|
mpc_delete(re2);
|
|
|
|
mpc_delete(re3);
|
|
|
|
mpc_delete(re4);
|
|
|
|
mpc_delete(re5);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-16 19:19:25 +01:00
|
|
|
void test_regex_boundary(void) {
|
|
|
|
|
|
|
|
mpc_parser_t *re0, *re1, *re2;
|
|
|
|
|
|
|
|
re0 = mpc_re("\\bfoo\\b");
|
|
|
|
re1 = mpc_re("(w| )?\\bfoo\\b");
|
|
|
|
re2 = mpc_re("py\\B.*");
|
|
|
|
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "foo", "foo"));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "foo.", "foo"));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "foo)", "foo"));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "foo baz", "foo"));
|
|
|
|
|
|
|
|
PT_ASSERT(regex_test_fail(re0, "foobar", "foo"));
|
|
|
|
PT_ASSERT(regex_test_fail(re0, "foo3", "foo"));
|
|
|
|
|
|
|
|
PT_ASSERT(regex_test_pass(re1, "foo", "foo"));
|
|
|
|
PT_ASSERT(regex_test_pass(re1, " foo", " foo"));
|
|
|
|
PT_ASSERT(regex_test_fail(re1, "wfoo", "foo"));
|
|
|
|
|
|
|
|
PT_ASSERT(regex_test_pass(re2, "python", "python"));
|
|
|
|
PT_ASSERT(regex_test_pass(re2, "py3", "py3"));
|
|
|
|
PT_ASSERT(regex_test_pass(re2, "py2", "py2"));
|
|
|
|
PT_ASSERT(regex_test_fail(re2, "py", "py"));
|
|
|
|
PT_ASSERT(regex_test_fail(re2, "py.", "py."));
|
|
|
|
PT_ASSERT(regex_test_fail(re2, "py!", "py!"));
|
|
|
|
|
|
|
|
mpc_delete(re0);
|
|
|
|
mpc_delete(re1);
|
|
|
|
mpc_delete(re2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-23 22:41:58 +01:00
|
|
|
void test_regex_range(void) {
|
|
|
|
|
2014-01-10 10:03:25 +00:00
|
|
|
mpc_parser_t *re0, *re1, *re2, *re3;
|
2013-09-30 20:55:57 +01:00
|
|
|
|
|
|
|
re0 = mpc_re("abg[abcdef]");
|
|
|
|
re1 = mpc_re("y*[a-z]");
|
|
|
|
re2 = mpc_re("zz(p+)?[A-Z_0\\]123]*");
|
|
|
|
re3 = mpc_re("^[^56hy].*$");
|
2013-09-26 13:15:00 +01:00
|
|
|
|
|
|
|
/* TODO: Testing */
|
2013-09-19 20:57:40 +01:00
|
|
|
|
|
|
|
mpc_delete(re0);
|
|
|
|
mpc_delete(re1);
|
|
|
|
mpc_delete(re2);
|
|
|
|
mpc_delete(re3);
|
|
|
|
|
2013-09-23 22:41:58 +01:00
|
|
|
}
|
|
|
|
|
2014-04-16 19:19:25 +01:00
|
|
|
void test_regex_string(void) {
|
|
|
|
|
|
|
|
mpc_parser_t *re0 = mpc_re("\"(\\\\.|[^\"])*\"");
|
|
|
|
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "\"there\"", "\"there\""));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "\"hello\"", "\"hello\""));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "\"i am dan\"", "\"i am dan\""));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "\"i a\\\"m dan\"", "\"i a\\\"m dan\""));
|
|
|
|
|
|
|
|
mpc_delete(re0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_regex_lisp_comment(void) {
|
|
|
|
|
|
|
|
mpc_parser_t *re0 = mpc_re(";[^\\n\\r]*");
|
|
|
|
|
|
|
|
PT_ASSERT(regex_test_pass(re0, ";comment", ";comment"));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, ";i am the\nman", ";i am the"));
|
|
|
|
|
|
|
|
mpc_delete(re0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-13 17:35:21 -04:00
|
|
|
void test_regex_newline(void) {
|
|
|
|
|
|
|
|
mpc_parser_t *re0 = mpc_re("[a-z]*");
|
|
|
|
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "hi", "hi"));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "hi\nk", "hi"));
|
|
|
|
PT_ASSERT(regex_test_fail(re0, "hi\nk", "hi\nk"));
|
|
|
|
|
|
|
|
mpc_delete(re0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-14 17:20:11 -04:00
|
|
|
void test_regex_multiline(void) {
|
|
|
|
|
|
|
|
mpc_parser_t *re0 = mpc_re_mode("(^[a-z]*$)*", MPC_RE_MULTILINE);
|
|
|
|
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "hello\nhello", "hello\nhello"));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "hello\nhello\n", "hello\nhello\n"));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "\nblah\n\nblah\n", "\nblah\n\nblah\n"));
|
|
|
|
PT_ASSERT(regex_test_fail(re0, "45234", "45234"));
|
|
|
|
PT_ASSERT(regex_test_fail(re0, "\n45234", "\n45234"));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "\n45234", "\n"));
|
|
|
|
|
|
|
|
mpc_delete(re0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_regex_dotall(void) {
|
|
|
|
|
|
|
|
mpc_parser_t *re0 = mpc_re_mode("^.*$", MPC_RE_DEFAULT);
|
|
|
|
mpc_parser_t *re1 = mpc_re_mode("^.*$", MPC_RE_DOTALL);
|
|
|
|
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "hello", "hello"));
|
|
|
|
PT_ASSERT(regex_test_fail(re0, "hello\n", "hello"));
|
|
|
|
PT_ASSERT(regex_test_fail(re0, "he\nllo\n", "he"));
|
|
|
|
PT_ASSERT(regex_test_pass(re0, "34njaksdklmasd", "34njaksdklmasd"));
|
|
|
|
PT_ASSERT(regex_test_fail(re0, "34njaksd\nklmasd", "34njaksd"));
|
|
|
|
|
|
|
|
PT_ASSERT(regex_test_pass(re1, "hello", "hello"));
|
|
|
|
PT_ASSERT(regex_test_pass(re1, "hello\n", "hello\n"));
|
|
|
|
PT_ASSERT(regex_test_pass(re1, "he\nllo\n", "he\nllo\n"));
|
|
|
|
PT_ASSERT(regex_test_pass(re1, "34njaksdklmasd", "34njaksdklmasd"));
|
|
|
|
PT_ASSERT(regex_test_pass(re1, "34njaksd\nklmasd", "34njaksd\nklmasd"));
|
|
|
|
|
|
|
|
mpc_delete(re0);
|
|
|
|
mpc_delete(re1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-23 22:41:58 +01:00
|
|
|
void suite_regex(void) {
|
|
|
|
pt_add_test(test_regex_basic, "Test Regex Basic", "Suite Regex");
|
|
|
|
pt_add_test(test_regex_range, "Test Regex Range", "Suite Regex");
|
2014-04-16 19:19:25 +01:00
|
|
|
pt_add_test(test_regex_string, "Test Regex String", "Suite Regex");
|
|
|
|
pt_add_test(test_regex_lisp_comment, "Test Regex Lisp Comment", "Suite Regex");
|
|
|
|
pt_add_test(test_regex_boundary, "Test Regex Boundary", "Suite Regex");
|
2018-10-13 17:35:21 -04:00
|
|
|
pt_add_test(test_regex_newline, "Test Regex Newline", "Suite Regex");
|
2018-10-14 17:20:11 -04:00
|
|
|
pt_add_test(test_regex_multiline, "Test Regex Multiline", "Suite Regex");
|
|
|
|
pt_add_test(test_regex_dotall, "Test Regex Dotall", "Suite Regex");
|
2013-11-10 16:56:59 +00:00
|
|
|
}
|