xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/iter_char.pass.cpp (revision 5a83710e371fe68a06e6e3876c6a2c8b820a8976)
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 #if _LIBCPP_DEBUG >= 1
15 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
16 #endif
17 
18 #include <string>
19 #include <stdexcept>
20 #include <cassert>
21 
22 #include "min_allocator.h"
23 
24 template <class S>
25 void
26 test(S& s, typename S::const_iterator p, typename S::value_type c, S expected)
27 {
28     bool sufficient_cap = s.size() < s.capacity();
29     typename S::difference_type pos = p - s.begin();
30     typename S::iterator i = s.insert(p, c);
31     assert(s.__invariants());
32     assert(s == expected);
33     assert(i - s.begin() == pos);
34     assert(*i == c);
35     if (sufficient_cap)
36         assert(i == p);
37 }
38 
39 int main()
40 {
41     {
42     typedef std::string S;
43     S s;
44     test(s, s.begin(), '1', S("1"));
45     test(s, s.begin(), 'a', S("a1"));
46     test(s, s.end(), 'b', S("a1b"));
47     test(s, s.end()-1, 'c', S("a1cb"));
48     test(s, s.end()-2, 'd', S("a1dcb"));
49     test(s, s.end()-3, '2', S("a12dcb"));
50     test(s, s.end()-4, '3', S("a132dcb"));
51     test(s, s.end()-5, '4', S("a1432dcb"));
52     test(s, s.begin()+1, '5', S("a51432dcb"));
53     test(s, s.begin()+2, '6', S("a561432dcb"));
54     test(s, s.begin()+3, '7', S("a5671432dcb"));
55     test(s, s.begin()+4, 'A', S("a567A1432dcb"));
56     test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
57     test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
58     }
59 #if __cplusplus >= 201103L
60     {
61     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
62     S s;
63     test(s, s.begin(), '1', S("1"));
64     test(s, s.begin(), 'a', S("a1"));
65     test(s, s.end(), 'b', S("a1b"));
66     test(s, s.end()-1, 'c', S("a1cb"));
67     test(s, s.end()-2, 'd', S("a1dcb"));
68     test(s, s.end()-3, '2', S("a12dcb"));
69     test(s, s.end()-4, '3', S("a132dcb"));
70     test(s, s.end()-5, '4', S("a1432dcb"));
71     test(s, s.begin()+1, '5', S("a51432dcb"));
72     test(s, s.begin()+2, '6', S("a561432dcb"));
73     test(s, s.begin()+3, '7', S("a5671432dcb"));
74     test(s, s.begin()+4, 'A', S("a567A1432dcb"));
75     test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
76     test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
77     }
78 #endif
79 #if _LIBCPP_DEBUG >= 1
80     {
81         typedef std::string S;
82         S s;
83         S s2;
84         s.insert(s2.begin(), '1');
85         assert(false);
86     }
87 #endif
88 }
89