xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.cons/from_iterator_sentinel.pass.cpp (revision 53406fb691db38b21decf233e091f648f8317b2d)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 // UNSUPPORTED: libcpp-no-concepts
10 
11 // <string_view>
12 
13 //  template <class It, class End>
14 //  constexpr basic_string_view(It begin, End end)
15 
16 #include <string_view>
17 #include <cassert>
18 #include <iterator>
19 
20 #include "make_string.h"
21 #include "test_iterators.h"
22 
23 template<class CharT, class Sentinel>
24 constexpr void test() {
25   auto val = MAKE_STRING_VIEW(CharT, "test");
26   auto sv = std::basic_string_view<CharT>(val.begin(), Sentinel(val.end()));
27   ASSERT_SAME_TYPE(decltype(sv), std::basic_string_view<CharT>);
28   assert(sv.size() == val.size());
29   assert(sv.data() == val.data());
30 }
31 
32 constexpr bool test() {
33   test<char, char*>();
34 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
35   test<wchar_t, wchar_t*>();
36 #endif
37   test<char8_t, char8_t*>();
38   test<char16_t, char16_t*>();
39   test<char32_t, char32_t*>();
40   test<char, const char*>();
41   test<char, sized_sentinel<const char*>>();
42   return true;
43 }
44 
45 #ifndef TEST_HAS_NO_EXCEPTIONS
46 template<class CharT>
47 struct ThrowingSentinel {
48   friend bool operator==(const CharT*, ThrowingSentinel) noexcept { return true; }
49   friend std::iter_difference_t<const CharT*> operator-(const CharT*, ThrowingSentinel) noexcept { return {}; }
50   friend std::iter_difference_t<const CharT*> operator-(ThrowingSentinel, const CharT*) { throw 42; }
51 };
52 
53 template <class CharT>
54 void test_throwing() {
55   auto val = MAKE_STRING_VIEW(CharT, "test");
56   try {
57     (void)std::basic_string_view<CharT>(val.begin(), ThrowingSentinel<CharT>());
58     assert(false);
59   } catch (int i) {
60     assert(i == 42);
61   }
62 }
63 
64 void test_throwing() {
65   test_throwing<char>();
66 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
67   test_throwing<wchar_t>();
68 #endif
69   test_throwing<char8_t>();
70   test_throwing<char16_t>();
71   test_throwing<char32_t>();
72 }
73 #endif
74 
75 static_assert( std::is_constructible_v<std::string_view, const char*, char*>);
76 static_assert( std::is_constructible_v<std::string_view, char*, const char*>);
77 static_assert(!std::is_constructible_v<std::string_view, char*, void*>);               // not a sentinel
78 static_assert(!std::is_constructible_v<std::string_view, signed char*, signed char*>); // wrong char type
79 static_assert(!std::is_constructible_v<std::string_view, random_access_iterator<char*>, random_access_iterator<char*>>); // not contiguous
80 static_assert( std::is_constructible_v<std::string_view, contiguous_iterator<char*>, contiguous_iterator<char*>>);
81 
82 int main(int, char**) {
83   test();
84   static_assert(test());
85 
86 #ifndef TEST_HAS_NO_EXCEPTIONS
87   test_throwing();
88 #endif
89 
90   return 0;
91 }
92 
93