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