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());
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 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 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 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 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
main(int,char **)83 int main(int, char**)
84 {
85 {
86 typedef test_allocator<char> A;
87
88 test(0, 'a');
89 test(0, 'a', A(2));
90
91 test(1, 'a');
92 test(1, 'a', A(2));
93
94 test(10, 'a');
95 test(10, 'a', A(2));
96
97 test(100, 'a');
98 test(100, 'a', A(2));
99
100 test(static_cast<char>(100), static_cast<char>(65));
101 test(static_cast<char>(100), static_cast<char>(65), A(3));
102 }
103 #if TEST_STD_VER >= 11
104 {
105 typedef min_allocator<char> A;
106
107 test(0, 'a');
108 test(0, 'a', A());
109
110 test(1, 'a');
111 test(1, 'a', A());
112
113 test(10, 'a');
114 test(10, 'a', A());
115
116 test(100, 'a');
117 test(100, 'a', A());
118
119 test(static_cast<char>(100), static_cast<char>(65));
120 test(static_cast<char>(100), static_cast<char>(65), A());
121 }
122 #endif
123
124 return 0;
125 }
126