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 
10f520c144SAsiri Rathnayake // XFAIL: libcpp-no-exceptions
115a83710eSEric Fiselier // <string>
125a83710eSEric Fiselier 
135a83710eSEric Fiselier // basic_string(const basic_string<charT,traits,Allocator>& str,
14*89685ed0SMarshall Clow //              size_type pos, size_type n,
15*89685ed0SMarshall Clow //              const Allocator& a = Allocator());
16*89685ed0SMarshall Clow //
17*89685ed0SMarshall Clow // basic_string(const basic_string<charT,traits,Allocator>& str,
18*89685ed0SMarshall Clow //              size_type pos,
195a83710eSEric Fiselier //              const Allocator& a = Allocator());
205a83710eSEric Fiselier 
215a83710eSEric Fiselier #include <string>
225a83710eSEric Fiselier #include <stdexcept>
235a83710eSEric Fiselier #include <algorithm>
24*89685ed0SMarshall Clow #include <vector>
25*89685ed0SMarshall Clow #include <scoped_allocator>
265a83710eSEric Fiselier #include <cassert>
275a83710eSEric Fiselier 
28*89685ed0SMarshall Clow #include "test_macros.h"
295a83710eSEric Fiselier #include "test_allocator.h"
305a83710eSEric Fiselier #include "min_allocator.h"
315a83710eSEric Fiselier 
325a83710eSEric Fiselier template <class S>
335a83710eSEric Fiselier 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;
385a83710eSEric Fiselier     try
395a83710eSEric Fiselier     {
405a83710eSEric Fiselier         S s2(str, pos);
415a83710eSEric Fiselier         assert(s2.__invariants());
425a83710eSEric Fiselier         assert(pos <= str.size());
435a83710eSEric Fiselier         unsigned 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     }
495a83710eSEric Fiselier     catch (std::out_of_range&)
505a83710eSEric Fiselier     {
515a83710eSEric Fiselier         assert(pos > str.size());
525a83710eSEric Fiselier     }
535a83710eSEric Fiselier }
545a83710eSEric Fiselier 
555a83710eSEric Fiselier template <class S>
565a83710eSEric Fiselier void
575a83710eSEric Fiselier test(S str, unsigned pos, unsigned n)
585a83710eSEric Fiselier {
595a83710eSEric Fiselier     typedef typename S::traits_type T;
605a83710eSEric Fiselier     typedef typename S::allocator_type A;
615a83710eSEric Fiselier     try
625a83710eSEric Fiselier     {
635a83710eSEric Fiselier         S s2(str, pos, n);
645a83710eSEric Fiselier         assert(s2.__invariants());
655a83710eSEric Fiselier         assert(pos <= str.size());
665a83710eSEric Fiselier         unsigned rlen = std::min<unsigned>(str.size() - pos, n);
675a83710eSEric Fiselier         assert(s2.size() == rlen);
685a83710eSEric Fiselier         assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
695a83710eSEric Fiselier         assert(s2.get_allocator() == A());
705a83710eSEric Fiselier         assert(s2.capacity() >= s2.size());
715a83710eSEric Fiselier     }
725a83710eSEric Fiselier     catch (std::out_of_range&)
735a83710eSEric Fiselier     {
745a83710eSEric Fiselier         assert(pos > str.size());
755a83710eSEric Fiselier     }
765a83710eSEric Fiselier }
775a83710eSEric Fiselier 
785a83710eSEric Fiselier template <class S>
795a83710eSEric Fiselier void
805a83710eSEric Fiselier test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
815a83710eSEric Fiselier {
825a83710eSEric Fiselier     typedef typename S::traits_type T;
835a83710eSEric Fiselier     typedef typename S::allocator_type A;
845a83710eSEric Fiselier     try
855a83710eSEric Fiselier     {
865a83710eSEric Fiselier         S s2(str, pos, n, a);
875a83710eSEric Fiselier         assert(s2.__invariants());
885a83710eSEric Fiselier         assert(pos <= str.size());
895a83710eSEric Fiselier         unsigned rlen = std::min<unsigned>(str.size() - pos, n);
905a83710eSEric Fiselier         assert(s2.size() == rlen);
915a83710eSEric Fiselier         assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
925a83710eSEric Fiselier         assert(s2.get_allocator() == a);
935a83710eSEric Fiselier         assert(s2.capacity() >= s2.size());
945a83710eSEric Fiselier     }
955a83710eSEric Fiselier     catch (std::out_of_range&)
965a83710eSEric Fiselier     {
975a83710eSEric Fiselier         assert(pos > str.size());
985a83710eSEric Fiselier     }
995a83710eSEric Fiselier }
1005a83710eSEric Fiselier 
101*89685ed0SMarshall Clow #if TEST_STD_VER >= 11
102*89685ed0SMarshall Clow void test2583()
103*89685ed0SMarshall Clow {   // LWG #2583
104*89685ed0SMarshall Clow     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA;
105*89685ed0SMarshall Clow     std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs;
106*89685ed0SMarshall Clow     StringA s{"1234"};
107*89685ed0SMarshall Clow     vs.emplace_back(s, 2);
108*89685ed0SMarshall Clow 
109*89685ed0SMarshall Clow     try { vs.emplace_back(s, 5); }
110*89685ed0SMarshall Clow     catch (const std::out_of_range&) { return; }
111*89685ed0SMarshall Clow     assert(false);
112*89685ed0SMarshall Clow }
113*89685ed0SMarshall Clow #endif
114*89685ed0SMarshall Clow 
1155a83710eSEric Fiselier int main()
1165a83710eSEric Fiselier {
1175a83710eSEric Fiselier     {
1185a83710eSEric Fiselier     typedef test_allocator<char> A;
1195a83710eSEric Fiselier     typedef std::basic_string<char, std::char_traits<char>, A> S;
1205a83710eSEric Fiselier 
1215a83710eSEric Fiselier     test(S(A(3)), 0);
1225a83710eSEric Fiselier     test(S(A(3)), 1);
1235a83710eSEric Fiselier     test(S("1", A(5)), 0);
1245a83710eSEric Fiselier     test(S("1", A(5)), 1);
1255a83710eSEric Fiselier     test(S("1", A(5)), 2);
1265a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0);
1275a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5);
1285a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50);
1295a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500);
1305a83710eSEric Fiselier 
1315a83710eSEric Fiselier     test(S(A(3)), 0, 0);
1325a83710eSEric Fiselier     test(S(A(3)), 0, 1);
1335a83710eSEric Fiselier     test(S(A(3)), 1, 0);
1345a83710eSEric Fiselier     test(S(A(3)), 1, 1);
1355a83710eSEric Fiselier     test(S(A(3)), 1, 2);
1365a83710eSEric Fiselier     test(S("1", A(5)), 0, 0);
1375a83710eSEric Fiselier     test(S("1", A(5)), 0, 1);
1385a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0);
1395a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1);
1405a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10);
1415a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100);
1425a83710eSEric Fiselier 
1435a83710eSEric Fiselier     test(S(A(3)), 0, 0, A(4));
1445a83710eSEric Fiselier     test(S(A(3)), 0, 1, A(4));
1455a83710eSEric Fiselier     test(S(A(3)), 1, 0, A(4));
1465a83710eSEric Fiselier     test(S(A(3)), 1, 1, A(4));
1475a83710eSEric Fiselier     test(S(A(3)), 1, 2, A(4));
1485a83710eSEric Fiselier     test(S("1", A(5)), 0, 0, A(6));
1495a83710eSEric Fiselier     test(S("1", A(5)), 0, 1, A(6));
1505a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8));
1515a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8));
1525a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8));
1535a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8));
1545a83710eSEric Fiselier     }
155*89685ed0SMarshall Clow #if TEST_STD_VER >= 11
1565a83710eSEric Fiselier     {
1575a83710eSEric Fiselier     typedef min_allocator<char> A;
1585a83710eSEric Fiselier     typedef std::basic_string<char, std::char_traits<char>, A> S;
1595a83710eSEric Fiselier 
1605a83710eSEric Fiselier     test(S(A()), 0);
1615a83710eSEric Fiselier     test(S(A()), 1);
1625a83710eSEric Fiselier     test(S("1", A()), 0);
1635a83710eSEric Fiselier     test(S("1", A()), 1);
1645a83710eSEric Fiselier     test(S("1", A()), 2);
1655a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0);
1665a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5);
1675a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50);
1685a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500);
1695a83710eSEric Fiselier 
1705a83710eSEric Fiselier     test(S(A()), 0, 0);
1715a83710eSEric Fiselier     test(S(A()), 0, 1);
1725a83710eSEric Fiselier     test(S(A()), 1, 0);
1735a83710eSEric Fiselier     test(S(A()), 1, 1);
1745a83710eSEric Fiselier     test(S(A()), 1, 2);
1755a83710eSEric Fiselier     test(S("1", A()), 0, 0);
1765a83710eSEric Fiselier     test(S("1", A()), 0, 1);
1775a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0);
1785a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1);
1795a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10);
1805a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100);
1815a83710eSEric Fiselier 
1825a83710eSEric Fiselier     test(S(A()), 0, 0, A());
1835a83710eSEric Fiselier     test(S(A()), 0, 1, A());
1845a83710eSEric Fiselier     test(S(A()), 1, 0, A());
1855a83710eSEric Fiselier     test(S(A()), 1, 1, A());
1865a83710eSEric Fiselier     test(S(A()), 1, 2, A());
1875a83710eSEric Fiselier     test(S("1", A()), 0, 0, A());
1885a83710eSEric Fiselier     test(S("1", A()), 0, 1, A());
1895a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A());
1905a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A());
1915a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A());
1925a83710eSEric Fiselier     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
1935a83710eSEric Fiselier     }
194*89685ed0SMarshall Clow 
195*89685ed0SMarshall Clow     test2583();
1965a83710eSEric Fiselier #endif
1975a83710eSEric Fiselier }
198