strpbrk_test.cpp 2.15 KB
//===-- Unittests for strpbrk ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/string/strpbrk.h"

#include "utils/UnitTest/Test.h"

TEST(StrPBrkTest, EmptyStringShouldReturnNullptr) {
  // The search should not include the null terminator.
  EXPECT_STREQ(__llvm_libc::strpbrk("", ""), nullptr);
  EXPECT_STREQ(__llvm_libc::strpbrk("_", ""), nullptr);
  EXPECT_STREQ(__llvm_libc::strpbrk("", "_"), nullptr);
}

TEST(StrPBrkTest, ShouldNotFindAnythingAfterNullTerminator) {
  const char src[4] = {'a', 'b', '\0', 'c'};
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "c"), nullptr);
}

TEST(StrPBrkTest, ShouldReturnNullptrIfNoCharactersFound) {
  EXPECT_STREQ(__llvm_libc::strpbrk("12345", "abcdef"), nullptr);
}

TEST(StrPBrkTest, FindsFirstCharacter) {
  const char *src = "12345";
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "1"), "12345");
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "-1"), "12345");
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "1_"), "12345");
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "f1_"), "12345");
  ASSERT_STREQ(src, "12345");
}

TEST(StrPBrkTest, FindsMiddleCharacter) {
  const char *src = "12345";
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "3"), "345");
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "?3"), "345");
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "3F"), "345");
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "z3_"), "345");
  ASSERT_STREQ(src, "12345");
}

TEST(StrPBrkTest, FindsLastCharacter) {
  const char *src = "12345";
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "5"), "5");
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "r5"), "5");
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "59"), "5");
  EXPECT_STREQ(__llvm_libc::strpbrk(src, "n5_"), "5");
  ASSERT_STREQ(src, "12345");
}

TEST(StrPBrkTest, FindsFirstOfRepeated) {
  EXPECT_STREQ(__llvm_libc::strpbrk("A,B,C,D", ","), ",B,C,D");
}

TEST(StrPBrkTest, FindsFirstInBreakset) {
  EXPECT_STREQ(__llvm_libc::strpbrk("12345", "34"), "345");
}