xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.cons/from_iterator_sentinel.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
14be7f489SJoe Loser //===----------------------------------------------------------------------===//
24be7f489SJoe Loser //
34be7f489SJoe Loser // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44be7f489SJoe Loser // See https://llvm.org/LICENSE.txt for license information.
54be7f489SJoe Loser // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64be7f489SJoe Loser //
74be7f489SJoe Loser //===----------------------------------------------------------------------===//
84be7f489SJoe Loser // UNSUPPORTED: c++03, c++11, c++14, c++17
94be7f489SJoe Loser 
104be7f489SJoe Loser // <string_view>
114be7f489SJoe Loser 
124be7f489SJoe Loser //  template <class It, class End>
134be7f489SJoe Loser //  constexpr basic_string_view(It begin, End end)
144be7f489SJoe Loser 
154be7f489SJoe Loser #include <string_view>
164be7f489SJoe Loser #include <cassert>
17494dad6bSJoe Loser #include <iterator>
184be7f489SJoe Loser 
194be7f489SJoe Loser #include "make_string.h"
204be7f489SJoe Loser #include "test_iterators.h"
214be7f489SJoe Loser 
222d653b7eSCasey Carter template <class It, class Sentinel, class CharT>
test_construction(std::basic_string_view<CharT> val)232d653b7eSCasey Carter constexpr void test_construction(std::basic_string_view<CharT> val) {
242d653b7eSCasey Carter   auto sv = std::basic_string_view<CharT>(It(val.data()), Sentinel(It(val.data() + val.size())));
254be7f489SJoe Loser   assert(sv.data() == val.data());
262d653b7eSCasey Carter   assert(sv.size() == val.size());
272d653b7eSCasey Carter }
282d653b7eSCasey Carter 
292d653b7eSCasey Carter template <class CharT>
test_with_char()302d653b7eSCasey Carter constexpr void test_with_char() {
312d653b7eSCasey Carter   const auto val = MAKE_STRING_VIEW(CharT, "test");
322d653b7eSCasey Carter   test_construction<CharT*, CharT*>(val);
332d653b7eSCasey Carter   test_construction<CharT*, const CharT*>(val);
342d653b7eSCasey Carter   test_construction<const CharT*, CharT*>(val);
352d653b7eSCasey Carter   test_construction<const CharT*, sized_sentinel<const CharT*>>(val);
362d653b7eSCasey Carter   test_construction<contiguous_iterator<const CharT*>, contiguous_iterator<const CharT*>>(val);
372d653b7eSCasey Carter   test_construction<contiguous_iterator<const CharT*>, sized_sentinel<contiguous_iterator<const CharT*>>>(val);
384be7f489SJoe Loser }
394be7f489SJoe Loser 
test()404be7f489SJoe Loser constexpr bool test() {
412d653b7eSCasey Carter   test_with_char<char>();
42f4c1258dSLouis Dionne #ifndef TEST_HAS_NO_WIDE_CHARACTERS
432d653b7eSCasey Carter   test_with_char<wchar_t>();
44f4c1258dSLouis Dionne #endif
452d653b7eSCasey Carter   test_with_char<char8_t>();
462d653b7eSCasey Carter   test_with_char<char16_t>();
472d653b7eSCasey Carter   test_with_char<char32_t>();
482d653b7eSCasey Carter 
494be7f489SJoe Loser   return true;
504be7f489SJoe Loser }
514be7f489SJoe Loser 
52494dad6bSJoe Loser #ifndef TEST_HAS_NO_EXCEPTIONS
53494dad6bSJoe Loser template <class CharT>
54494dad6bSJoe Loser struct ThrowingSentinel {
operator ==(const CharT *,ThrowingSentinel)55494dad6bSJoe Loser   friend bool operator==(const CharT*, ThrowingSentinel) noexcept { return true; }
operator -(const CharT *,ThrowingSentinel)56494dad6bSJoe Loser   friend std::iter_difference_t<const CharT*> operator-(const CharT*, ThrowingSentinel) noexcept { return {}; }
operator -(ThrowingSentinel,const CharT *)57494dad6bSJoe Loser   friend std::iter_difference_t<const CharT*> operator-(ThrowingSentinel, const CharT*) { throw 42; }
58494dad6bSJoe Loser };
59494dad6bSJoe Loser 
60494dad6bSJoe Loser template <class CharT>
test_throwing()61494dad6bSJoe Loser void test_throwing() {
62494dad6bSJoe Loser   auto val = MAKE_STRING_VIEW(CharT, "test");
63494dad6bSJoe Loser   try {
642d653b7eSCasey Carter     (void)std::basic_string_view<CharT>(val.data(), ThrowingSentinel<CharT>());
65494dad6bSJoe Loser     assert(false);
66494dad6bSJoe Loser   } catch (int i) {
67494dad6bSJoe Loser     assert(i == 42);
68494dad6bSJoe Loser   }
69494dad6bSJoe Loser }
70494dad6bSJoe Loser 
test_throwing()71494dad6bSJoe Loser void test_throwing() {
72494dad6bSJoe Loser   test_throwing<char>();
73494dad6bSJoe Loser #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
74494dad6bSJoe Loser   test_throwing<wchar_t>();
75494dad6bSJoe Loser #  endif
76494dad6bSJoe Loser   test_throwing<char8_t>();
77494dad6bSJoe Loser   test_throwing<char16_t>();
78494dad6bSJoe Loser   test_throwing<char32_t>();
79494dad6bSJoe Loser }
80494dad6bSJoe Loser #endif
81494dad6bSJoe Loser 
824be7f489SJoe Loser static_assert(std::is_constructible_v<std::string_view, const char*, char*>);
834be7f489SJoe Loser static_assert(std::is_constructible_v<std::string_view, char*, const char*>);
844be7f489SJoe Loser static_assert(!std::is_constructible_v<std::string_view, char*, void*>);               // not a sentinel
854be7f489SJoe Loser static_assert(!std::is_constructible_v<std::string_view, signed char*, signed char*>); // wrong char type
86*a40bada9SBrendan Emery static_assert(!std::is_constructible_v<std::string_view,
87*a40bada9SBrendan Emery                                        random_access_iterator<char*>,
88*a40bada9SBrendan Emery                                        random_access_iterator<char*>>); // not contiguous
894be7f489SJoe Loser static_assert(std::is_constructible_v<std::string_view, contiguous_iterator<char*>, contiguous_iterator<char*>>);
904be7f489SJoe Loser 
main(int,char **)914be7f489SJoe Loser int main(int, char**) {
924be7f489SJoe Loser   test();
934be7f489SJoe Loser   static_assert(test());
944be7f489SJoe Loser 
95494dad6bSJoe Loser #ifndef TEST_HAS_NO_EXCEPTIONS
96494dad6bSJoe Loser   test_throwing();
97494dad6bSJoe Loser #endif
98494dad6bSJoe Loser 
994be7f489SJoe Loser   return 0;
1004be7f489SJoe Loser }
101