15a83710eSEric Fiselier //===----------------------------------------------------------------------===// 25a83710eSEric Fiselier // 357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65a83710eSEric Fiselier // 75a83710eSEric Fiselier //===----------------------------------------------------------------------===// 85a83710eSEric Fiselier 9*425620ccSNikolas Klauser // XFAIL: LIBCXX-AIX-FIXME 10*425620ccSNikolas Klauser 115a83710eSEric Fiselier // <string> 125a83710eSEric Fiselier 135a83710eSEric Fiselier // basic_string(const basic_string<charT,traits,Allocator>& str, 1489685ed0SMarshall Clow // size_type pos, size_type n, 15*425620ccSNikolas Klauser // const Allocator& a = Allocator()); // constexpr since C++20 1689685ed0SMarshall Clow // 1789685ed0SMarshall Clow // basic_string(const basic_string<charT,traits,Allocator>& str, 1889685ed0SMarshall Clow // size_type pos, 19*425620ccSNikolas Klauser // const Allocator& a = Allocator()); // constexpr since C++20 205a83710eSEric Fiselier 215a83710eSEric Fiselier #include <string> 225a83710eSEric Fiselier #include <stdexcept> 235a83710eSEric Fiselier #include <algorithm> 2489685ed0SMarshall Clow #include <vector> 2589685ed0SMarshall Clow #include <scoped_allocator> 265a83710eSEric Fiselier #include <cassert> 275a83710eSEric Fiselier 2889685ed0SMarshall Clow #include "test_macros.h" 295a83710eSEric Fiselier #include "test_allocator.h" 305a83710eSEric Fiselier #include "min_allocator.h" 315a83710eSEric Fiselier 325a83710eSEric Fiselier template <class S> 33e85018b7SNikolas Klauser TEST_CONSTEXPR_CXX20 void 345a83710eSEric Fiselier test(S str, unsigned pos) 355a83710eSEric Fiselier { 365a83710eSEric Fiselier typedef typename S::traits_type T; 375a83710eSEric Fiselier typedef typename S::allocator_type A; 388a915ed6SRoger Ferrer Ibanez 398a915ed6SRoger Ferrer Ibanez if (pos <= str.size()) 405a83710eSEric Fiselier { 415a83710eSEric Fiselier S s2(str, pos); 421f4231f8SEric Fiselier LIBCPP_ASSERT(s2.__invariants()); 43d4b83e6dSStephan T. Lavavej typename S::size_type rlen = str.size() - pos; 445a83710eSEric Fiselier assert(s2.size() == rlen); 455a83710eSEric Fiselier assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); 465a83710eSEric Fiselier assert(s2.get_allocator() == A()); 475a83710eSEric Fiselier assert(s2.capacity() >= s2.size()); 485a83710eSEric Fiselier } 498a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS 5085e9b268SNikolas Klauser else if (!TEST_IS_CONSTANT_EVALUATED) 518a915ed6SRoger Ferrer Ibanez { 528a915ed6SRoger Ferrer Ibanez try 538a915ed6SRoger Ferrer Ibanez { 548a915ed6SRoger Ferrer Ibanez S s2(str, pos); 558a915ed6SRoger Ferrer Ibanez assert(false); 568a915ed6SRoger Ferrer Ibanez } 575a83710eSEric Fiselier catch (std::out_of_range&) 585a83710eSEric Fiselier { 595a83710eSEric Fiselier assert(pos > str.size()); 605a83710eSEric Fiselier } 615a83710eSEric Fiselier } 628a915ed6SRoger Ferrer Ibanez #endif 638a915ed6SRoger Ferrer Ibanez } 645a83710eSEric Fiselier 655a83710eSEric Fiselier template <class S> 66e85018b7SNikolas Klauser TEST_CONSTEXPR_CXX20 void 675a83710eSEric Fiselier test(S str, unsigned pos, unsigned n) 685a83710eSEric Fiselier { 695a83710eSEric Fiselier typedef typename S::traits_type T; 705a83710eSEric Fiselier typedef typename S::allocator_type A; 718a915ed6SRoger Ferrer Ibanez if (pos <= str.size()) 725a83710eSEric Fiselier { 735a83710eSEric Fiselier S s2(str, pos, n); 741f4231f8SEric Fiselier LIBCPP_ASSERT(s2.__invariants()); 75d4b83e6dSStephan T. Lavavej typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n); 765a83710eSEric Fiselier assert(s2.size() == rlen); 775a83710eSEric Fiselier assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); 785a83710eSEric Fiselier assert(s2.get_allocator() == A()); 795a83710eSEric Fiselier assert(s2.capacity() >= s2.size()); 805a83710eSEric Fiselier } 818a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS 8285e9b268SNikolas Klauser else if (!TEST_IS_CONSTANT_EVALUATED) 838a915ed6SRoger Ferrer Ibanez { 848a915ed6SRoger Ferrer Ibanez try 858a915ed6SRoger Ferrer Ibanez { 868a915ed6SRoger Ferrer Ibanez S s2(str, pos, n); 878a915ed6SRoger Ferrer Ibanez assert(false); 888a915ed6SRoger Ferrer Ibanez } 895a83710eSEric Fiselier catch (std::out_of_range&) 905a83710eSEric Fiselier { 915a83710eSEric Fiselier assert(pos > str.size()); 925a83710eSEric Fiselier } 935a83710eSEric Fiselier } 948a915ed6SRoger Ferrer Ibanez #endif 958a915ed6SRoger Ferrer Ibanez } 965a83710eSEric Fiselier 975a83710eSEric Fiselier template <class S> 98e85018b7SNikolas Klauser TEST_CONSTEXPR_CXX20 void 995a83710eSEric Fiselier test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a) 1005a83710eSEric Fiselier { 1015a83710eSEric Fiselier typedef typename S::traits_type T; 1028a915ed6SRoger Ferrer Ibanez 1038a915ed6SRoger Ferrer Ibanez if (pos <= str.size()) 1045a83710eSEric Fiselier { 1055a83710eSEric Fiselier S s2(str, pos, n, a); 1061f4231f8SEric Fiselier LIBCPP_ASSERT(s2.__invariants()); 107d4b83e6dSStephan T. Lavavej typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n); 1085a83710eSEric Fiselier assert(s2.size() == rlen); 1095a83710eSEric Fiselier assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); 1105a83710eSEric Fiselier assert(s2.get_allocator() == a); 1115a83710eSEric Fiselier assert(s2.capacity() >= s2.size()); 1125a83710eSEric Fiselier } 1138a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS 11485e9b268SNikolas Klauser else if (!TEST_IS_CONSTANT_EVALUATED) 1158a915ed6SRoger Ferrer Ibanez { 1168a915ed6SRoger Ferrer Ibanez try 1178a915ed6SRoger Ferrer Ibanez { 1188a915ed6SRoger Ferrer Ibanez S s2(str, pos, n, a); 1198a915ed6SRoger Ferrer Ibanez assert(false); 1208a915ed6SRoger Ferrer Ibanez } 1215a83710eSEric Fiselier catch (std::out_of_range&) 1225a83710eSEric Fiselier { 1235a83710eSEric Fiselier assert(pos > str.size()); 1245a83710eSEric Fiselier } 1255a83710eSEric Fiselier } 1268a915ed6SRoger Ferrer Ibanez #endif 1278a915ed6SRoger Ferrer Ibanez } 1285a83710eSEric Fiselier 12985e9b268SNikolas Klauser void test_lwg2583() 13085e9b268SNikolas Klauser { 13185e9b268SNikolas Klauser #if TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS) 13289685ed0SMarshall Clow typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> StringA; 13389685ed0SMarshall Clow std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs; 13489685ed0SMarshall Clow StringA s{"1234"}; 13589685ed0SMarshall Clow vs.emplace_back(s, 2); 13689685ed0SMarshall Clow 13789685ed0SMarshall Clow try { vs.emplace_back(s, 5); } 13889685ed0SMarshall Clow catch (const std::out_of_range&) { return; } 13989685ed0SMarshall Clow assert(false); 14085e9b268SNikolas Klauser #endif 14189685ed0SMarshall Clow } 14289685ed0SMarshall Clow 143*425620ccSNikolas Klauser TEST_CONSTEXPR_CXX20 bool test() { 1445a83710eSEric Fiselier { 1455a83710eSEric Fiselier typedef test_allocator<char> A; 1465a83710eSEric Fiselier typedef std::basic_string<char, std::char_traits<char>, A> S; 1475a83710eSEric Fiselier 1485a83710eSEric Fiselier test(S(A(3)), 0); 1495a83710eSEric Fiselier test(S(A(3)), 1); 1505a83710eSEric Fiselier test(S("1", A(5)), 0); 1515a83710eSEric Fiselier test(S("1", A(5)), 1); 1525a83710eSEric Fiselier test(S("1", A(5)), 2); 1535a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0); 1545a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5); 1555a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50); 1565a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500); 1575a83710eSEric Fiselier 1585a83710eSEric Fiselier test(S(A(3)), 0, 0); 1595a83710eSEric Fiselier test(S(A(3)), 0, 1); 1605a83710eSEric Fiselier test(S(A(3)), 1, 0); 1615a83710eSEric Fiselier test(S(A(3)), 1, 1); 1625a83710eSEric Fiselier test(S(A(3)), 1, 2); 1635a83710eSEric Fiselier test(S("1", A(5)), 0, 0); 1645a83710eSEric Fiselier test(S("1", A(5)), 0, 1); 1655a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0); 1665a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1); 1675a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10); 1685a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100); 1695a83710eSEric Fiselier 1705a83710eSEric Fiselier test(S(A(3)), 0, 0, A(4)); 1715a83710eSEric Fiselier test(S(A(3)), 0, 1, A(4)); 1725a83710eSEric Fiselier test(S(A(3)), 1, 0, A(4)); 1735a83710eSEric Fiselier test(S(A(3)), 1, 1, A(4)); 1745a83710eSEric Fiselier test(S(A(3)), 1, 2, A(4)); 1755a83710eSEric Fiselier test(S("1", A(5)), 0, 0, A(6)); 1765a83710eSEric Fiselier test(S("1", A(5)), 0, 1, A(6)); 1775a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8)); 1785a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8)); 1795a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8)); 1805a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8)); 1815a83710eSEric Fiselier } 18289685ed0SMarshall Clow #if TEST_STD_VER >= 11 1835a83710eSEric Fiselier { 1845a83710eSEric Fiselier typedef min_allocator<char> A; 1855a83710eSEric Fiselier typedef std::basic_string<char, std::char_traits<char>, A> S; 1865a83710eSEric Fiselier 1875a83710eSEric Fiselier test(S(A()), 0); 1885a83710eSEric Fiselier test(S(A()), 1); 1895a83710eSEric Fiselier test(S("1", A()), 0); 1905a83710eSEric Fiselier test(S("1", A()), 1); 1915a83710eSEric Fiselier test(S("1", A()), 2); 1925a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0); 1935a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5); 1945a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50); 1955a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500); 1965a83710eSEric Fiselier 1975a83710eSEric Fiselier test(S(A()), 0, 0); 1985a83710eSEric Fiselier test(S(A()), 0, 1); 1995a83710eSEric Fiselier test(S(A()), 1, 0); 2005a83710eSEric Fiselier test(S(A()), 1, 1); 2015a83710eSEric Fiselier test(S(A()), 1, 2); 2025a83710eSEric Fiselier test(S("1", A()), 0, 0); 2035a83710eSEric Fiselier test(S("1", A()), 0, 1); 2045a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0); 2055a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1); 2065a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10); 2075a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100); 2085a83710eSEric Fiselier 2095a83710eSEric Fiselier test(S(A()), 0, 0, A()); 2105a83710eSEric Fiselier test(S(A()), 0, 1, A()); 2115a83710eSEric Fiselier test(S(A()), 1, 0, A()); 2125a83710eSEric Fiselier test(S(A()), 1, 1, A()); 2135a83710eSEric Fiselier test(S(A()), 1, 2, A()); 2145a83710eSEric Fiselier test(S("1", A()), 0, 0, A()); 2155a83710eSEric Fiselier test(S("1", A()), 0, 1, A()); 2165a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A()); 2175a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A()); 2185a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A()); 2195a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A()); 2205a83710eSEric Fiselier } 2218a915ed6SRoger Ferrer Ibanez #endif 2222df59c50SJF Bastien 223e85018b7SNikolas Klauser return true; 224e85018b7SNikolas Klauser } 225e85018b7SNikolas Klauser 226e85018b7SNikolas Klauser int main(int, char**) 227e85018b7SNikolas Klauser { 228e85018b7SNikolas Klauser test(); 229e85018b7SNikolas Klauser #if TEST_STD_VER > 17 230*425620ccSNikolas Klauser static_assert(test()); 231e85018b7SNikolas Klauser #endif 23285e9b268SNikolas Klauser test_lwg2583(); 233e85018b7SNikolas Klauser 2342df59c50SJF Bastien return 0; 2355a83710eSEric Fiselier } 236