xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.cons/from_literal.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)
14 //    : __data (_s), __size(_Traits::length(_s)) {}
15 
16 #include <string_view>
17 #include <string>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "constexpr_char_traits.h"
22 
23 template <typename CharT>
StrLen(const CharT * s)24 size_t StrLen(const CharT* s) {
25   std::size_t retVal = 0;
26   while (*s != 0) {
27     ++retVal;
28     ++s;
29   }
30   return retVal;
31 }
32 
33 template <typename CharT>
test(const CharT * s)34 void test(const CharT* s) {
35   typedef std::basic_string_view<CharT> SV;
36   //  I'd love to do this, but it would require traits::length() to be noexcept
37   //  LIBCPP_ASSERT_NOEXCEPT(SV(s));
38 
39   SV sv1(s);
40   assert(sv1.size() == StrLen(s));
41   assert(sv1.data() == s);
42 }
43 
main(int,char **)44 int main(int, char**) {
45   test("QBCDE");
46   test("A");
47   test("");
48 
49 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
50   test(L"QBCDE");
51   test(L"A");
52   test(L"");
53 #endif
54 
55 #if TEST_STD_VER >= 11
56   test(u"QBCDE");
57   test(u"A");
58   test(u"");
59 
60   test(U"QBCDE");
61   test(U"A");
62   test(U"");
63 #endif
64 
65 #if TEST_STD_VER > 11
66   {
67     constexpr std::basic_string_view<char, constexpr_char_traits<char>> sv1("ABCDE");
68     static_assert(sv1.size() == 5, "");
69   }
70 #endif
71 
72   return 0;
73 }
74