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

source code of libc/test/src/string/strpbrk_test.cpp