]> git.proxmox.com Git - libgit2.git/blame - tests/util/memmem.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / util / memmem.c
CommitLineData
6c7cee42
RD
1#include "clar_libgit2.h"
2
3static 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
10static 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
ad5611d8 17void test_memmem__found(void)
6c7cee42
RD
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
ad5611d8 29void test_memmem__absent(void)
6c7cee42
RD
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
ad5611d8 39void test_memmem__edgecases(void)
6c7cee42
RD
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}