Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <string> |
| 10 | |
Howard Hinnant | bf2897c | 2010-08-22 00:47:54 +0000 | [diff] [blame] | 11 | // basic_string(const basic_string<charT,traits,Allocator>& str, |
Marshall Clow | 89685ed | 2016-04-07 18:13:41 +0000 | [diff] [blame] | 12 | // size_type pos, size_type n, |
| 13 | // const Allocator& a = Allocator()); |
| 14 | // |
| 15 | // basic_string(const basic_string<charT,traits,Allocator>& str, |
| 16 | // size_type pos, |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 17 | // const Allocator& a = Allocator()); |
| 18 | |
Louis Dionne | 25838c6 | 2019-02-27 00:57:57 +0000 | [diff] [blame] | 19 | // When back-deploying to macosx10.7, the RTTI for exception classes |
| 20 | // incorrectly provided by libc++.dylib is mixed with the one in |
| 21 | // libc++abi.dylib and exceptions are not caught properly. |
| 22 | // XFAIL: with_system_cxx_lib=macosx10.7 |
| 23 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <stdexcept> |
| 26 | #include <algorithm> |
Marshall Clow | 89685ed | 2016-04-07 18:13:41 +0000 | [diff] [blame] | 27 | #include <vector> |
| 28 | #include <scoped_allocator> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 29 | #include <cassert> |
| 30 | |
Marshall Clow | 89685ed | 2016-04-07 18:13:41 +0000 | [diff] [blame] | 31 | #include "test_macros.h" |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 32 | #include "test_allocator.h" |
Marshall Clow | e34f6f6 | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 33 | #include "min_allocator.h" |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 34 | |
| 35 | template <class S> |
| 36 | void |
| 37 | test(S str, unsigned pos) |
| 38 | { |
| 39 | typedef typename S::traits_type T; |
| 40 | typedef typename S::allocator_type A; |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 41 | |
| 42 | if (pos <= str.size()) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | { |
| 44 | S s2(str, pos); |
Eric Fiselier | 1f4231f | 2016-04-28 22:28:23 +0000 | [diff] [blame] | 45 | LIBCPP_ASSERT(s2.__invariants()); |
Stephan T. Lavavej | d4b83e6 | 2016-12-06 01:14:51 +0000 | [diff] [blame] | 46 | typename S::size_type rlen = str.size() - pos; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 | assert(s2.size() == rlen); |
| 48 | assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); |
| 49 | assert(s2.get_allocator() == A()); |
| 50 | assert(s2.capacity() >= s2.size()); |
| 51 | } |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 52 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 53 | else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | { |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 55 | try |
| 56 | { |
| 57 | S s2(str, pos); |
| 58 | assert(false); |
| 59 | } |
| 60 | catch (std::out_of_range&) |
| 61 | { |
| 62 | assert(pos > str.size()); |
| 63 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 64 | } |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 65 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | template <class S> |
| 69 | void |
| 70 | test(S str, unsigned pos, unsigned n) |
| 71 | { |
| 72 | typedef typename S::traits_type T; |
| 73 | typedef typename S::allocator_type A; |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 74 | if (pos <= str.size()) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 75 | { |
| 76 | S s2(str, pos, n); |
Eric Fiselier | 1f4231f | 2016-04-28 22:28:23 +0000 | [diff] [blame] | 77 | LIBCPP_ASSERT(s2.__invariants()); |
Stephan T. Lavavej | d4b83e6 | 2016-12-06 01:14:51 +0000 | [diff] [blame] | 78 | typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 79 | assert(s2.size() == rlen); |
| 80 | assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); |
| 81 | assert(s2.get_allocator() == A()); |
| 82 | assert(s2.capacity() >= s2.size()); |
| 83 | } |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 84 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 85 | else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 86 | { |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 87 | try |
| 88 | { |
| 89 | S s2(str, pos, n); |
| 90 | assert(false); |
| 91 | } |
| 92 | catch (std::out_of_range&) |
| 93 | { |
| 94 | assert(pos > str.size()); |
| 95 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 96 | } |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 97 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | template <class S> |
| 101 | void |
| 102 | test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a) |
| 103 | { |
| 104 | typedef typename S::traits_type T; |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 105 | |
| 106 | if (pos <= str.size()) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 107 | { |
| 108 | S s2(str, pos, n, a); |
Eric Fiselier | 1f4231f | 2016-04-28 22:28:23 +0000 | [diff] [blame] | 109 | LIBCPP_ASSERT(s2.__invariants()); |
Stephan T. Lavavej | d4b83e6 | 2016-12-06 01:14:51 +0000 | [diff] [blame] | 110 | typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 111 | assert(s2.size() == rlen); |
| 112 | assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); |
| 113 | assert(s2.get_allocator() == a); |
| 114 | assert(s2.capacity() >= s2.size()); |
| 115 | } |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 116 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 117 | else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 118 | { |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 119 | try |
| 120 | { |
| 121 | S s2(str, pos, n, a); |
| 122 | assert(false); |
| 123 | } |
| 124 | catch (std::out_of_range&) |
| 125 | { |
| 126 | assert(pos > str.size()); |
| 127 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 128 | } |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 129 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Marshall Clow | 89685ed | 2016-04-07 18:13:41 +0000 | [diff] [blame] | 132 | #if TEST_STD_VER >= 11 |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 133 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 89685ed | 2016-04-07 18:13:41 +0000 | [diff] [blame] | 134 | void test2583() |
| 135 | { // LWG #2583 |
| 136 | typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA; |
| 137 | std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs; |
| 138 | StringA s{"1234"}; |
| 139 | vs.emplace_back(s, 2); |
| 140 | |
| 141 | try { vs.emplace_back(s, 5); } |
| 142 | catch (const std::out_of_range&) { return; } |
| 143 | assert(false); |
| 144 | } |
| 145 | #endif |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 146 | #endif |
Marshall Clow | 89685ed | 2016-04-07 18:13:41 +0000 | [diff] [blame] | 147 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 148 | int main(int, char**) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 149 | { |
Howard Hinnant | eec7218 | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 150 | { |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 151 | typedef test_allocator<char> A; |
| 152 | typedef std::basic_string<char, std::char_traits<char>, A> S; |
| 153 | |
| 154 | test(S(A(3)), 0); |
| 155 | test(S(A(3)), 1); |
| 156 | test(S("1", A(5)), 0); |
| 157 | test(S("1", A(5)), 1); |
| 158 | test(S("1", A(5)), 2); |
| 159 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0); |
| 160 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5); |
| 161 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50); |
| 162 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500); |
| 163 | |
| 164 | test(S(A(3)), 0, 0); |
| 165 | test(S(A(3)), 0, 1); |
| 166 | test(S(A(3)), 1, 0); |
| 167 | test(S(A(3)), 1, 1); |
| 168 | test(S(A(3)), 1, 2); |
| 169 | test(S("1", A(5)), 0, 0); |
| 170 | test(S("1", A(5)), 0, 1); |
| 171 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0); |
| 172 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1); |
| 173 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10); |
| 174 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100); |
| 175 | |
| 176 | test(S(A(3)), 0, 0, A(4)); |
| 177 | test(S(A(3)), 0, 1, A(4)); |
| 178 | test(S(A(3)), 1, 0, A(4)); |
| 179 | test(S(A(3)), 1, 1, A(4)); |
| 180 | test(S(A(3)), 1, 2, A(4)); |
| 181 | test(S("1", A(5)), 0, 0, A(6)); |
| 182 | test(S("1", A(5)), 0, 1, A(6)); |
| 183 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8)); |
| 184 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8)); |
| 185 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8)); |
| 186 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8)); |
Howard Hinnant | eec7218 | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 187 | } |
Marshall Clow | 89685ed | 2016-04-07 18:13:41 +0000 | [diff] [blame] | 188 | #if TEST_STD_VER >= 11 |
Howard Hinnant | eec7218 | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 189 | { |
| 190 | typedef min_allocator<char> A; |
| 191 | typedef std::basic_string<char, std::char_traits<char>, A> S; |
| 192 | |
| 193 | test(S(A()), 0); |
| 194 | test(S(A()), 1); |
| 195 | test(S("1", A()), 0); |
| 196 | test(S("1", A()), 1); |
| 197 | test(S("1", A()), 2); |
| 198 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0); |
| 199 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5); |
| 200 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50); |
| 201 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500); |
| 202 | |
| 203 | test(S(A()), 0, 0); |
| 204 | test(S(A()), 0, 1); |
| 205 | test(S(A()), 1, 0); |
| 206 | test(S(A()), 1, 1); |
| 207 | test(S(A()), 1, 2); |
| 208 | test(S("1", A()), 0, 0); |
| 209 | test(S("1", A()), 0, 1); |
| 210 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0); |
| 211 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1); |
| 212 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10); |
| 213 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100); |
| 214 | |
| 215 | test(S(A()), 0, 0, A()); |
| 216 | test(S(A()), 0, 1, A()); |
| 217 | test(S(A()), 1, 0, A()); |
| 218 | test(S(A()), 1, 1, A()); |
| 219 | test(S(A()), 1, 2, A()); |
| 220 | test(S("1", A()), 0, 0, A()); |
| 221 | test(S("1", A()), 0, 1, A()); |
| 222 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A()); |
| 223 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A()); |
| 224 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A()); |
| 225 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A()); |
| 226 | } |
Marshall Clow | 89685ed | 2016-04-07 18:13:41 +0000 | [diff] [blame] | 227 | |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 228 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 89685ed | 2016-04-07 18:13:41 +0000 | [diff] [blame] | 229 | test2583(); |
Howard Hinnant | eec7218 | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 230 | #endif |
Roger Ferrer Ibanez | 8a915ed | 2016-11-01 15:46:16 +0000 | [diff] [blame] | 231 | #endif |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 232 | |
| 233 | return 0; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 234 | } |