Files
clang-p2996/libcxx/test/std/strings/string.view/string.view.capacity/capacity.pass.cpp
Joe Loser 4fc502368a [libc++][test] Skip string_view tests for other vendors on older modes
`string_view` is supported all the way back to C++03 as an extension in
`libc++`, and so many of the tests run in all standards modes for all vendors.
This is unlikely desired by other standard library vendors using our test suite.
So, disable the tests for vendors other than `libc++` in these older standards
modes.

Differential Revision: https://reviews.llvm.org/D126850
2022-06-03 13:51:49 -06:00

121 lines
3.7 KiB
C++

//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
// <string_view>
// [string.view.capacity], capacity
// constexpr size_type size() const noexcept;
// constexpr size_type length() const noexcept;
// constexpr size_type max_size() const noexcept;
// constexpr bool empty() const noexcept;
#include <string_view>
#include <cassert>
#include <limits>
#include "test_macros.h"
template<typename SV>
void test1 () {
#if TEST_STD_VER > 11
{
constexpr SV sv1;
static_assert ( sv1.size() == 0, "" );
static_assert ( sv1.empty(), "");
static_assert ( sv1.size() == sv1.length(), "" );
static_assert ( sv1.max_size() > sv1.size(), "");
}
#endif
{
SV sv1;
ASSERT_NOEXCEPT(sv1.size());
ASSERT_NOEXCEPT(sv1.empty());
ASSERT_NOEXCEPT(sv1.max_size());
ASSERT_NOEXCEPT(sv1.length());
assert ( sv1.size() == 0 );
assert ( sv1.empty());
assert ( sv1.size() == sv1.length());
assert ( sv1.max_size() > sv1.size());
}
// Sanity check max_size() -- a string_view can't store more bytes than a single object
// can contain. Any implementation that fails this check is certainly lying.
{
typedef typename SV::value_type CharT;
typedef typename SV::size_type Size;
SV sv;
assert(sv.max_size() <= std::numeric_limits<Size>::max() / sizeof(CharT));
LIBCPP_ASSERT(sv.max_size() == std::numeric_limits<Size>::max() / sizeof(CharT));
}
}
template<typename CharT>
void test2 ( const CharT *s, size_t len ) {
{
std::basic_string_view<CharT> sv1 ( s );
assert ( sv1.size() == len );
assert ( sv1.data() == s );
assert ( sv1.empty() == (len == 0));
assert ( sv1.size() == sv1.length());
assert ( sv1.max_size() > sv1.size());
#if TEST_STD_VER > 14
// make sure we pick up std::size, too!
assert ( sv1.size() == std::size(sv1));
assert ( sv1.empty() == std::empty(sv1));
#endif
}
}
int main(int, char**) {
test1<std::string_view> ();
#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
test1<std::u8string_view> ();
#endif
test1<std::u16string_view> ();
test1<std::u32string_view> ();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test1<std::wstring_view> ();
#endif
test2 ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE", 105 );
test2 ( "ABCDE", 5 );
test2 ( "a", 1 );
test2 ( "", 0 );
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test2 ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE", 105 );
test2 ( L"ABCDE", 5 );
test2 ( L"a", 1 );
test2 ( L"", 0 );
#endif
#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
test2 ( u8"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE", 105 );
test2 ( u8"ABCDE", 5 );
test2 ( u8"a", 1 );
test2 ( u8"", 0 );
#endif
#if TEST_STD_VER >= 11
test2 ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE", 105 );
test2 ( u"ABCDE", 5 );
test2 ( u"a", 1 );
test2 ( u"", 0 );
test2 ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE", 105 );
test2 ( U"ABCDE", 5 );
test2 ( U"a", 1 );
test2 ( U"", 0 );
#endif
return 0;
}