1 //===-- Unittests for strstr ----------------------------------------------===//
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/string/strstr.h"
10 #include "test/UnitTest/Test.h"
12 TEST(LlvmLibcStrStrTest
, NeedleNotInHaystack
) {
13 const char *haystack
= "12345";
14 const char *needle
= "a";
15 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), nullptr);
18 TEST(LlvmLibcStrStrTest
, NeedleIsEmptyString
) {
19 const char *haystack
= "12345";
20 const char *needle
= "";
21 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), haystack
);
24 TEST(LlvmLibcStrStrTest
, HaystackIsEmptyString
) {
25 const char *haystack
= "";
26 const char *needle
= "12345";
27 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), nullptr);
30 TEST(LlvmLibcStrStrTest
, HaystackAndNeedleAreEmptyStrings
) {
31 const char *haystack
= "";
32 const char *needle
= "";
33 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), "");
36 TEST(LlvmLibcStrStrTest
, HaystackAndNeedleAreSingleCharacters
) {
37 const char *haystack
= "a";
38 // Same characer returns that character.
39 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, /*needle=*/"a"), "a");
40 // Different character returns nullptr.
41 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, /*needle=*/"b"), nullptr);
44 TEST(LlvmLibcStrStrTest
, NeedleEqualToHaystack
) {
45 const char *haystack
= "12345";
46 const char *needle
= "12345";
47 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), "12345");
50 TEST(LlvmLibcStrStrTest
, NeedleSmallerThanHaystack
) {
51 const char *haystack
= "12345";
52 const char *needle
= "345";
53 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), "345");
56 TEST(LlvmLibcStrStrTest
, NeedleLargerThanHaystack
) {
57 const char *haystack
= "123";
58 const char *needle
= "12345";
59 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), nullptr);
62 TEST(LlvmLibcStrStrTest
, NeedleAtBeginning
) {
63 const char *haystack
= "12345";
64 const char *needle
= "12";
65 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), "12345");
68 TEST(LlvmLibcStrStrTest
, NeedleInMiddle
) {
69 const char *haystack
= "abcdefghi";
70 const char *needle
= "def";
71 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), "defghi");
74 TEST(LlvmLibcStrStrTest
, NeedleDirectlyBeforeNullTerminator
) {
75 const char *haystack
= "abcdefghi";
76 const char *needle
= "ghi";
77 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), "ghi");
80 TEST(LlvmLibcStrStrTest
, NeedlePastNullTerminator
) {
81 const char haystack
[5] = {'1', '2', '\0', '3', '4'};
82 // Shouldn't find anything after the null terminator.
83 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, /*needle=*/"3"), nullptr);
84 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, /*needle=*/"4"), nullptr);
87 TEST(LlvmLibcStrStrTest
, PartialNeedle
) {
88 const char *haystack
= "la_ap_lap";
89 const char *needle
= "lap";
90 // Shouldn't find la or ap.
91 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), "lap");
94 TEST(LlvmLibcStrStrTest
, MisspelledNeedle
) {
95 const char *haystack
= "atalloftwocities...wait, tale";
96 const char *needle
= "tale";
97 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), "tale");
100 TEST(LlvmLibcStrStrTest
, AnagramNeedle
) {
101 const char *haystack
= "dgo_ogd_god_odg_gdo_dog";
102 const char *needle
= "dog";
103 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, needle
), "dog");
106 TEST(LlvmLibcStrStrTest
, MorphedNeedle
) {
107 // Changes a single letter in the needle to mismatch with the haystack.
108 const char *haystack
= "once upon a time";
109 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, "time"), "time");
110 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, "lime"), nullptr);
111 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, "tome"), nullptr);
112 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, "tire"), nullptr);
113 ASSERT_STREQ(LIBC_NAMESPACE::strstr(haystack
, "timo"), nullptr);