1 //===-- Unittests for strstr ----------------------------------------------===//
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/strstr.h"
10 #include "utils/UnitTest/Test.h"
11
TEST(StrStrTest,NeedleNotInHaystack)12 TEST(StrStrTest, NeedleNotInHaystack) {
13 const char *haystack = "12345";
14 const char *needle = "a";
15 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), nullptr);
16 }
17
TEST(StrStrTest,NeedleIsEmptyString)18 TEST(StrStrTest, NeedleIsEmptyString) {
19 const char *haystack = "12345";
20 const char *needle = "";
21 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), haystack);
22 }
23
TEST(StrStrTest,HaystackIsEmptyString)24 TEST(StrStrTest, HaystackIsEmptyString) {
25 const char *haystack = "";
26 const char *needle = "12345";
27 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), nullptr);
28 }
29
TEST(StrStrTest,HaystackAndNeedleAreEmptyStrings)30 TEST(StrStrTest, HaystackAndNeedleAreEmptyStrings) {
31 const char *haystack = "";
32 const char *needle = "";
33 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "");
34 }
35
TEST(StrStrTest,HaystackAndNeedleAreSingleCharacters)36 TEST(StrStrTest, HaystackAndNeedleAreSingleCharacters) {
37 const char *haystack = "a";
38 // Same characer returns that character.
39 ASSERT_STREQ(__llvm_libc::strstr(haystack, /*needle=*/"a"), "a");
40 // Different character returns nullptr.
41 ASSERT_STREQ(__llvm_libc::strstr(haystack, /*needle=*/"b"), nullptr);
42 }
43
TEST(StrStrTest,NeedleEqualToHaystack)44 TEST(StrStrTest, NeedleEqualToHaystack) {
45 const char *haystack = "12345";
46 const char *needle = "12345";
47 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "12345");
48 }
49
TEST(StrStrTest,NeedleSmallerThanHaystack)50 TEST(StrStrTest, NeedleSmallerThanHaystack) {
51 const char *haystack = "12345";
52 const char *needle = "345";
53 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "345");
54 }
55
TEST(StrStrTest,NeedleLargerThanHaystack)56 TEST(StrStrTest, NeedleLargerThanHaystack) {
57 const char *haystack = "123";
58 const char *needle = "12345";
59 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), nullptr);
60 }
61
TEST(StrStrTest,NeedleAtBeginning)62 TEST(StrStrTest, NeedleAtBeginning) {
63 const char *haystack = "12345";
64 const char *needle = "12";
65 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "12345");
66 }
67
TEST(StrStrTest,NeedleInMiddle)68 TEST(StrStrTest, NeedleInMiddle) {
69 const char *haystack = "abcdefghi";
70 const char *needle = "def";
71 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "defghi");
72 }
73
TEST(StrStrTest,NeedleDirectlyBeforeNullTerminator)74 TEST(StrStrTest, NeedleDirectlyBeforeNullTerminator) {
75 const char *haystack = "abcdefghi";
76 const char *needle = "ghi";
77 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "ghi");
78 }
79
TEST(StrStrTest,NeedlePastNullTerminator)80 TEST(StrStrTest, NeedlePastNullTerminator) {
81 const char haystack[5] = {'1', '2', '\0', '3', '4'};
82 // Shouldn't find anything after the null terminator.
83 ASSERT_STREQ(__llvm_libc::strstr(haystack, /*needle=*/"3"), nullptr);
84 ASSERT_STREQ(__llvm_libc::strstr(haystack, /*needle=*/"4"), nullptr);
85 }
86
TEST(StrStrTest,PartialNeedle)87 TEST(StrStrTest, PartialNeedle) {
88 const char *haystack = "la_ap_lap";
89 const char *needle = "lap";
90 // Shouldn't find la or ap.
91 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "lap");
92 }
93
TEST(StrStrTest,MisspelledNeedle)94 TEST(StrStrTest, MisspelledNeedle) {
95 const char *haystack = "atalloftwocities...wait, tale";
96 const char *needle = "tale";
97 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "tale");
98 }
99
TEST(StrStrTest,AnagramNeedle)100 TEST(StrStrTest, AnagramNeedle) {
101 const char *haystack = "dgo_ogd_god_odg_gdo_dog";
102 const char *needle = "dog";
103 ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "dog");
104 }
105
TEST(StrStrTest,MorphedNeedle)106 TEST(StrStrTest, 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(__llvm_libc::strstr(haystack, "time"), "time");
110 ASSERT_STREQ(__llvm_libc::strstr(haystack, "lime"), nullptr);
111 ASSERT_STREQ(__llvm_libc::strstr(haystack, "tome"), nullptr);
112 ASSERT_STREQ(__llvm_libc::strstr(haystack, "tire"), nullptr);
113 ASSERT_STREQ(__llvm_libc::strstr(haystack, "timo"), nullptr);
114 }
115