xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.cons/from_iterator_sentinel.pass.cpp (revision 494dad6b72d499ea832ab4b9b9bb76e0330e4eeb)
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 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11 
12 // <string_view>
13 
14 //  template <class It, class End>
15 //  constexpr basic_string_view(It begin, End end)
16 
17 #include <string_view>
18 #include <cassert>
19 #include <iterator>
20 #include <ranges>
21 
22 #include "make_string.h"
23 #include "test_iterators.h"
24 
25 template<class CharT, class Sentinel>
26 constexpr void test() {
27   auto val = MAKE_STRING_VIEW(CharT, "test");
28   auto sv = std::basic_string_view<CharT>(val.begin(), Sentinel(val.end()));
29   ASSERT_SAME_TYPE(decltype(sv), std::basic_string_view<CharT>);
30   assert(sv.size() == val.size());
31   assert(sv.data() == val.data());
32 }
33 
34 constexpr bool test() {
35   test<char, char*>();
36 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
37   test<wchar_t, wchar_t*>();
38 #endif
39   test<char8_t, char8_t*>();
40   test<char16_t, char16_t*>();
41   test<char32_t, char32_t*>();
42   test<char, const char*>();
43   test<char, sized_sentinel<const char*>>();
44   return true;
45 }
46 
47 #ifndef TEST_HAS_NO_EXCEPTIONS
48 template<class CharT>
49 struct ThrowingSentinel {
50   friend bool operator==(const CharT*, ThrowingSentinel) noexcept { return true; }
51   friend std::iter_difference_t<const CharT*> operator-(const CharT*, ThrowingSentinel) noexcept { return {}; }
52   friend std::iter_difference_t<const CharT*> operator-(ThrowingSentinel, const CharT*) { throw 42; }
53 };
54 
55 template <class CharT>
56 void test_throwing() {
57   auto val = MAKE_STRING_VIEW(CharT, "test");
58   try {
59     (void)std::basic_string_view<CharT>(val.begin(), ThrowingSentinel<CharT>());
60     assert(false);
61   } catch (int i) {
62     assert(i == 42);
63   }
64 }
65 
66 void test_throwing() {
67   test_throwing<char>();
68 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
69   test_throwing<wchar_t>();
70 #endif
71   test_throwing<char8_t>();
72   test_throwing<char16_t>();
73   test_throwing<char32_t>();
74 }
75 #endif
76 
77 static_assert( std::is_constructible_v<std::string_view, const char*, char*>);
78 static_assert( std::is_constructible_v<std::string_view, char*, const char*>);
79 static_assert(!std::is_constructible_v<std::string_view, char*, void*>);               // not a sentinel
80 static_assert(!std::is_constructible_v<std::string_view, signed char*, signed char*>); // wrong char type
81 static_assert(!std::is_constructible_v<std::string_view, random_access_iterator<char*>, random_access_iterator<char*>>); // not contiguous
82 static_assert( std::is_constructible_v<std::string_view, contiguous_iterator<char*>, contiguous_iterator<char*>>);
83 
84 int main(int, char**) {
85   test();
86   static_assert(test());
87 
88 #ifndef TEST_HAS_NO_EXCEPTIONS
89   test_throwing();
90 #endif
91 
92   return 0;
93 }
94 
95