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 
95a83710eSEric Fiselier // <string>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // basic_string(const basic_string<charT,traits,Allocator>& str,
1289685ed0SMarshall Clow //              size_type pos, size_type n,
1389685ed0SMarshall Clow //              const Allocator& a = Allocator());
1489685ed0SMarshall Clow //
1589685ed0SMarshall Clow // basic_string(const basic_string<charT,traits,Allocator>& str,
1689685ed0SMarshall Clow //              size_type pos,
175a83710eSEric Fiselier //              const Allocator& a = Allocator());
185a83710eSEric Fiselier 
195a83710eSEric Fiselier #include <string>
205a83710eSEric Fiselier #include <stdexcept>
215a83710eSEric Fiselier #include <algorithm>
2289685ed0SMarshall Clow #include <vector>
2389685ed0SMarshall Clow #include <scoped_allocator>
245a83710eSEric Fiselier #include <cassert>
255a83710eSEric Fiselier 
2689685ed0SMarshall Clow #include "test_macros.h"
275a83710eSEric Fiselier #include "test_allocator.h"
285a83710eSEric Fiselier #include "min_allocator.h"
295a83710eSEric Fiselier 
305a83710eSEric Fiselier template <class S>
315a83710eSEric Fiselier void
325a83710eSEric Fiselier test(S str, unsigned pos)
335a83710eSEric Fiselier {
345a83710eSEric Fiselier     typedef typename S::traits_type T;
355a83710eSEric Fiselier     typedef typename S::allocator_type A;
368a915ed6SRoger Ferrer Ibanez 
378a915ed6SRoger Ferrer Ibanez     if (pos <= str.size())
385a83710eSEric Fiselier     {
395a83710eSEric Fiselier         S s2(str, pos);
401f4231f8SEric Fiselier         LIBCPP_ASSERT(s2.__invariants());
41d4b83e6dSStephan T. Lavavej         typename S::size_type rlen = str.size() - pos;
425a83710eSEric Fiselier         assert(s2.size() == rlen);
435a83710eSEric Fiselier         assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
445a83710eSEric Fiselier         assert(s2.get_allocator() == A());
455a83710eSEric Fiselier         assert(s2.capacity() >= s2.size());
465a83710eSEric Fiselier     }
478a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS
488a915ed6SRoger Ferrer Ibanez     else
498a915ed6SRoger Ferrer Ibanez     {
508a915ed6SRoger Ferrer Ibanez         try
518a915ed6SRoger Ferrer Ibanez         {
528a915ed6SRoger Ferrer Ibanez             S s2(str, pos);
538a915ed6SRoger Ferrer Ibanez             assert(false);
548a915ed6SRoger Ferrer Ibanez         }
555a83710eSEric Fiselier         catch (std::out_of_range&)
565a83710eSEric Fiselier         {
575a83710eSEric Fiselier             assert(pos > str.size());
585a83710eSEric Fiselier         }
595a83710eSEric Fiselier     }
608a915ed6SRoger Ferrer Ibanez #endif
618a915ed6SRoger Ferrer Ibanez }
625a83710eSEric Fiselier 
635a83710eSEric Fiselier template <class S>
645a83710eSEric Fiselier void
655a83710eSEric Fiselier test(S str, unsigned pos, unsigned n)
665a83710eSEric Fiselier {
675a83710eSEric Fiselier     typedef typename S::traits_type T;
685a83710eSEric Fiselier     typedef typename S::allocator_type A;
698a915ed6SRoger Ferrer Ibanez     if (pos <= str.size())
705a83710eSEric Fiselier     {
715a83710eSEric Fiselier         S s2(str, pos, n);
721f4231f8SEric Fiselier         LIBCPP_ASSERT(s2.__invariants());
73d4b83e6dSStephan T. Lavavej         typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n);
745a83710eSEric Fiselier         assert(s2.size() == rlen);
755a83710eSEric Fiselier         assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
765a83710eSEric Fiselier         assert(s2.get_allocator() == A());
775a83710eSEric Fiselier         assert(s2.capacity() >= s2.size());
785a83710eSEric Fiselier     }
798a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS
808a915ed6SRoger Ferrer Ibanez     else
818a915ed6SRoger Ferrer Ibanez     {
828a915ed6SRoger Ferrer Ibanez         try
838a915ed6SRoger Ferrer Ibanez         {
848a915ed6SRoger Ferrer Ibanez             S s2(str, pos, n);
858a915ed6SRoger Ferrer Ibanez             assert(false);
868a915ed6SRoger Ferrer Ibanez         }
875a83710eSEric Fiselier         catch (std::out_of_range&)
885a83710eSEric Fiselier         {
895a83710eSEric Fiselier             assert(pos > str.size());
905a83710eSEric Fiselier         }
915a83710eSEric Fiselier     }
928a915ed6SRoger Ferrer Ibanez #endif
938a915ed6SRoger Ferrer Ibanez }
945a83710eSEric Fiselier 
955a83710eSEric Fiselier template <class S>
965a83710eSEric Fiselier void
975a83710eSEric Fiselier test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
985a83710eSEric Fiselier {
995a83710eSEric Fiselier     typedef typename S::traits_type T;
1008a915ed6SRoger Ferrer Ibanez 
1018a915ed6SRoger Ferrer Ibanez     if (pos <= str.size())
1025a83710eSEric Fiselier     {
1035a83710eSEric Fiselier         S s2(str, pos, n, a);
1041f4231f8SEric Fiselier         LIBCPP_ASSERT(s2.__invariants());
105d4b83e6dSStephan T. Lavavej         typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n);
1065a83710eSEric Fiselier         assert(s2.size() == rlen);
1075a83710eSEric Fiselier         assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
1085a83710eSEric Fiselier         assert(s2.get_allocator() == a);
1095a83710eSEric Fiselier         assert(s2.capacity() >= s2.size());
1105a83710eSEric Fiselier     }
1118a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS
1128a915ed6SRoger Ferrer Ibanez     else
1138a915ed6SRoger Ferrer Ibanez     {
1148a915ed6SRoger Ferrer Ibanez         try
1158a915ed6SRoger Ferrer Ibanez         {
1168a915ed6SRoger Ferrer Ibanez             S s2(str, pos, n, a);
1178a915ed6SRoger Ferrer Ibanez             assert(false);
1188a915ed6SRoger Ferrer Ibanez         }
1195a83710eSEric Fiselier         catch (std::out_of_range&)
1205a83710eSEric Fiselier         {
1215a83710eSEric Fiselier             assert(pos > str.size());
1225a83710eSEric Fiselier         }
1235a83710eSEric Fiselier     }
1248a915ed6SRoger Ferrer Ibanez #endif
1258a915ed6SRoger Ferrer Ibanez }
1265a83710eSEric Fiselier 
12789685ed0SMarshall Clow #if TEST_STD_VER >= 11
1288a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS
12989685ed0SMarshall Clow void test2583()
13089685ed0SMarshall Clow {   // LWG #2583
13189685ed0SMarshall Clow     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA;
13289685ed0SMarshall Clow     std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs;
13389685ed0SMarshall Clow     StringA s{"1234"};
13489685ed0SMarshall Clow     vs.emplace_back(s, 2);
13589685ed0SMarshall Clow 
13689685ed0SMarshall Clow     try { vs.emplace_back(s, 5); }
13789685ed0SMarshall Clow     catch (const std::out_of_range&) { return; }
13889685ed0SMarshall Clow     assert(false);
13989685ed0SMarshall Clow }
14089685ed0SMarshall Clow #endif
1418a915ed6SRoger Ferrer Ibanez #endif
14289685ed0SMarshall Clow 
143*2df59c50SJF Bastien int main(int, char**)
1445a83710eSEric Fiselier {
1455a83710eSEric Fiselier     {
1465a83710eSEric Fiselier     typedef test_allocator<char> A;
1475a83710eSEric Fiselier     typedef std::basic_string<char, std::char_traits<char>, A> S;
1485a83710eSEric Fiselier 
1495a83710eSEric Fiselier     test(S(A(3)), 0);
1505a83710eSEric Fiselier     test(S(A(3)), 1);
1515a83710eSEric Fiselier     test(S("1", A(5)), 0);
1525a83710eSEric Fiselier     test(S("1", A(5)), 1);
1535a83710eSEric Fiselier     test(S("1", A(5)), 2);
1545a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0);
1555a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5);
1565a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50);
1575a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500);
1585a83710eSEric Fiselier 
1595a83710eSEric Fiselier     test(S(A(3)), 0, 0);
1605a83710eSEric Fiselier     test(S(A(3)), 0, 1);
1615a83710eSEric Fiselier     test(S(A(3)), 1, 0);
1625a83710eSEric Fiselier     test(S(A(3)), 1, 1);
1635a83710eSEric Fiselier     test(S(A(3)), 1, 2);
1645a83710eSEric Fiselier     test(S("1", A(5)), 0, 0);
1655a83710eSEric Fiselier     test(S("1", A(5)), 0, 1);
1665a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0);
1675a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1);
1685a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10);
1695a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100);
1705a83710eSEric Fiselier 
1715a83710eSEric Fiselier     test(S(A(3)), 0, 0, A(4));
1725a83710eSEric Fiselier     test(S(A(3)), 0, 1, A(4));
1735a83710eSEric Fiselier     test(S(A(3)), 1, 0, A(4));
1745a83710eSEric Fiselier     test(S(A(3)), 1, 1, A(4));
1755a83710eSEric Fiselier     test(S(A(3)), 1, 2, A(4));
1765a83710eSEric Fiselier     test(S("1", A(5)), 0, 0, A(6));
1775a83710eSEric Fiselier     test(S("1", A(5)), 0, 1, A(6));
1785a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8));
1795a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8));
1805a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8));
1815a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8));
1825a83710eSEric Fiselier     }
18389685ed0SMarshall Clow #if TEST_STD_VER >= 11
1845a83710eSEric Fiselier     {
1855a83710eSEric Fiselier     typedef min_allocator<char> A;
1865a83710eSEric Fiselier     typedef std::basic_string<char, std::char_traits<char>, A> S;
1875a83710eSEric Fiselier 
1885a83710eSEric Fiselier     test(S(A()), 0);
1895a83710eSEric Fiselier     test(S(A()), 1);
1905a83710eSEric Fiselier     test(S("1", A()), 0);
1915a83710eSEric Fiselier     test(S("1", A()), 1);
1925a83710eSEric Fiselier     test(S("1", A()), 2);
1935a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0);
1945a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5);
1955a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50);
1965a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500);
1975a83710eSEric Fiselier 
1985a83710eSEric Fiselier     test(S(A()), 0, 0);
1995a83710eSEric Fiselier     test(S(A()), 0, 1);
2005a83710eSEric Fiselier     test(S(A()), 1, 0);
2015a83710eSEric Fiselier     test(S(A()), 1, 1);
2025a83710eSEric Fiselier     test(S(A()), 1, 2);
2035a83710eSEric Fiselier     test(S("1", A()), 0, 0);
2045a83710eSEric Fiselier     test(S("1", A()), 0, 1);
2055a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0);
2065a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1);
2075a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10);
2085a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100);
2095a83710eSEric Fiselier 
2105a83710eSEric Fiselier     test(S(A()), 0, 0, A());
2115a83710eSEric Fiselier     test(S(A()), 0, 1, A());
2125a83710eSEric Fiselier     test(S(A()), 1, 0, A());
2135a83710eSEric Fiselier     test(S(A()), 1, 1, A());
2145a83710eSEric Fiselier     test(S(A()), 1, 2, A());
2155a83710eSEric Fiselier     test(S("1", A()), 0, 0, A());
2165a83710eSEric Fiselier     test(S("1", A()), 0, 1, A());
2175a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A());
2185a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A());
2195a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A());
2205a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
2215a83710eSEric Fiselier     }
22289685ed0SMarshall Clow 
2238a915ed6SRoger Ferrer Ibanez #ifndef TEST_HAS_NO_EXCEPTIONS
22489685ed0SMarshall Clow     test2583();
2255a83710eSEric Fiselier #endif
2268a915ed6SRoger Ferrer Ibanez #endif
227*2df59c50SJF Bastien 
228*2df59c50SJF Bastien   return 0;
2295a83710eSEric Fiselier }
230