From c9916d64b1cf996a4cdb9f8887972038b492426d Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 8 Aug 2010 18:34:38 -0400 Subject: [PATCH] Improve regex test coverage --- glib/tests/regex.c | 559 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 558 insertions(+), 1 deletion(-) diff --git a/glib/tests/regex.c b/glib/tests/regex.c index 1b843b88d..bd46a5e53 100644 --- a/glib/tests/regex.c +++ b/glib/tests/regex.c @@ -1468,8 +1468,8 @@ test_class (void) { GRegex *regex; GError *error; - gboolean res; GMatchInfo *match; + gboolean res; gchar *str; error = NULL; @@ -1506,6 +1506,558 @@ test_class (void) g_match_info_free (match); g_regex_unref (regex); } + +/* examples for lookahead assertions taken from pcrepattern(3) */ +static void +test_lookahead (void) +{ + GRegex *regex; + GError *error; + GMatchInfo *match; + gboolean res; + gchar *str; + gint start, end; + + error = NULL; + regex = g_regex_new ("\\w+(?=;)", G_REGEX_OPTIMIZE, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "word1 word2: word3;", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_assert_cmpint (g_match_info_get_match_count (match), ==, 1); + str = g_match_info_fetch (match, 0); + g_assert_cmpstr (str, ==, "word3"); + g_free (str); + g_match_info_free (match); + g_regex_unref (regex); + + error = NULL; + regex = g_regex_new ("foo(?!bar)", G_REGEX_OPTIMIZE, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "foobar foobaz", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_assert_cmpint (g_match_info_get_match_count (match), ==, 1); + res = g_match_info_fetch_pos (match, 0, &start, &end); + g_assert (res); + g_assert_cmpint (start, ==, 7); + g_assert_cmpint (end, ==, 10); + g_match_info_free (match); + g_regex_unref (regex); + + error = NULL; + regex = g_regex_new ("(?!bar)foo", G_REGEX_OPTIMIZE, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "foobar foobaz", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_assert_cmpint (g_match_info_get_match_count (match), ==, 1); + res = g_match_info_fetch_pos (match, 0, &start, &end); + g_assert (res); + g_assert_cmpint (start, ==, 0); + g_assert_cmpint (end, ==, 3); + res = g_match_info_next (match, &error); + g_assert (res); + g_assert_no_error (error); + res = g_match_info_fetch_pos (match, 0, &start, &end); + g_assert (res); + g_assert_cmpint (start, ==, 7); + g_assert_cmpint (end, ==, 10); + g_match_info_free (match); + g_regex_unref (regex); +} + +/* examples for lookbehind assertions taken from pcrepattern(3) */ +static void +test_lookbehind (void) +{ + GRegex *regex; + GError *error; + GMatchInfo *match; + gboolean res; + gint start, end; + + error = NULL; + regex = g_regex_new ("(?Mon|Fri|Sun)(?:day)?|(?Tue)(?:sday)?|(?Wed)(?:nesday)?|(?Thu)(?:rsday)?|(?Sat)(?:urday)?", G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "Mon Tuesday Wed Saturday", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + str = g_match_info_fetch_named (match, "DN"); + g_assert_cmpstr (str, ==, "Mon"); + g_free (str); + res = g_match_info_next (match, &error); + g_assert (res); + str = g_match_info_fetch_named (match, "DN"); + g_assert_cmpstr (str, ==, "Tue"); + g_free (str); + res = g_match_info_next (match, &error); + g_assert (res); + str = g_match_info_fetch_named (match, "DN"); + g_assert_cmpstr (str, ==, "Wed"); + g_free (str); + res = g_match_info_next (match, &error); + g_assert (res); + str = g_match_info_fetch_named (match, "DN"); + g_assert_cmpstr (str, ==, "Sat"); + g_free (str); + g_match_info_free (match); + g_regex_unref (regex); + + regex = g_regex_new ("^(a|b\\1)+$", G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "aaaaaaaaaaaaaaaa", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "ababbaa", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); +} + +/* examples for conditions taken from pcrepattern(3) */ +static void +test_condition (void) +{ + GRegex *regex; + GError *error; + GMatchInfo *match; + gboolean res; + + error = NULL; + regex = g_regex_new ("^(a+)(\\()?[^()]+(?(-1)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "a(zzzzzz)b", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); + + error = NULL; + regex = g_regex_new ("^(a+)(?\\()?[^()]+(?()\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "a(zzzzzz)b", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); + + regex = g_regex_new ("^(a+)(?(+1)\\[|\\<)?[^()]+(\\])?(b+)$", G_REGEX_OPTIMIZE, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "a[zzzzzz]b", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "aaa 2[0-4]\\d | 25[0-5] | 1\\d\\d | [1-9]?\\d) )" + "\\b (?&byte) (\\.(?&byte)){3} \\b", + G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "128.0.0.1", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "192.168.1.1", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "209.132.180.167", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); + + regex = g_regex_new ("^(?(?=[^a-z]*[a-z])" + "\\d{2}-[a-z]{3}-\\d{2} | \\d{2}-\\d{2}-\\d{2} )$", + G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "01-abc-24", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "01-23-45", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "01-uv-45", 0, &match); + g_assert (!res); + g_assert (!g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "01-234-45", 0, &match); + g_assert (!res); + g_assert (!g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); +} + +/* examples for recursion taken from pcrepattern(3) */ +static void +test_recursion (void) +{ + GRegex *regex; + GError *error; + GMatchInfo *match; + gboolean res; + gint start; + + error = NULL; + regex = g_regex_new ("\\( ( [^()]++ | (?R) )* \\)", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "(middle)", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "(((xxx(((", 0, &match); + g_assert (!res); + g_assert (!g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); + + regex = g_regex_new ("^( \\( ( [^()]++ | (?1) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "(((xxx((()", 0, &match); + g_assert (!res); + g_assert (!g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); + + regex = g_regex_new ("^(? \\( ( [^()]++ | (?&pn) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error); + g_assert (regex); + g_assert_no_error (error); + g_regex_match (regex, "(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()", 0, &match); + g_assert (!res); + g_assert (!g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); + + regex = g_regex_new ("< (?: (?(R) \\d++ | [^<>]*+) | (?R)) * >", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, ">>>", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + res = g_match_info_fetch_pos (match, 0, &start, NULL); + g_assert (res); + g_assert_cmpint (start, ==, 0); + g_match_info_free (match); + res = g_regex_match (regex, ">>>", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + res = g_match_info_fetch_pos (match, 0, &start, NULL); + g_assert (res); + g_assert_cmpint (start, >, 0); + g_match_info_free (match); + g_regex_unref (regex); + + regex = g_regex_new ("^((.)(?1)\\2|.)$", G_REGEX_OPTIMIZE, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "abcdcba", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "abcddcba", 0, &match); + g_assert (!res); + g_assert (!g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); + + regex = g_regex_new ("^(?:((.)(?1)\\2|)|((.)(?3)\\4|.))$", G_REGEX_OPTIMIZE, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "abcdcba", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "abcddcba", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); + + regex = g_regex_new ("^\\W*+(?:((.)\\W*+(?1)\\W*+\\2|)|((.)\\W*+(?3)\\W*+\\4|\\W*+.\\W*+))\\W*+$", G_REGEX_OPTIMIZE|G_REGEX_CASELESS, 0, &error); + g_assert (regex); + g_assert_no_error (error); + res = g_regex_match (regex, "abcdcba", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "A man, a plan, a canal: Panama!", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + res = g_regex_match (regex, "Oozy rat in a sanitary zoo", 0, &match); + g_assert (res); + g_assert (g_match_info_matches (match)); + g_match_info_free (match); + g_regex_unref (regex); +} + int main (int argc, char *argv[]) { @@ -1519,6 +2071,11 @@ main (int argc, char *argv[]) g_test_add_func ("/regex/compile", test_compile); g_test_add_func ("/regex/properties", test_properties); g_test_add_func ("/regex/class", test_class); + g_test_add_func ("/regex/lookahead", test_lookahead); + g_test_add_func ("/regex/lookbehind", test_lookbehind); + g_test_add_func ("/regex/subpattern", test_subpattern); + g_test_add_func ("/regex/condition", test_condition); + g_test_add_func ("/regex/recursion", test_recursion); /* TEST_NEW(pattern, compile_opts, match_opts) */ TEST_NEW("", 0, 0);