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(LlvmLibcStrStrTest,NeedleNotInHaystack)12 TEST(LlvmLibcStrStrTest, NeedleNotInHaystack) {
13   const char *haystack = "12345";
14   const char *needle = "a";
15   ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), nullptr);
16 }
17 
TEST(LlvmLibcStrStrTest,NeedleIsEmptyString)18 TEST(LlvmLibcStrStrTest, NeedleIsEmptyString) {
19   const char *haystack = "12345";
20   const char *needle = "";
21   ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), haystack);
22 }
23 
TEST(LlvmLibcStrStrTest,HaystackIsEmptyString)24 TEST(LlvmLibcStrStrTest, HaystackIsEmptyString) {
25   const char *haystack = "";
26   const char *needle = "12345";
27   ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), nullptr);
28 }
29 
TEST(LlvmLibcStrStrTest,HaystackAndNeedleAreEmptyStrings)30 TEST(LlvmLibcStrStrTest, HaystackAndNeedleAreEmptyStrings) {
31   const char *haystack = "";
32   const char *needle = "";
33   ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "");
34 }
35 
TEST(LlvmLibcStrStrTest,HaystackAndNeedleAreSingleCharacters)36 TEST(LlvmLibcStrStrTest, 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(LlvmLibcStrStrTest,NeedleEqualToHaystack)44 TEST(LlvmLibcStrStrTest, NeedleEqualToHaystack) {
45   const char *haystack = "12345";
46   const char *needle = "12345";
47   ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "12345");
48 }
49 
TEST(LlvmLibcStrStrTest,NeedleSmallerThanHaystack)50 TEST(LlvmLibcStrStrTest, NeedleSmallerThanHaystack) {
51   const char *haystack = "12345";
52   const char *needle = "345";
53   ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "345");
54 }
55 
TEST(LlvmLibcStrStrTest,NeedleLargerThanHaystack)56 TEST(LlvmLibcStrStrTest, NeedleLargerThanHaystack) {
57   const char *haystack = "123";
58   const char *needle = "12345";
59   ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), nullptr);
60 }
61 
TEST(LlvmLibcStrStrTest,NeedleAtBeginning)62 TEST(LlvmLibcStrStrTest, NeedleAtBeginning) {
63   const char *haystack = "12345";
64   const char *needle = "12";
65   ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "12345");
66 }
67 
TEST(LlvmLibcStrStrTest,NeedleInMiddle)68 TEST(LlvmLibcStrStrTest, NeedleInMiddle) {
69   const char *haystack = "abcdefghi";
70   const char *needle = "def";
71   ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "defghi");
72 }
73 
TEST(LlvmLibcStrStrTest,NeedleDirectlyBeforeNullTerminator)74 TEST(LlvmLibcStrStrTest, NeedleDirectlyBeforeNullTerminator) {
75   const char *haystack = "abcdefghi";
76   const char *needle = "ghi";
77   ASSERT_STREQ(__llvm_libc::strstr(haystack, needle), "ghi");
78 }
79 
TEST(LlvmLibcStrStrTest,NeedlePastNullTerminator)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(__llvm_libc::strstr(haystack, /*needle=*/"3"), nullptr);
84   ASSERT_STREQ(__llvm_libc::strstr(haystack, /*needle=*/"4"), nullptr);
85 }
86 
TEST(LlvmLibcStrStrTest,PartialNeedle)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(__llvm_libc::strstr(haystack, needle), "lap");
92 }
93 
TEST(LlvmLibcStrStrTest,MisspelledNeedle)94 TEST(LlvmLibcStrStrTest, 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(LlvmLibcStrStrTest,AnagramNeedle)100 TEST(LlvmLibcStrStrTest, 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(LlvmLibcStrStrTest,MorphedNeedle)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(__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