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(size_type n, charT c, 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(unsigned n,charT c)25 test(unsigned n, charT c)
26 {
27 typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
28 typedef typename S::allocator_type A;
29 S s2(n, c);
30 LIBCPP_ASSERT(s2.__invariants());
31 assert(s2.size() == n);
32 for (unsigned i = 0; i < n; ++i)
33 assert(s2[i] == c);
34 assert(s2.get_allocator() == A());
35 assert(s2.capacity() >= s2.size());
36 }
37
38 template <class charT, class A>
39 TEST_CONSTEXPR_CXX20 void
test(unsigned n,charT c,const A & a)40 test(unsigned n, charT c, const A& a)
41 {
42 typedef std::basic_string<charT, std::char_traits<charT>, A> S;
43 S s2(n, c, a);
44 LIBCPP_ASSERT(s2.__invariants());
45 assert(s2.size() == n);
46 for (unsigned i = 0; i < n; ++i)
47 assert(s2[i] == c);
48 assert(s2.get_allocator() == a);
49 assert(s2.capacity() >= s2.size());
50 }
51
52 template <class Tp>
53 TEST_CONSTEXPR_CXX20 void
test(Tp n,Tp c)54 test(Tp n, Tp c)
55 {
56 typedef char charT;
57 typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
58 typedef typename S::allocator_type A;
59 S s2(n, c);
60 LIBCPP_ASSERT(s2.__invariants());
61 assert(s2.size() == static_cast<std::size_t>(n));
62 for (int i = 0; i < n; ++i)
63 assert(s2[i] == c);
64 assert(s2.get_allocator() == A());
65 assert(s2.capacity() >= s2.size());
66 }
67
68 template <class Tp, class A>
69 TEST_CONSTEXPR_CXX20 void
test(Tp n,Tp c,const A & a)70 test(Tp n, Tp c, const A& a)
71 {
72 typedef char charT;
73 typedef std::basic_string<charT, std::char_traits<charT>, A> S;
74 S s2(n, c, a);
75 LIBCPP_ASSERT(s2.__invariants());
76 assert(s2.size() == static_cast<std::size_t>(n));
77 for (int i = 0; i < n; ++i)
78 assert(s2[i] == c);
79 assert(s2.get_allocator() == a);
80 assert(s2.capacity() >= s2.size());
81 }
82
test()83 TEST_CONSTEXPR_CXX20 bool test() {
84 {
85 typedef test_allocator<char> A;
86
87 test(0, 'a');
88 test(0, 'a', A(2));
89
90 test(1, 'a');
91 test(1, 'a', A(2));
92
93 test(10, 'a');
94 test(10, 'a', A(2));
95
96 test(100, 'a');
97 test(100, 'a', A(2));
98
99 test(static_cast<char>(100), static_cast<char>(65));
100 test(static_cast<char>(100), static_cast<char>(65), A(3));
101 }
102 #if TEST_STD_VER >= 11
103 {
104 typedef min_allocator<char> A;
105
106 test(0, 'a');
107 test(0, 'a', A());
108
109 test(1, 'a');
110 test(1, 'a', A());
111
112 test(10, 'a');
113 test(10, 'a', A());
114
115 test(100, 'a');
116 test(100, 'a', A());
117
118 test(static_cast<char>(100), static_cast<char>(65));
119 test(static_cast<char>(100), static_cast<char>(65), A());
120 }
121 #endif
122
123 return true;
124 }
125
main(int,char **)126 int main(int, char**)
127 {
128 test();
129 #if TEST_STD_VER > 17
130 static_assert(test());
131 #endif
132
133 return 0;
134 }
135