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