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 // iterator insert(const_iterator p, charT c);
13
14 #include <string>
15 #include <stdexcept>
16 #include <cassert>
17
18 #include "test_macros.h"
19 #include "min_allocator.h"
20
21 template <class S>
22 void
test(S & s,typename S::const_iterator p,typename S::value_type c,S expected)23 test(S& s, typename S::const_iterator p, typename S::value_type c, S expected)
24 {
25 bool sufficient_cap = s.size() < s.capacity();
26 typename S::difference_type pos = p - s.begin();
27 typename S::iterator i = s.insert(p, c);
28 LIBCPP_ASSERT(s.__invariants());
29 assert(s == expected);
30 assert(i - s.begin() == pos);
31 assert(*i == c);
32 if (sufficient_cap)
33 assert(i == p);
34 }
35
main()36 int main()
37 {
38 {
39 typedef std::string S;
40 S s;
41 test(s, s.begin(), '1', S("1"));
42 test(s, s.begin(), 'a', S("a1"));
43 test(s, s.end(), 'b', S("a1b"));
44 test(s, s.end()-1, 'c', S("a1cb"));
45 test(s, s.end()-2, 'd', S("a1dcb"));
46 test(s, s.end()-3, '2', S("a12dcb"));
47 test(s, s.end()-4, '3', S("a132dcb"));
48 test(s, s.end()-5, '4', S("a1432dcb"));
49 test(s, s.begin()+1, '5', S("a51432dcb"));
50 test(s, s.begin()+2, '6', S("a561432dcb"));
51 test(s, s.begin()+3, '7', S("a5671432dcb"));
52 test(s, s.begin()+4, 'A', S("a567A1432dcb"));
53 test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
54 test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
55 }
56 #if TEST_STD_VER >= 11
57 {
58 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
59 S s;
60 test(s, s.begin(), '1', S("1"));
61 test(s, s.begin(), 'a', S("a1"));
62 test(s, s.end(), 'b', S("a1b"));
63 test(s, s.end()-1, 'c', S("a1cb"));
64 test(s, s.end()-2, 'd', S("a1dcb"));
65 test(s, s.end()-3, '2', S("a12dcb"));
66 test(s, s.end()-4, '3', S("a132dcb"));
67 test(s, s.end()-5, '4', S("a1432dcb"));
68 test(s, s.begin()+1, '5', S("a51432dcb"));
69 test(s, s.begin()+2, '6', S("a561432dcb"));
70 test(s, s.begin()+3, '7', S("a5671432dcb"));
71 test(s, s.begin()+4, 'A', S("a567A1432dcb"));
72 test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
73 test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
74 }
75 #endif
76 }
77