1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <string>
11
12 // basic_string(const basic_string<charT,traits,Allocator>& str,
13 // size_type pos, size_type n = npos,
14 // const Allocator& a = Allocator());
15
16 #include <string>
17 #include <stdexcept>
18 #include <algorithm>
19 #include <cassert>
20
21 #include "test_allocator.h"
22 #include "min_allocator.h"
23
24 template <class S>
25 void
test(S str,unsigned pos)26 test(S str, unsigned pos)
27 {
28 typedef typename S::traits_type T;
29 typedef typename S::allocator_type A;
30 try
31 {
32 S s2(str, pos);
33 assert(s2.__invariants());
34 assert(pos <= str.size());
35 unsigned rlen = str.size() - pos;
36 assert(s2.size() == rlen);
37 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
38 assert(s2.get_allocator() == A());
39 assert(s2.capacity() >= s2.size());
40 }
41 catch (std::out_of_range&)
42 {
43 assert(pos > str.size());
44 }
45 }
46
47 template <class S>
48 void
test(S str,unsigned pos,unsigned n)49 test(S str, unsigned pos, unsigned n)
50 {
51 typedef typename S::traits_type T;
52 typedef typename S::allocator_type A;
53 try
54 {
55 S s2(str, pos, n);
56 assert(s2.__invariants());
57 assert(pos <= str.size());
58 unsigned rlen = std::min<unsigned>(str.size() - pos, n);
59 assert(s2.size() == rlen);
60 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
61 assert(s2.get_allocator() == A());
62 assert(s2.capacity() >= s2.size());
63 }
64 catch (std::out_of_range&)
65 {
66 assert(pos > str.size());
67 }
68 }
69
70 template <class S>
71 void
test(S str,unsigned pos,unsigned n,const typename S::allocator_type & a)72 test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
73 {
74 typedef typename S::traits_type T;
75 typedef typename S::allocator_type A;
76 try
77 {
78 S s2(str, pos, n, a);
79 assert(s2.__invariants());
80 assert(pos <= str.size());
81 unsigned rlen = std::min<unsigned>(str.size() - pos, n);
82 assert(s2.size() == rlen);
83 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
84 assert(s2.get_allocator() == a);
85 assert(s2.capacity() >= s2.size());
86 }
87 catch (std::out_of_range&)
88 {
89 assert(pos > str.size());
90 }
91 }
92
main()93 int main()
94 {
95 {
96 typedef test_allocator<char> A;
97 typedef std::basic_string<char, std::char_traits<char>, A> S;
98
99 test(S(A(3)), 0);
100 test(S(A(3)), 1);
101 test(S("1", A(5)), 0);
102 test(S("1", A(5)), 1);
103 test(S("1", A(5)), 2);
104 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0);
105 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5);
106 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50);
107 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500);
108
109 test(S(A(3)), 0, 0);
110 test(S(A(3)), 0, 1);
111 test(S(A(3)), 1, 0);
112 test(S(A(3)), 1, 1);
113 test(S(A(3)), 1, 2);
114 test(S("1", A(5)), 0, 0);
115 test(S("1", A(5)), 0, 1);
116 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0);
117 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1);
118 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10);
119 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100);
120
121 test(S(A(3)), 0, 0, A(4));
122 test(S(A(3)), 0, 1, A(4));
123 test(S(A(3)), 1, 0, A(4));
124 test(S(A(3)), 1, 1, A(4));
125 test(S(A(3)), 1, 2, A(4));
126 test(S("1", A(5)), 0, 0, A(6));
127 test(S("1", A(5)), 0, 1, A(6));
128 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8));
129 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8));
130 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8));
131 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8));
132 }
133 #if __cplusplus >= 201103L
134 {
135 typedef min_allocator<char> A;
136 typedef std::basic_string<char, std::char_traits<char>, A> S;
137
138 test(S(A()), 0);
139 test(S(A()), 1);
140 test(S("1", A()), 0);
141 test(S("1", A()), 1);
142 test(S("1", A()), 2);
143 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0);
144 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5);
145 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50);
146 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500);
147
148 test(S(A()), 0, 0);
149 test(S(A()), 0, 1);
150 test(S(A()), 1, 0);
151 test(S(A()), 1, 1);
152 test(S(A()), 1, 2);
153 test(S("1", A()), 0, 0);
154 test(S("1", A()), 0, 1);
155 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0);
156 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1);
157 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10);
158 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100);
159
160 test(S(A()), 0, 0, A());
161 test(S(A()), 0, 1, A());
162 test(S(A()), 1, 0, A());
163 test(S(A()), 1, 1, A());
164 test(S(A()), 1, 2, A());
165 test(S("1", A()), 0, 0, A());
166 test(S("1", A()), 0, 1, A());
167 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A());
168 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A());
169 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A());
170 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
171 }
172 #endif
173 }
174