xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.cons/from_iterator_sentinel.pass.cpp (revision f4c1258d5633fcf06385ff3fd1f4bf57ab971964)
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 <ranges>
20 
21 #include "make_string.h"
22 #include "test_iterators.h"
23 
24 template<class CharT, class Sentinel>
25 constexpr void test() {
26   auto val = MAKE_STRING_VIEW(CharT, "test");
27   auto sv = std::basic_string_view<CharT>(val.begin(), Sentinel(val.end()));
28   ASSERT_SAME_TYPE(decltype(sv), std::basic_string_view<CharT>);
29   assert(sv.size() == val.size());
30   assert(sv.data() == val.data());
31 }
32 
33 constexpr bool test() {
34   test<char, char*>();
35 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
36   test<wchar_t, wchar_t*>();
37 #endif
38   test<char8_t, char8_t*>();
39   test<char16_t, char16_t*>();
40   test<char32_t, char32_t*>();
41   test<char, const char*>();
42   test<char, sized_sentinel<const char*>>();
43   return true;
44 }
45 
46 static_assert( std::is_constructible_v<std::string_view, const char*, char*>);
47 static_assert( std::is_constructible_v<std::string_view, char*, const char*>);
48 static_assert(!std::is_constructible_v<std::string_view, char*, void*>);               // not a sentinel
49 static_assert(!std::is_constructible_v<std::string_view, signed char*, signed char*>); // wrong char type
50 static_assert(!std::is_constructible_v<std::string_view, random_access_iterator<char*>, random_access_iterator<char*>>); // not contiguous
51 static_assert( std::is_constructible_v<std::string_view, contiguous_iterator<char*>, contiguous_iterator<char*>>);
52 
53 int main(int, char**) {
54   test();
55   static_assert(test());
56 
57   return 0;
58 }
59 
60