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,
13*425620ccSNikolas Klauser //              const Allocator& a = Allocator()); // constexpr since C++20
1489685ed0SMarshall Clow //
1589685ed0SMarshall Clow // basic_string(const basic_string<charT,traits,Allocator>& str,
1689685ed0SMarshall Clow //              size_type pos,
17*425620ccSNikolas Klauser //              const Allocator& a = Allocator()); // constexpr since C++20
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>
31e85018b7SNikolas Klauser TEST_CONSTEXPR_CXX20 void
test(S str,unsigned pos)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
4885e9b268SNikolas Klauser     else if (!TEST_IS_CONSTANT_EVALUATED)
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>
64e85018b7SNikolas Klauser TEST_CONSTEXPR_CXX20 void
test(S str,unsigned pos,unsigned n)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
8085e9b268SNikolas Klauser     else if (!TEST_IS_CONSTANT_EVALUATED)
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>
96e85018b7SNikolas Klauser TEST_CONSTEXPR_CXX20 void
test(S str,unsigned pos,unsigned n,const typename S::allocator_type & a)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
11285e9b268SNikolas Klauser     else if (!TEST_IS_CONSTANT_EVALUATED)
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 
test_lwg2583()12785e9b268SNikolas Klauser void test_lwg2583()
12885e9b268SNikolas Klauser {
12985e9b268SNikolas Klauser #if TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS)
13089685ed0SMarshall Clow     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> StringA;
13189685ed0SMarshall Clow     std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs;
13289685ed0SMarshall Clow     StringA s{"1234"};
13389685ed0SMarshall Clow     vs.emplace_back(s, 2);
13489685ed0SMarshall Clow 
13589685ed0SMarshall Clow     try { vs.emplace_back(s, 5); }
13689685ed0SMarshall Clow     catch (const std::out_of_range&) { return; }
13789685ed0SMarshall Clow     assert(false);
13885e9b268SNikolas Klauser #endif
13989685ed0SMarshall Clow }
14089685ed0SMarshall Clow 
test()141*425620ccSNikolas Klauser TEST_CONSTEXPR_CXX20 bool test() {
1425a83710eSEric Fiselier   {
1435a83710eSEric Fiselier     typedef test_allocator<char> A;
1445a83710eSEric Fiselier     typedef std::basic_string<char, std::char_traits<char>, A> S;
1455a83710eSEric Fiselier 
1465a83710eSEric Fiselier     test(S(A(3)), 0);
1475a83710eSEric Fiselier     test(S(A(3)), 1);
1485a83710eSEric Fiselier     test(S("1", A(5)), 0);
1495a83710eSEric Fiselier     test(S("1", A(5)), 1);
1505a83710eSEric Fiselier     test(S("1", A(5)), 2);
1515a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0);
1525a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5);
1535a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50);
1545a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500);
1555a83710eSEric Fiselier 
1565a83710eSEric Fiselier     test(S(A(3)), 0, 0);
1575a83710eSEric Fiselier     test(S(A(3)), 0, 1);
1585a83710eSEric Fiselier     test(S(A(3)), 1, 0);
1595a83710eSEric Fiselier     test(S(A(3)), 1, 1);
1605a83710eSEric Fiselier     test(S(A(3)), 1, 2);
1615a83710eSEric Fiselier     test(S("1", A(5)), 0, 0);
1625a83710eSEric Fiselier     test(S("1", A(5)), 0, 1);
1635a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0);
1645a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1);
1655a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10);
1665a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100);
1675a83710eSEric Fiselier 
1685a83710eSEric Fiselier     test(S(A(3)), 0, 0, A(4));
1695a83710eSEric Fiselier     test(S(A(3)), 0, 1, A(4));
1705a83710eSEric Fiselier     test(S(A(3)), 1, 0, A(4));
1715a83710eSEric Fiselier     test(S(A(3)), 1, 1, A(4));
1725a83710eSEric Fiselier     test(S(A(3)), 1, 2, A(4));
1735a83710eSEric Fiselier     test(S("1", A(5)), 0, 0, A(6));
1745a83710eSEric Fiselier     test(S("1", A(5)), 0, 1, A(6));
1755a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8));
1765a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8));
1775a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8));
1785a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8));
1795a83710eSEric Fiselier   }
18089685ed0SMarshall Clow #if TEST_STD_VER >= 11
1815a83710eSEric Fiselier   {
1825a83710eSEric Fiselier     typedef min_allocator<char> A;
1835a83710eSEric Fiselier     typedef std::basic_string<char, std::char_traits<char>, A> S;
1845a83710eSEric Fiselier 
1855a83710eSEric Fiselier     test(S(A()), 0);
1865a83710eSEric Fiselier     test(S(A()), 1);
1875a83710eSEric Fiselier     test(S("1", A()), 0);
1885a83710eSEric Fiselier     test(S("1", A()), 1);
1895a83710eSEric Fiselier     test(S("1", A()), 2);
1905a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0);
1915a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5);
1925a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50);
1935a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500);
1945a83710eSEric Fiselier 
1955a83710eSEric Fiselier     test(S(A()), 0, 0);
1965a83710eSEric Fiselier     test(S(A()), 0, 1);
1975a83710eSEric Fiselier     test(S(A()), 1, 0);
1985a83710eSEric Fiselier     test(S(A()), 1, 1);
1995a83710eSEric Fiselier     test(S(A()), 1, 2);
2005a83710eSEric Fiselier     test(S("1", A()), 0, 0);
2015a83710eSEric Fiselier     test(S("1", A()), 0, 1);
2025a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0);
2035a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1);
2045a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10);
2055a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100);
2065a83710eSEric Fiselier 
2075a83710eSEric Fiselier     test(S(A()), 0, 0, A());
2085a83710eSEric Fiselier     test(S(A()), 0, 1, A());
2095a83710eSEric Fiselier     test(S(A()), 1, 0, A());
2105a83710eSEric Fiselier     test(S(A()), 1, 1, A());
2115a83710eSEric Fiselier     test(S(A()), 1, 2, A());
2125a83710eSEric Fiselier     test(S("1", A()), 0, 0, A());
2135a83710eSEric Fiselier     test(S("1", A()), 0, 1, A());
2145a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A());
2155a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A());
2165a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A());
2175a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
2185a83710eSEric Fiselier   }
2198a915ed6SRoger Ferrer Ibanez #endif
2202df59c50SJF Bastien 
221e85018b7SNikolas Klauser   return true;
222e85018b7SNikolas Klauser }
223e85018b7SNikolas Klauser 
main(int,char **)224e85018b7SNikolas Klauser int main(int, char**)
225e85018b7SNikolas Klauser {
226e85018b7SNikolas Klauser   test();
227e85018b7SNikolas Klauser #if TEST_STD_VER > 17
228*425620ccSNikolas Klauser   static_assert(test());
229e85018b7SNikolas Klauser #endif
23085e9b268SNikolas Klauser   test_lwg2583();
231e85018b7SNikolas Klauser 
2322df59c50SJF Bastien   return 0;
2335a83710eSEric Fiselier }
234