//===----------------------------------------------------------------------===// // // 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: c++03, c++11, c++14, c++17 // // template // constexpr basic_string_view(It begin, End end) #include #include #include #include "make_string.h" #include "test_iterators.h" template constexpr void test_construction(std::basic_string_view val) { auto sv = std::basic_string_view(It(val.data()), Sentinel(It(val.data() + val.size()))); assert(sv.data() == val.data()); assert(sv.size() == val.size()); } template constexpr void test_with_char() { const auto val = MAKE_STRING_VIEW(CharT, "test"); test_construction(val); test_construction(val); test_construction(val); test_construction>(val); test_construction, contiguous_iterator>(val); test_construction, sized_sentinel>>(val); } constexpr bool test() { test_with_char(); #ifndef TEST_HAS_NO_WIDE_CHARACTERS test_with_char(); #endif test_with_char(); test_with_char(); test_with_char(); return true; } #ifndef TEST_HAS_NO_EXCEPTIONS template struct ThrowingSentinel { friend bool operator==(const CharT*, ThrowingSentinel) noexcept { return true; } friend std::iter_difference_t operator-(const CharT*, ThrowingSentinel) noexcept { return {}; } friend std::iter_difference_t operator-(ThrowingSentinel, const CharT*) { throw 42; } }; template void test_throwing() { auto val = MAKE_STRING_VIEW(CharT, "test"); try { (void)std::basic_string_view(val.data(), ThrowingSentinel()); assert(false); } catch (int i) { assert(i == 42); } } void test_throwing() { test_throwing(); # ifndef TEST_HAS_NO_WIDE_CHARACTERS test_throwing(); # endif test_throwing(); test_throwing(); test_throwing(); } #endif static_assert(std::is_constructible_v); static_assert(std::is_constructible_v); static_assert(!std::is_constructible_v); // not a sentinel static_assert(!std::is_constructible_v); // wrong char type static_assert(!std::is_constructible_v, random_access_iterator>); // not contiguous static_assert(std::is_constructible_v, contiguous_iterator>); int main(int, char**) { test(); static_assert(test()); #ifndef TEST_HAS_NO_EXCEPTIONS test_throwing(); #endif return 0; }