1 //===-- Unittests for memmem ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/macros/config.h"
10 #include "src/string/memmem.h"
11 #include "test/UnitTest/Test.h"
13 #include "src/string/string_utils.h"
15 namespace LIBC_NAMESPACE_DECL
{
17 TEST(LlvmLibcMemmemTest
, EmptyHaystackEmptyNeedleReturnsHaystck
) {
20 void *result
= LIBC_NAMESPACE::memmem(h
, 0, n
, 0);
21 ASSERT_EQ(static_cast<char *>(result
), h
);
24 TEST(LlvmLibcMemmemTest
, EmptyHaystackNonEmptyNeedleReturnsNull
) {
26 char n
[] = {'a', 'b', 'c'};
27 void *result
= LIBC_NAMESPACE::memmem(h
, 0, n
, sizeof(n
));
28 ASSERT_EQ(result
, static_cast<void *>(nullptr));
31 TEST(LlvmLibcMemmemTest
, EmptyNeedleReturnsHaystack
) {
32 char h
[] = {'a', 'b', 'c'};
34 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, 0);
35 ASSERT_EQ(static_cast<char *>(result
), h
+ 0);
38 TEST(LlvmLibcMemmemTest
, ExactMatchReturnsHaystack
) {
39 char h
[] = {'a', 'b', 'c'};
40 char n
[] = {'a', 'b', 'c'};
41 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, sizeof(n
));
42 ASSERT_EQ(static_cast<char *>(result
), h
+ 0);
45 TEST(LlvmLibcMemmemTest
, ReturnFirstMatchOfNeedle
) {
46 char h
[] = {'a', 'a', 'b', 'c'};
48 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, sizeof(n
));
49 ASSERT_EQ(static_cast<char *>(result
), h
+ 0);
52 TEST(LlvmLibcMemmemTest
, ReturnFirstExactMatchOfNeedle
) {
54 char h
[] = {'a', 'b', 'a', 'c', 'a', 'a'};
55 char n
[] = {'a', 'a'};
56 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, sizeof(n
));
57 ASSERT_EQ(static_cast<char *>(result
), h
+ 4);
60 char h
[] = {'a', 'a', 'b', 'a', 'b', 'a'};
61 char n
[] = {'a', 'b', 'a'};
62 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, sizeof(n
));
63 ASSERT_EQ(static_cast<char *>(result
), h
+ 1);
67 TEST(LlvmLibcMemmemTest
, NullTerminatorDoesNotInterruptMatch
) {
68 char h
[] = {'\0', 'a', 'b'};
69 char n
[] = {'a', 'b'};
70 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, sizeof(n
));
71 ASSERT_EQ(static_cast<char *>(result
), h
+ 1);
74 TEST(LlvmLibcMemmemTest
, ReturnNullIfNoExactMatch
) {
77 char n
[] = {'a', 'a'};
78 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, sizeof(n
));
79 ASSERT_EQ(result
, static_cast<void *>(nullptr));
82 char h
[] = {'a', 'A'};
83 char n
[] = {'a', 'a'};
84 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, sizeof(n
));
85 ASSERT_EQ(result
, static_cast<void *>(nullptr));
89 char n
[] = {'a', '\0'};
90 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, sizeof(n
));
91 ASSERT_EQ(result
, static_cast<void *>(nullptr));
95 char n
[] = {'\0', '\0'};
96 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, sizeof(n
));
97 ASSERT_EQ(result
, static_cast<void *>(nullptr));
101 TEST(LlvmLibcMemmemTest
, ReturnMatchOfSpecifiedNeedleLength
) {
103 char h
[] = {'a', 'b', 'c'};
104 char n
[] = {'x', 'y', 'z'};
105 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, 0);
106 ASSERT_EQ(static_cast<char *>(result
), h
+ 0);
109 char h
[] = {'a', 'b', 'c'};
110 char n
[] = {'b', 'c', 'a'};
111 void *result
= LIBC_NAMESPACE::memmem(h
, sizeof(h
), n
, 2);
112 ASSERT_EQ(static_cast<char *>(result
), h
+ 1);
116 TEST(LlvmLibcMemmemTest
, ReturnNullIfInadequateHaystackLength
) {
118 char h
[] = {'a', 'b', 'c'};
120 void *result
= LIBC_NAMESPACE::memmem(h
, 2, n
, sizeof(n
));
121 ASSERT_EQ(result
, static_cast<void *>(nullptr));
124 char h
[] = {'a', 'b', 'c'};
125 char n
[] = {'a', 'b', 'c'};
126 void *result
= LIBC_NAMESPACE::memmem(h
, 2, n
, sizeof(n
));
127 ASSERT_EQ(result
, static_cast<void *>(nullptr));
130 } // namespace LIBC_NAMESPACE_DECL