llvm-project/llvm/unittests/ADT/StringViewExtrasTest.cpp
Nick Desaulniers d6d30dd959 [ADT] add StringViewExtras llvm::starts_with for std::string_view
std::string_view::starts_with isn't available until C++20. Create
llvm::starts_with for now; we can delete this when LLVM moves to C++20
one day.

To run the newly added unit test:
$ cd llvm/build; ninja ADTTests; cd -
$ ./llvm/build/unittests/ADT/ADTTests --gtest_filter=StringViewExtrasTest.\*

Reviewed By: MaskRay, erichkeane

Differential Revision: https://reviews.llvm.org/D148367
2023-04-14 13:41:55 -07:00

33 lines
1.1 KiB
C++

//===- StringExtrasTest.cpp - Unit tests for String extras ----------------===//
//
// 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 "llvm/ADT/StringViewExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <string_view>
using namespace llvm;
TEST(StringViewExtrasTest, starts_with) {
std::string haystack = "hello world";
EXPECT_TRUE(llvm::starts_with(haystack, 'h'));
EXPECT_FALSE(llvm::starts_with(haystack, '\0'));
EXPECT_TRUE(llvm::starts_with(haystack, "hello"));
// TODO: should this differ from \0?
EXPECT_TRUE(llvm::starts_with(haystack, ""));
std::string empty;
EXPECT_FALSE(llvm::starts_with(empty, 'h'));
EXPECT_FALSE(llvm::starts_with(empty, '\0'));
EXPECT_FALSE(llvm::starts_with(empty, "hello"));
// TODO: should this differ from \0?
EXPECT_TRUE(llvm::starts_with(empty, ""));
}