1//===-- Unittests for memmem ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/string/memmem.h"
10#include "test/UnitTest/Test.h"
11
12#include "src/string/string_utils.h"
13
14namespace LIBC_NAMESPACE {
15
16TEST(LlvmLibcMemmemTest, EmptyHaystackEmptyNeedleReturnsHaystck) {
17 char *h = nullptr;
18 char *n = nullptr;
19 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: 0, needle: n, needle_len: 0);
20 ASSERT_EQ(static_cast<char *>(result), h);
21}
22
23TEST(LlvmLibcMemmemTest, EmptyHaystackNonEmptyNeedleReturnsNull) {
24 char *h = nullptr;
25 char n[] = {'a', 'b', 'c'};
26 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: 0, needle: n, needle_len: sizeof(n));
27 ASSERT_EQ(result, static_cast<void *>(nullptr));
28}
29
30TEST(LlvmLibcMemmemTest, EmptyNeedleReturnsHaystack) {
31 char h[] = {'a', 'b', 'c'};
32 char *n = nullptr;
33 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: 0);
34 ASSERT_EQ(static_cast<char *>(result), h + 0);
35}
36
37TEST(LlvmLibcMemmemTest, ExactMatchReturnsHaystack) {
38 char h[] = {'a', 'b', 'c'};
39 char n[] = {'a', 'b', 'c'};
40 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: sizeof(n));
41 ASSERT_EQ(static_cast<char *>(result), h + 0);
42}
43
44TEST(LlvmLibcMemmemTest, ReturnFirstMatchOfNeedle) {
45 char h[] = {'a', 'a', 'b', 'c'};
46 char n[] = {'a'};
47 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: sizeof(n));
48 ASSERT_EQ(static_cast<char *>(result), h + 0);
49}
50
51TEST(LlvmLibcMemmemTest, ReturnFirstExactMatchOfNeedle) {
52 {
53 char h[] = {'a', 'b', 'a', 'c', 'a', 'a'};
54 char n[] = {'a', 'a'};
55 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: sizeof(n));
56 ASSERT_EQ(static_cast<char *>(result), h + 4);
57 }
58 {
59 char h[] = {'a', 'a', 'b', 'a', 'b', 'a'};
60 char n[] = {'a', 'b', 'a'};
61 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: sizeof(n));
62 ASSERT_EQ(static_cast<char *>(result), h + 1);
63 }
64}
65
66TEST(LlvmLibcMemmemTest, NullTerminatorDoesNotInterruptMatch) {
67 char h[] = {'\0', 'a', 'b'};
68 char n[] = {'a', 'b'};
69 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: sizeof(n));
70 ASSERT_EQ(static_cast<char *>(result), h + 1);
71}
72
73TEST(LlvmLibcMemmemTest, ReturnNullIfNoExactMatch) {
74 {
75 char h[] = {'a'};
76 char n[] = {'a', 'a'};
77 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: sizeof(n));
78 ASSERT_EQ(result, static_cast<void *>(nullptr));
79 }
80 {
81 char h[] = {'a', 'A'};
82 char n[] = {'a', 'a'};
83 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: sizeof(n));
84 ASSERT_EQ(result, static_cast<void *>(nullptr));
85 }
86 {
87 char h[] = {'a'};
88 char n[] = {'a', '\0'};
89 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: sizeof(n));
90 ASSERT_EQ(result, static_cast<void *>(nullptr));
91 }
92 {
93 char h[] = {'\0'};
94 char n[] = {'\0', '\0'};
95 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: sizeof(n));
96 ASSERT_EQ(result, static_cast<void *>(nullptr));
97 }
98}
99
100TEST(LlvmLibcMemmemTest, ReturnMatchOfSpecifiedNeedleLength) {
101 {
102 char h[] = {'a', 'b', 'c'};
103 char n[] = {'x', 'y', 'z'};
104 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: 0);
105 ASSERT_EQ(static_cast<char *>(result), h + 0);
106 }
107 {
108 char h[] = {'a', 'b', 'c'};
109 char n[] = {'b', 'c', 'a'};
110 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: sizeof(h), needle: n, needle_len: 2);
111 ASSERT_EQ(static_cast<char *>(result), h + 1);
112 }
113}
114
115TEST(LlvmLibcMemmemTest, ReturnNullIfInadequateHaystackLength) {
116 {
117 char h[] = {'a', 'b', 'c'};
118 char n[] = {'c'};
119 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: 2, needle: n, needle_len: sizeof(n));
120 ASSERT_EQ(result, static_cast<void *>(nullptr));
121 }
122 {
123 char h[] = {'a', 'b', 'c'};
124 char n[] = {'a', 'b', 'c'};
125 void *result = LIBC_NAMESPACE::memmem(haystack: h, haystack_len: 2, needle: n, needle_len: sizeof(n));
126 ASSERT_EQ(result, static_cast<void *>(nullptr));
127 }
128}
129} // namespace LIBC_NAMESPACE
130

source code of libc/test/src/string/memmem_test.cpp