]> git.proxmox.com Git - libgit2.git/blob - tests/core/memmem.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / memmem.c
1 #include "clar_libgit2.h"
2
3 static void assert_found(const char *haystack, const char *needle, size_t expected_pos)
4 {
5 cl_assert_equal_p(git__memmem(haystack, haystack ? strlen(haystack) : 0,
6 needle, needle ? strlen(needle) : 0),
7 haystack + expected_pos);
8 }
9
10 static void assert_absent(const char *haystack, const char *needle)
11 {
12 cl_assert_equal_p(git__memmem(haystack, haystack ? strlen(haystack) : 0,
13 needle, needle ? strlen(needle) : 0),
14 NULL);
15 }
16
17 void test_core_memmem__found(void)
18 {
19 assert_found("a", "a", 0);
20 assert_found("ab", "a", 0);
21 assert_found("ba", "a", 1);
22 assert_found("aa", "a", 0);
23 assert_found("aab", "aa", 0);
24 assert_found("baa", "aa", 1);
25 assert_found("dabc", "abc", 1);
26 assert_found("abababc", "abc", 4);
27 }
28
29 void test_core_memmem__absent(void)
30 {
31 assert_absent("a", "b");
32 assert_absent("a", "aa");
33 assert_absent("ba", "ab");
34 assert_absent("ba", "ab");
35 assert_absent("abc", "abcd");
36 assert_absent("abcabcabc", "bcac");
37 }
38
39 void test_core_memmem__edgecases(void)
40 {
41 assert_absent(NULL, NULL);
42 assert_absent("a", NULL);
43 assert_absent(NULL, "a");
44 assert_absent("", "a");
45 assert_absent("a", "");
46 }