15a83710eSEric Fiselier //===----------------------------------------------------------------------===// 25a83710eSEric Fiselier // 35a83710eSEric Fiselier // The LLVM Compiler Infrastructure 45a83710eSEric Fiselier // 55a83710eSEric Fiselier // This file is dual licensed under the MIT and the University of Illinois Open 65a83710eSEric Fiselier // Source Licenses. See LICENSE.TXT for details. 75a83710eSEric Fiselier // 85a83710eSEric Fiselier //===----------------------------------------------------------------------===// 95a83710eSEric Fiselier 105a83710eSEric Fiselier // <string> 115a83710eSEric Fiselier 125a83710eSEric Fiselier // basic_string(const basic_string<charT,traits,Allocator>& str, 1389685ed0SMarshall Clow // size_type pos, size_type n, 1489685ed0SMarshall Clow // const Allocator& a = Allocator()); 1589685ed0SMarshall Clow // 1689685ed0SMarshall Clow // basic_string(const basic_string<charT,traits,Allocator>& str, 1789685ed0SMarshall Clow // size_type pos, 185a83710eSEric Fiselier // const Allocator& a = Allocator()); 195a83710eSEric Fiselier 205a83710eSEric Fiselier #include <string> 215a83710eSEric Fiselier #include <stdexcept> 225a83710eSEric Fiselier #include <algorithm> 2389685ed0SMarshall Clow #include <vector> 2489685ed0SMarshall Clow #include <scoped_allocator> 255a83710eSEric Fiselier #include <cassert> 265a83710eSEric Fiselier 2789685ed0SMarshall Clow #include "test_macros.h" 285a83710eSEric Fiselier #include "test_allocator.h" 295a83710eSEric Fiselier #include "min_allocator.h" 305a83710eSEric Fiselier 315a83710eSEric Fiselier template <class S> 325a83710eSEric Fiselier void 335a83710eSEric Fiselier test(S str, unsigned pos) 345a83710eSEric Fiselier { 355a83710eSEric Fiselier typedef typename S::traits_type T; 365a83710eSEric Fiselier typedef typename S::allocator_type A; 378a915ed6SRoger Ferrer Ibanez 388a915ed6SRoger Ferrer Ibanez if (pos <= str.size()) 395a83710eSEric Fiselier { 405a83710eSEric Fiselier S s2(str, pos); 411f4231f8SEric Fiselier LIBCPP_ASSERT(s2.__invariants()); 42*d4b83e6dSStephan T. Lavavej typename S::size_type rlen = str.size() - pos; 435a83710eSEric Fiselier assert(s2.size() == rlen); 445a83710eSEric Fiselier assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); 455a83710eSEric Fiselier assert(s2.get_allocator() == A()); 465a83710eSEric Fiselier assert(s2.capacity() >= s2.size()); 475a83710eSEric Fiselier } 488a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS 498a915ed6SRoger Ferrer Ibanez else 508a915ed6SRoger Ferrer Ibanez { 518a915ed6SRoger Ferrer Ibanez try 528a915ed6SRoger Ferrer Ibanez { 538a915ed6SRoger Ferrer Ibanez S s2(str, pos); 548a915ed6SRoger Ferrer Ibanez assert(false); 558a915ed6SRoger Ferrer Ibanez } 565a83710eSEric Fiselier catch (std::out_of_range&) 575a83710eSEric Fiselier { 585a83710eSEric Fiselier assert(pos > str.size()); 595a83710eSEric Fiselier } 605a83710eSEric Fiselier } 618a915ed6SRoger Ferrer Ibanez #endif 628a915ed6SRoger Ferrer Ibanez } 635a83710eSEric Fiselier 645a83710eSEric Fiselier template <class S> 655a83710eSEric Fiselier void 665a83710eSEric Fiselier test(S str, unsigned pos, unsigned n) 675a83710eSEric Fiselier { 685a83710eSEric Fiselier typedef typename S::traits_type T; 695a83710eSEric Fiselier typedef typename S::allocator_type A; 708a915ed6SRoger Ferrer Ibanez if (pos <= str.size()) 715a83710eSEric Fiselier { 725a83710eSEric Fiselier S s2(str, pos, n); 731f4231f8SEric Fiselier LIBCPP_ASSERT(s2.__invariants()); 74*d4b83e6dSStephan T. Lavavej typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n); 755a83710eSEric Fiselier assert(s2.size() == rlen); 765a83710eSEric Fiselier assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); 775a83710eSEric Fiselier assert(s2.get_allocator() == A()); 785a83710eSEric Fiselier assert(s2.capacity() >= s2.size()); 795a83710eSEric Fiselier } 808a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS 818a915ed6SRoger Ferrer Ibanez else 828a915ed6SRoger Ferrer Ibanez { 838a915ed6SRoger Ferrer Ibanez try 848a915ed6SRoger Ferrer Ibanez { 858a915ed6SRoger Ferrer Ibanez S s2(str, pos, n); 868a915ed6SRoger Ferrer Ibanez assert(false); 878a915ed6SRoger Ferrer Ibanez } 885a83710eSEric Fiselier catch (std::out_of_range&) 895a83710eSEric Fiselier { 905a83710eSEric Fiselier assert(pos > str.size()); 915a83710eSEric Fiselier } 925a83710eSEric Fiselier } 938a915ed6SRoger Ferrer Ibanez #endif 948a915ed6SRoger Ferrer Ibanez } 955a83710eSEric Fiselier 965a83710eSEric Fiselier template <class S> 975a83710eSEric Fiselier void 985a83710eSEric Fiselier test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a) 995a83710eSEric Fiselier { 1005a83710eSEric Fiselier typedef typename S::traits_type T; 1015a83710eSEric Fiselier typedef typename S::allocator_type A; 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()); 107*d4b83e6dSStephan 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 1148a915ed6SRoger Ferrer Ibanez else 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 12989685ed0SMarshall Clow #if TEST_STD_VER >= 11 1308a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS 13189685ed0SMarshall Clow void test2583() 13289685ed0SMarshall Clow { // LWG #2583 13389685ed0SMarshall Clow typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA; 13489685ed0SMarshall Clow std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs; 13589685ed0SMarshall Clow StringA s{"1234"}; 13689685ed0SMarshall Clow vs.emplace_back(s, 2); 13789685ed0SMarshall Clow 13889685ed0SMarshall Clow try { vs.emplace_back(s, 5); } 13989685ed0SMarshall Clow catch (const std::out_of_range&) { return; } 14089685ed0SMarshall Clow assert(false); 14189685ed0SMarshall Clow } 14289685ed0SMarshall Clow #endif 1438a915ed6SRoger Ferrer Ibanez #endif 14489685ed0SMarshall Clow 1455a83710eSEric Fiselier int main() 1465a83710eSEric Fiselier { 1475a83710eSEric Fiselier { 1485a83710eSEric Fiselier typedef test_allocator<char> A; 1495a83710eSEric Fiselier typedef std::basic_string<char, std::char_traits<char>, A> S; 1505a83710eSEric Fiselier 1515a83710eSEric Fiselier test(S(A(3)), 0); 1525a83710eSEric Fiselier test(S(A(3)), 1); 1535a83710eSEric Fiselier test(S("1", A(5)), 0); 1545a83710eSEric Fiselier test(S("1", A(5)), 1); 1555a83710eSEric Fiselier test(S("1", A(5)), 2); 1565a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0); 1575a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5); 1585a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50); 1595a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500); 1605a83710eSEric Fiselier 1615a83710eSEric Fiselier test(S(A(3)), 0, 0); 1625a83710eSEric Fiselier test(S(A(3)), 0, 1); 1635a83710eSEric Fiselier test(S(A(3)), 1, 0); 1645a83710eSEric Fiselier test(S(A(3)), 1, 1); 1655a83710eSEric Fiselier test(S(A(3)), 1, 2); 1665a83710eSEric Fiselier test(S("1", A(5)), 0, 0); 1675a83710eSEric Fiselier test(S("1", A(5)), 0, 1); 1685a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0); 1695a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1); 1705a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10); 1715a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100); 1725a83710eSEric Fiselier 1735a83710eSEric Fiselier test(S(A(3)), 0, 0, A(4)); 1745a83710eSEric Fiselier test(S(A(3)), 0, 1, A(4)); 1755a83710eSEric Fiselier test(S(A(3)), 1, 0, A(4)); 1765a83710eSEric Fiselier test(S(A(3)), 1, 1, A(4)); 1775a83710eSEric Fiselier test(S(A(3)), 1, 2, A(4)); 1785a83710eSEric Fiselier test(S("1", A(5)), 0, 0, A(6)); 1795a83710eSEric Fiselier test(S("1", A(5)), 0, 1, A(6)); 1805a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8)); 1815a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8)); 1825a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8)); 1835a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8)); 1845a83710eSEric Fiselier } 18589685ed0SMarshall Clow #if TEST_STD_VER >= 11 1865a83710eSEric Fiselier { 1875a83710eSEric Fiselier typedef min_allocator<char> A; 1885a83710eSEric Fiselier typedef std::basic_string<char, std::char_traits<char>, A> S; 1895a83710eSEric Fiselier 1905a83710eSEric Fiselier test(S(A()), 0); 1915a83710eSEric Fiselier test(S(A()), 1); 1925a83710eSEric Fiselier test(S("1", A()), 0); 1935a83710eSEric Fiselier test(S("1", A()), 1); 1945a83710eSEric Fiselier test(S("1", A()), 2); 1955a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0); 1965a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5); 1975a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50); 1985a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500); 1995a83710eSEric Fiselier 2005a83710eSEric Fiselier test(S(A()), 0, 0); 2015a83710eSEric Fiselier test(S(A()), 0, 1); 2025a83710eSEric Fiselier test(S(A()), 1, 0); 2035a83710eSEric Fiselier test(S(A()), 1, 1); 2045a83710eSEric Fiselier test(S(A()), 1, 2); 2055a83710eSEric Fiselier test(S("1", A()), 0, 0); 2065a83710eSEric Fiselier test(S("1", A()), 0, 1); 2075a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0); 2085a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1); 2095a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10); 2105a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100); 2115a83710eSEric Fiselier 2125a83710eSEric Fiselier test(S(A()), 0, 0, A()); 2135a83710eSEric Fiselier test(S(A()), 0, 1, A()); 2145a83710eSEric Fiselier test(S(A()), 1, 0, A()); 2155a83710eSEric Fiselier test(S(A()), 1, 1, A()); 2165a83710eSEric Fiselier test(S(A()), 1, 2, A()); 2175a83710eSEric Fiselier test(S("1", A()), 0, 0, A()); 2185a83710eSEric Fiselier test(S("1", A()), 0, 1, A()); 2195a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A()); 2205a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A()); 2215a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A()); 2225a83710eSEric Fiselier test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A()); 2235a83710eSEric Fiselier } 22489685ed0SMarshall Clow 2258a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS 22689685ed0SMarshall Clow test2583(); 2275a83710eSEric Fiselier #endif 2288a915ed6SRoger Ferrer Ibanez #endif 2295a83710eSEric Fiselier } 230