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 // <string> 10 11 // explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator()); // constexpr since C++20 12 13 #include <algorithm> 14 #include <cassert> 15 #include <stdexcept> 16 #include <string_view> 17 #include <string> 18 #include <type_traits> 19 20 #include "min_allocator.h" 21 #include "test_allocator.h" 22 #include "test_macros.h" 23 24 static_assert(!std::is_convertible<std::string_view, std::string const&>::value, ""); 25 static_assert(!std::is_convertible<std::string_view, std::string>::value, ""); 26 27 template <class charT> 28 TEST_CONSTEXPR_CXX20 void 29 test(std::basic_string_view<charT> sv) 30 { 31 typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S; 32 typedef typename S::traits_type T; 33 typedef typename S::allocator_type A; 34 { 35 S s2(sv); 36 LIBCPP_ASSERT(s2.__invariants()); 37 assert(s2.size() == sv.size()); 38 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0); 39 assert(s2.get_allocator() == A()); 40 assert(s2.capacity() >= s2.size()); 41 } 42 { 43 S s2; 44 s2 = sv; 45 LIBCPP_ASSERT(s2.__invariants()); 46 assert(s2.size() == sv.size()); 47 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0); 48 assert(s2.get_allocator() == A()); 49 assert(s2.capacity() >= s2.size()); 50 } 51 } 52 53 template <class charT, class A> 54 TEST_CONSTEXPR_CXX20 void 55 test(std::basic_string_view<charT> sv, const A& a) 56 { 57 typedef std::basic_string<charT, std::char_traits<charT>, A> S; 58 typedef typename S::traits_type T; 59 { 60 S s2(sv, a); 61 LIBCPP_ASSERT(s2.__invariants()); 62 assert(s2.size() == sv.size()); 63 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0); 64 assert(s2.get_allocator() == a); 65 assert(s2.capacity() >= s2.size()); 66 } 67 { 68 S s2(a); 69 s2 = sv; 70 LIBCPP_ASSERT(s2.__invariants()); 71 assert(s2.size() == sv.size()); 72 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0); 73 assert(s2.get_allocator() == a); 74 assert(s2.capacity() >= s2.size()); 75 } 76 } 77 78 TEST_CONSTEXPR_CXX20 bool test() { 79 { 80 typedef test_allocator<char> A; 81 typedef std::basic_string_view<char, std::char_traits<char> > SV; 82 83 test(SV("")); 84 test(SV(""), A(2)); 85 86 test(SV("1")); 87 test(SV("1") ,A(2)); 88 89 test(SV("1234567980")); 90 test(SV("1234567980"), A(2)); 91 92 test(SV("123456798012345679801234567980123456798012345679801234567980")); 93 test(SV("123456798012345679801234567980123456798012345679801234567980"), A(2)); 94 } 95 #if TEST_STD_VER >= 11 96 { 97 typedef min_allocator<char> A; 98 typedef std::basic_string_view<char, std::char_traits<char> > SV; 99 100 test(SV("")); 101 test(SV(""), A()); 102 103 test(SV("1")); 104 test(SV("1") ,A()); 105 106 test(SV("1234567980")); 107 test(SV("1234567980"), A()); 108 109 test(SV("123456798012345679801234567980123456798012345679801234567980")); 110 test(SV("123456798012345679801234567980123456798012345679801234567980"), A()); 111 } 112 #endif 113 114 return true; 115 } 116 117 int main(int, char**) 118 { 119 test(); 120 #if TEST_STD_VER > 17 121 static_assert(test()); 122 #endif 123 124 return 0; 125 } 126