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