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