xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.cons/from_ptr_len.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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 
9 // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
10 
11 // <string_view>
12 
13 //  constexpr basic_string_view(const _CharT* _s, size_type _len)
14 //      : __data (_s), __size(_len) {}
15 
16 #include <string_view>
17 #include <string>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 
22 template <typename CharT>
test(const CharT * s,std::size_t sz)23 void test(const CharT* s, std::size_t sz) {
24   {
25     typedef std::basic_string_view<CharT> SV;
26     LIBCPP_ASSERT_NOEXCEPT(SV(s, sz));
27 
28     SV sv1(s, sz);
29     assert(sv1.size() == sz);
30     assert(sv1.data() == s);
31   }
32 }
33 
main(int,char **)34 int main(int, char**) {
35   test("QBCDE", 5);
36   test("QBCDE", 2);
37   test("", 0);
38 #if TEST_STD_VER > 11
39   {
40     constexpr const char* s = "QBCDE";
41     constexpr std::basic_string_view<char> sv1(s, 2);
42     static_assert(sv1.size() == 2, "");
43     static_assert(sv1.data() == s, "");
44   }
45 #endif
46 
47 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
48   test(L"QBCDE", 5);
49   test(L"QBCDE", 2);
50   test(L"", 0);
51 #  if TEST_STD_VER > 11
52   {
53     constexpr const wchar_t* s = L"QBCDE";
54     constexpr std::basic_string_view<wchar_t> sv1(s, 2);
55     static_assert(sv1.size() == 2, "");
56     static_assert(sv1.data() == s, "");
57   }
58 #  endif
59 #endif // TEST_HAS_NO_WIDE_CHARACTERS
60 
61 #if TEST_STD_VER >= 11
62   test(u"QBCDE", 5);
63   test(u"QBCDE", 2);
64   test(u"", 0);
65 #  if TEST_STD_VER > 11
66   {
67     constexpr const char16_t* s = u"QBCDE";
68     constexpr std::basic_string_view<char16_t> sv1(s, 2);
69     static_assert(sv1.size() == 2, "");
70     static_assert(sv1.data() == s, "");
71   }
72 #  endif
73 
74   test(U"QBCDE", 5);
75   test(U"QBCDE", 2);
76   test(U"", 0);
77 #  if TEST_STD_VER > 11
78   {
79     constexpr const char32_t* s = U"QBCDE";
80     constexpr std::basic_string_view<char32_t> sv1(s, 2);
81     static_assert(sv1.size() == 2, "");
82     static_assert(sv1.data() == s, "");
83   }
84 #  endif
85 #endif
86 
87   return 0;
88 }
89