1#include "tests/test_internal.h"
2
3TEST_GROUP_DECLARE(string);
4
5TEST_DECLARE_UNIT(string, strstr_patterns) {
6 /* Giving an empty needle returns start of haystack */
7 const char *h1 = "abcdef";
8 TEST_ASSERT_PTR_EQ(strstr(h1, ""), h1);
9
10 TEST_ASSERT_NULL(strstr("short", "longer_needle"));
11
12 TEST_ASSERT_NONNULL(strstr("hello", "hello"));
13
14 /* KMP prefix backtracking */
15 const char *h2 = "aabaabaabaax";
16 const char *n2 = "aabaax";
17 char *match2 = strstr(haystack: h2, needle: n2);
18 TEST_ASSERT_NONNULL(match2);
19 TEST_ASSERT_PTR_EQ(match2, h2 + 6);
20
21 /* trailing */
22 const char *h3 = "aaaaab";
23 const char *n3 = "aaab";
24 char *match3 = strstr(haystack: h3, needle: n3);
25 TEST_ASSERT_NONNULL(match3);
26 TEST_ASSERT_PTR_EQ(match3, h3 + 2);
27
28 TEST_ASSERT_NULL(strstr("ababababa", "ababc"));
29
30 return TEST_SUCCESS;
31}
32