1 //===-- Unittests for strpbrk ---------------------------------------------===//
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/strpbrk.h"
10
11 #include "utils/UnitTest/Test.h"
12
TEST(StrPBrkTest,EmptyStringShouldReturnNullptr)13 TEST(StrPBrkTest, EmptyStringShouldReturnNullptr) {
14 // The search should not include the null terminator.
15 EXPECT_STREQ(__llvm_libc::strpbrk("", ""), nullptr);
16 EXPECT_STREQ(__llvm_libc::strpbrk("_", ""), nullptr);
17 EXPECT_STREQ(__llvm_libc::strpbrk("", "_"), nullptr);
18 }
19
TEST(StrPBrkTest,ShouldNotFindAnythingAfterNullTerminator)20 TEST(StrPBrkTest, ShouldNotFindAnythingAfterNullTerminator) {
21 const char src[4] = {'a', 'b', '\0', 'c'};
22 EXPECT_STREQ(__llvm_libc::strpbrk(src, "c"), nullptr);
23 }
24
TEST(StrPBrkTest,ShouldReturnNullptrIfNoCharactersFound)25 TEST(StrPBrkTest, ShouldReturnNullptrIfNoCharactersFound) {
26 EXPECT_STREQ(__llvm_libc::strpbrk("12345", "abcdef"), nullptr);
27 }
28
TEST(StrPBrkTest,FindsFirstCharacter)29 TEST(StrPBrkTest, FindsFirstCharacter) {
30 const char *src = "12345";
31 EXPECT_STREQ(__llvm_libc::strpbrk(src, "1"), "12345");
32 EXPECT_STREQ(__llvm_libc::strpbrk(src, "-1"), "12345");
33 EXPECT_STREQ(__llvm_libc::strpbrk(src, "1_"), "12345");
34 EXPECT_STREQ(__llvm_libc::strpbrk(src, "f1_"), "12345");
35 ASSERT_STREQ(src, "12345");
36 }
37
TEST(StrPBrkTest,FindsMiddleCharacter)38 TEST(StrPBrkTest, FindsMiddleCharacter) {
39 const char *src = "12345";
40 EXPECT_STREQ(__llvm_libc::strpbrk(src, "3"), "345");
41 EXPECT_STREQ(__llvm_libc::strpbrk(src, "?3"), "345");
42 EXPECT_STREQ(__llvm_libc::strpbrk(src, "3F"), "345");
43 EXPECT_STREQ(__llvm_libc::strpbrk(src, "z3_"), "345");
44 ASSERT_STREQ(src, "12345");
45 }
46
TEST(StrPBrkTest,FindsLastCharacter)47 TEST(StrPBrkTest, FindsLastCharacter) {
48 const char *src = "12345";
49 EXPECT_STREQ(__llvm_libc::strpbrk(src, "5"), "5");
50 EXPECT_STREQ(__llvm_libc::strpbrk(src, "r5"), "5");
51 EXPECT_STREQ(__llvm_libc::strpbrk(src, "59"), "5");
52 EXPECT_STREQ(__llvm_libc::strpbrk(src, "n5_"), "5");
53 ASSERT_STREQ(src, "12345");
54 }
55
TEST(StrPBrkTest,FindsFirstOfRepeated)56 TEST(StrPBrkTest, FindsFirstOfRepeated) {
57 EXPECT_STREQ(__llvm_libc::strpbrk("A,B,C,D", ","), ",B,C,D");
58 }
59
TEST(StrPBrkTest,FindsFirstInBreakset)60 TEST(StrPBrkTest, FindsFirstInBreakset) {
61 EXPECT_STREQ(__llvm_libc::strpbrk("12345", "34"), "345");
62 }
63