]>
git.proxmox.com Git - libgit2.git/blob - tests/util/regexp.c
1 #include "clar_libgit2.h"
8 static const char *old_locales
[LC_ALL
];
11 static git_regexp regex
;
13 void test_regexp__initialize(void)
16 memset(&old_locales
, 0, sizeof(old_locales
));
20 void test_regexp__cleanup(void)
22 git_regexp_dispose(®ex
);
25 static void try_set_locale(int category
)
28 old_locales
[category
] = setlocale(category
, NULL
);
31 if (!setlocale(category
, "UTF-8") &&
32 !setlocale(category
, "c.utf8") &&
33 !setlocale(category
, "en_US.UTF-8"))
37 cl_fail("Expected locale to be switched to multibyte");
41 void test_regexp__compile_ignores_global_locale_ctype(void)
43 try_set_locale(LC_CTYPE
);
44 cl_git_pass(git_regexp_compile(®ex
, "[\xc0-\xff][\x80-\xbf]", 0));
47 void test_regexp__compile_ignores_global_locale_collate(void)
53 try_set_locale(LC_COLLATE
);
54 cl_git_pass(git_regexp_compile(®ex
, "[\xc0-\xff][\x80-\xbf]", 0));
57 void test_regexp__regex_matches_digits_with_locale(void)
65 try_set_locale(LC_COLLATE
);
66 try_set_locale(LC_CTYPE
);
68 cl_git_pass(git_regexp_compile(®ex
, "[[:digit:]]", 0));
71 for (c
= '0'; c
<= '9'; c
++) {
73 cl_git_pass(git_regexp_match(®ex
, str
));
77 void test_regexp__regex_matches_alphabet_with_locale(void)
85 try_set_locale(LC_COLLATE
);
86 try_set_locale(LC_CTYPE
);
88 cl_git_pass(git_regexp_compile(®ex
, "[[:alpha:]]", 0));
91 for (c
= 'a'; c
<= 'z'; c
++) {
93 cl_git_pass(git_regexp_match(®ex
, str
));
95 for (c
= 'A'; c
<= 'Z'; c
++) {
97 cl_git_pass(git_regexp_match(®ex
, str
));
101 void test_regexp__simple_search_matches(void)
103 cl_git_pass(git_regexp_compile(®ex
, "a", 0));
104 cl_git_pass(git_regexp_search(®ex
, "a", 0, NULL
));
107 void test_regexp__case_insensitive_search_matches(void)
109 cl_git_pass(git_regexp_compile(®ex
, "a", GIT_REGEXP_ICASE
));
110 cl_git_pass(git_regexp_search(®ex
, "A", 0, NULL
));
113 void test_regexp__nonmatching_search_returns_error(void)
115 cl_git_pass(git_regexp_compile(®ex
, "a", 0));
116 cl_git_fail(git_regexp_search(®ex
, "b", 0, NULL
));
119 void test_regexp__search_finds_complete_match(void)
121 git_regmatch matches
[1];
123 cl_git_pass(git_regexp_compile(®ex
, "abc", 0));
124 cl_git_pass(git_regexp_search(®ex
, "abc", 1, matches
));
125 cl_assert_equal_i(matches
[0].start
, 0);
126 cl_assert_equal_i(matches
[0].end
, 3);
129 void test_regexp__search_finds_correct_offsets(void)
131 git_regmatch matches
[3];
133 cl_git_pass(git_regexp_compile(®ex
, "(a*)(b*)", 0));
134 cl_git_pass(git_regexp_search(®ex
, "ab", 3, matches
));
135 cl_assert_equal_i(matches
[0].start
, 0);
136 cl_assert_equal_i(matches
[0].end
, 2);
137 cl_assert_equal_i(matches
[1].start
, 0);
138 cl_assert_equal_i(matches
[1].end
, 1);
139 cl_assert_equal_i(matches
[2].start
, 1);
140 cl_assert_equal_i(matches
[2].end
, 2);
143 void test_regexp__search_finds_empty_group(void)
145 git_regmatch matches
[3];
147 cl_git_pass(git_regexp_compile(®ex
, "(a*)(b*)c", 0));
148 cl_git_pass(git_regexp_search(®ex
, "ac", 3, matches
));
149 cl_assert_equal_i(matches
[0].start
, 0);
150 cl_assert_equal_i(matches
[0].end
, 2);
151 cl_assert_equal_i(matches
[1].start
, 0);
152 cl_assert_equal_i(matches
[1].end
, 1);
153 cl_assert_equal_i(matches
[2].start
, 1);
154 cl_assert_equal_i(matches
[2].end
, 1);
157 void test_regexp__search_fills_matches_with_first_matching_groups(void)
159 git_regmatch matches
[2];
161 cl_git_pass(git_regexp_compile(®ex
, "(a)(b)(c)", 0));
162 cl_git_pass(git_regexp_search(®ex
, "abc", 2, matches
));
163 cl_assert_equal_i(matches
[0].start
, 0);
164 cl_assert_equal_i(matches
[0].end
, 3);
165 cl_assert_equal_i(matches
[1].start
, 0);
166 cl_assert_equal_i(matches
[1].end
, 1);
169 void test_regexp__search_skips_nonmatching_group(void)
171 git_regmatch matches
[4];
173 cl_git_pass(git_regexp_compile(®ex
, "(a)(b)?(c)", 0));
174 cl_git_pass(git_regexp_search(®ex
, "ac", 4, matches
));
175 cl_assert_equal_i(matches
[0].start
, 0);
176 cl_assert_equal_i(matches
[0].end
, 2);
177 cl_assert_equal_i(matches
[1].start
, 0);
178 cl_assert_equal_i(matches
[1].end
, 1);
179 cl_assert_equal_i(matches
[2].start
, -1);
180 cl_assert_equal_i(matches
[2].end
, -1);
181 cl_assert_equal_i(matches
[3].start
, 1);
182 cl_assert_equal_i(matches
[3].end
, 2);
185 void test_regexp__search_initializes_trailing_nonmatching_groups(void)
187 git_regmatch matches
[3];
189 cl_git_pass(git_regexp_compile(®ex
, "(a)bc", 0));
190 cl_git_pass(git_regexp_search(®ex
, "abc", 3, matches
));
191 cl_assert_equal_i(matches
[0].start
, 0);
192 cl_assert_equal_i(matches
[0].end
, 3);
193 cl_assert_equal_i(matches
[1].start
, 0);
194 cl_assert_equal_i(matches
[1].end
, 1);
195 cl_assert_equal_i(matches
[2].start
, -1);
196 cl_assert_equal_i(matches
[2].end
, -1);