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