1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <string>
11 
12 // basic_string<charT,traits,Allocator>&
13 //   append(const basic_string<charT,traits>& str, size_type pos, size_type n);
14 
15 #include <string>
16 #include <stdexcept>
17 #include <cassert>
18 
19 #include "../../min_allocator.h"
20 
21 template <class S>
22 void
23 test(S s, S str, typename S::size_type pos, typename S::size_type n, S expected)
24 {
25     try
26     {
27         s.append(str, pos, n);
28         assert(s.__invariants());
29         assert(pos <= str.size());
30         assert(s == expected);
31     }
32     catch (std::out_of_range&)
33     {
34         assert(pos > str.size());
35     }
36 }
37 
38 int main()
39 {
40     {
41     typedef std::string S;
42     test(S(), S(), 0, 0, S());
43     test(S(), S(), 1, 0, S());
44     test(S(), S("12345"), 0, 3, S("123"));
45     test(S(), S("12345"), 1, 4, S("2345"));
46     test(S(), S("12345"), 3, 15, S("45"));
47     test(S(), S("12345"), 5, 15, S(""));
48     test(S(), S("12345"), 6, 15, S("not happening"));
49     test(S(), S("12345678901234567890"), 0, 0, S());
50     test(S(), S("12345678901234567890"), 1, 1, S("2"));
51     test(S(), S("12345678901234567890"), 2, 3, S("345"));
52     test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
53     test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
54 
55     test(S("12345"), S(), 0, 0, S("12345"));
56     test(S("12345"), S("12345"), 2, 2, S("1234534"));
57     test(S("12345"), S("1234567890"), 0, 100, S("123451234567890"));
58 
59     test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890"));
60     test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234"));
61     test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
62          S("123456789012345678906789012345"));
63     }
64 #if __cplusplus >= 201103L
65     {
66     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
67     test(S(), S(), 0, 0, S());
68     test(S(), S(), 1, 0, S());
69     test(S(), S("12345"), 0, 3, S("123"));
70     test(S(), S("12345"), 1, 4, S("2345"));
71     test(S(), S("12345"), 3, 15, S("45"));
72     test(S(), S("12345"), 5, 15, S(""));
73     test(S(), S("12345"), 6, 15, S("not happening"));
74     test(S(), S("12345678901234567890"), 0, 0, S());
75     test(S(), S("12345678901234567890"), 1, 1, S("2"));
76     test(S(), S("12345678901234567890"), 2, 3, S("345"));
77     test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
78     test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
79 
80     test(S("12345"), S(), 0, 0, S("12345"));
81     test(S("12345"), S("12345"), 2, 2, S("1234534"));
82     test(S("12345"), S("1234567890"), 0, 100, S("123451234567890"));
83 
84     test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890"));
85     test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234"));
86     test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
87          S("123456789012345678906789012345"));
88     }
89 #endif
90 }
91