chromium/third_party/abseil-cpp/absl/strings/string_view_test.cc

// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "absl/strings/string_view.h"

#include <stdlib.h>

#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <ios>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <type_traits>
#include <utility>

#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/meta/type_traits.h"

#if defined(ABSL_HAVE_STD_STRING_VIEW) || defined(__ANDROID__)
// We don't control the death messaging when using std::string_view.
// Android assert messages only go to system log, so death tests cannot inspect
// the message for matching.
#define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#else
#define ABSL_EXPECT_DEATH_IF_SUPPORTED
#endif

namespace {

static_assert;

static_assert;

// A minimal allocator that uses malloc().
template <typename T>
struct Mallocator {};
template <typename T, typename U>
bool operator==(const Mallocator<T>&, const Mallocator<U>&) {}
template <typename T, typename U>
bool operator!=(const Mallocator<T>&, const Mallocator<U>&) {}

TEST(StringViewTest, Ctor) {}

TEST(StringViewTest, Swap) {}

TEST(StringViewTest, STLComparator) {}

#define COMPARE

TEST(StringViewTest, ComparisonOperators) {}

TEST(StringViewTest, ComparisonOperatorsByCharacterPosition) {}
#undef COMPARE

// Sadly, our users often confuse std::string::npos with
// absl::string_view::npos; So much so that we test here that they are the same.
// They need to both be unsigned, and both be the maximum-valued integer of
// their type.

template <typename T>
struct is_type {};

TEST(StringViewTest, NposMatchesStdStringView) {}

TEST(StringViewTest, STL1) {}

// Separated from STL1() because some compilers produce an overly
// large stack frame for the combined function.
TEST(StringViewTest, STL2) {}

// Continued from STL2
TEST(StringViewTest, STL2FindFirst) {}

// Continued from STL2
TEST(StringViewTest, STL2FindLast) {}

// Continued from STL2
TEST(StringViewTest, STL2Substr) {}

TEST(StringViewTest, TruncSubstr) {}

TEST(StringViewTest, UTF8) {}

TEST(StringViewTest, FindConformance) {}

TEST(StringViewTest, Remove) {}

TEST(StringViewTest, Set) {}

TEST(StringViewTest, FrontBack) {}

TEST(StringViewTest, FrontBackSingleChar) {}

TEST(StringViewTest, FrontBackEmpty) {}

// `std::string_view::string_view(const char*)` calls
// `std::char_traits<char>::length(const char*)` to get the string length. In
// libc++, it doesn't allow `nullptr` in the constexpr context, with the error
// "read of dereferenced null pointer is not allowed in a constant expression".
// At run time, the behavior of `std::char_traits::length()` on `nullptr` is
// undefined by the standard and usually results in crash with libc++.
// GCC also started rejected this in libstdc++ starting in GCC9.
// In MSVC, creating a constexpr string_view from nullptr also triggers an
// "unevaluable pointer value" error. This compiler implementation conforms
// to the standard, but `absl::string_view` implements a different
// behavior for historical reasons. We work around tests that construct
// `string_view` from `nullptr` when using libc++.
#if !defined(ABSL_USES_STD_STRING_VIEW) ||                    \
    (!(defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 9) && \
     !defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
#define ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
#endif

TEST(StringViewTest, NULLInput) {}

TEST(StringViewTest, Comparisons2) {}

TEST(StringViewTest, At) {}

#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
TEST(StringViewTest, StartsWith) {}

TEST(StringViewTest, EndsWith) {}
#endif  // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L

struct MyCharAlloc : std::allocator<char> {};

TEST(StringViewTest, ExplicitConversionOperator) {}

TEST(StringViewTest, NullSafeStringView) {}

TEST(StringViewTest, ConstexprNullSafeStringView) {}

TEST(StringViewTest, ConstexprCompiles) {}

constexpr char ConstexprMethodsHelper() {}

TEST(StringViewTest, ConstexprMethods) {}

TEST(StringViewTest, Noexcept) {}

TEST(StringViewTest, BoundsCheck) {}

TEST(ComparisonOpsTest, StringCompareNotAmbiguous) {}

TEST(ComparisonOpsTest, HeterogeneousStringViewEquals) {}

TEST(FindOneCharTest, EdgeCases) {}

#ifndef ABSL_HAVE_THREAD_SANITIZER  // Allocates too much memory for tsan.
TEST(HugeStringView, TwoPointTwoGB) {}
#endif  // ABSL_HAVE_THREAD_SANITIZER

#if !defined(NDEBUG) && !defined(ABSL_USES_STD_STRING_VIEW)
TEST(NonNegativeLenTest, NonNegativeLen) {
  ABSL_EXPECT_DEATH_IF_SUPPORTED(
      absl::string_view("xyz", static_cast<size_t>(-1)), "len <= kMaxSize");
}

TEST(LenExceedsMaxSizeTest, LenExceedsMaxSize) {
  auto max_size = absl::string_view().max_size();

  // This should construct ok (although the view itself is obviously invalid).
  absl::string_view ok_view("", max_size);

  // Adding one to the max should trigger an assertion.
  ABSL_EXPECT_DEATH_IF_SUPPORTED(absl::string_view("", max_size + 1),
                                 "len <= kMaxSize");
}
#endif  // !defined(NDEBUG) && !defined(ABSL_USES_STD_STRING_VIEW)

class StringViewStreamTest : public ::testing::Test {};

TEST_F(StringViewStreamTest, Padding) {}

TEST_F(StringViewStreamTest, ResetsWidth) {}

}  // namespace