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 = npos);
14 //  the "= npos" was added for C++14
15 
16 #include <string>
17 #include <stdexcept>
18 #include <cassert>
19 
20 #include "min_allocator.h"
21 
22 template <class S>
23 void
test(S s,S str,typename S::size_type pos,typename S::size_type n,S expected)24 test(S s, S str, typename S::size_type pos, typename S::size_type n, S expected)
25 {
26     try
27     {
28         s.append(str, pos, n);
29         assert(s.__invariants());
30         assert(pos <= str.size());
31         assert(s == expected);
32     }
33     catch (std::out_of_range&)
34     {
35         assert(pos > str.size());
36     }
37 }
38 
39 template <class S>
40 void
test_npos(S s,S str,typename S::size_type pos,S expected)41 test_npos(S s, S str, typename S::size_type pos, S expected)
42 {
43     try
44     {
45         s.append(str, pos);
46         assert(s.__invariants());
47         assert(pos <= str.size());
48         assert(s == expected);
49     }
50     catch (std::out_of_range&)
51     {
52         assert(pos > str.size());
53     }
54 }
55 
main()56 int main()
57 {
58     {
59     typedef std::string S;
60     test(S(), S(), 0, 0, S());
61     test(S(), S(), 1, 0, S());
62     test(S(), S("12345"), 0, 3, S("123"));
63     test(S(), S("12345"), 1, 4, S("2345"));
64     test(S(), S("12345"), 3, 15, S("45"));
65     test(S(), S("12345"), 5, 15, S(""));
66     test(S(), S("12345"), 6, 15, S("not happening"));
67     test(S(), S("12345678901234567890"), 0, 0, S());
68     test(S(), S("12345678901234567890"), 1, 1, S("2"));
69     test(S(), S("12345678901234567890"), 2, 3, S("345"));
70     test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
71     test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
72 
73     test(S("12345"), S(), 0, 0, S("12345"));
74     test(S("12345"), S("12345"), 2, 2, S("1234534"));
75     test(S("12345"), S("1234567890"), 0, 100, S("123451234567890"));
76 
77     test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890"));
78     test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234"));
79     test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
80          S("123456789012345678906789012345"));
81     }
82 #if __cplusplus >= 201103L
83     {
84     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
85     test(S(), S(), 0, 0, S());
86     test(S(), S(), 1, 0, S());
87     test(S(), S("12345"), 0, 3, S("123"));
88     test(S(), S("12345"), 1, 4, S("2345"));
89     test(S(), S("12345"), 3, 15, S("45"));
90     test(S(), S("12345"), 5, 15, S(""));
91     test(S(), S("12345"), 6, 15, S("not happening"));
92     test(S(), S("12345678901234567890"), 0, 0, S());
93     test(S(), S("12345678901234567890"), 1, 1, S("2"));
94     test(S(), S("12345678901234567890"), 2, 3, S("345"));
95     test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
96     test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
97 
98     test(S("12345"), S(), 0, 0, S("12345"));
99     test(S("12345"), S("12345"), 2, 2, S("1234534"));
100     test(S("12345"), S("1234567890"), 0, 100, S("123451234567890"));
101 
102     test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890"));
103     test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234"));
104     test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
105          S("123456789012345678906789012345"));
106     }
107 #endif
108     {
109     typedef std::string S;
110     test_npos(S(), S(), 0, S());
111     test_npos(S(), S(), 1, S());
112     test_npos(S(), S("12345"), 0, S("12345"));
113     test_npos(S(), S("12345"), 1, S("2345"));
114     test_npos(S(), S("12345"), 3, S("45"));
115     test_npos(S(), S("12345"), 5, S(""));
116     test_npos(S(), S("12345"), 6, S("not happening"));
117     }
118 }
119