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 // basic_string(const charT* s, const Allocator& a = Allocator()); // constexpr since C++20 12 13 #include <string> 14 #include <stdexcept> 15 #include <algorithm> 16 #include <cassert> 17 #include <cstddef> 18 19 #include "test_macros.h" 20 #include "test_allocator.h" 21 #include "min_allocator.h" 22 23 template <class charT> 24 TEST_CONSTEXPR_CXX20 void test(const charT* s) { 25 typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S; 26 typedef typename S::traits_type T; 27 typedef typename S::allocator_type A; 28 std::size_t n = T::length(s); 29 S s2(s); 30 LIBCPP_ASSERT(s2.__invariants()); 31 assert(s2.size() == n); 32 assert(T::compare(s2.data(), s, n) == 0); 33 assert(s2.get_allocator() == A()); 34 assert(s2.capacity() >= s2.size()); 35 } 36 37 template <class charT, class A> 38 TEST_CONSTEXPR_CXX20 void test(const charT* s, const A& a) { 39 typedef std::basic_string<charT, std::char_traits<charT>, A> S; 40 typedef typename S::traits_type T; 41 std::size_t n = T::length(s); 42 S s2(s, a); 43 LIBCPP_ASSERT(s2.__invariants()); 44 assert(s2.size() == n); 45 assert(T::compare(s2.data(), s, n) == 0); 46 assert(s2.get_allocator() == a); 47 assert(s2.capacity() >= s2.size()); 48 } 49 50 TEST_CONSTEXPR_CXX20 bool test() { 51 { 52 typedef test_allocator<char> A; 53 54 test(""); 55 test("", A(2)); 56 57 test("1"); 58 test("1", A(2)); 59 60 test("1234567980"); 61 test("1234567980", A(2)); 62 63 test("123456798012345679801234567980123456798012345679801234567980"); 64 test("123456798012345679801234567980123456798012345679801234567980", A(2)); 65 } 66 #if TEST_STD_VER >= 11 67 { 68 typedef min_allocator<char> A; 69 70 test(""); 71 test("", A()); 72 73 test("1"); 74 test("1", A()); 75 76 test("1234567980"); 77 test("1234567980", A()); 78 79 test("123456798012345679801234567980123456798012345679801234567980"); 80 test("123456798012345679801234567980123456798012345679801234567980", A()); 81 } 82 #endif 83 84 return true; 85 } 86 87 int main(int, char**) { 88 test(); 89 #if TEST_STD_VER > 17 90 static_assert(test()); 91 #endif 92 93 return 0; 94 } 95