xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/iter_char.pass.cpp (revision dcffa7d3e140cf2e2a80f93168b40c449bc1d230)
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 TEST_CONSTEXPR_CXX20 void
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 
35 bool test() {
36   {
37     typedef std::string S;
38     S s;
39     test(s, s.begin(), '1', S("1"));
40     test(s, s.begin(), 'a', S("a1"));
41     test(s, s.end(), 'b', S("a1b"));
42     test(s, s.end()-1, 'c', S("a1cb"));
43     test(s, s.end()-2, 'd', S("a1dcb"));
44     test(s, s.end()-3, '2', S("a12dcb"));
45     test(s, s.end()-4, '3', S("a132dcb"));
46     test(s, s.end()-5, '4', S("a1432dcb"));
47     test(s, s.begin()+1, '5', S("a51432dcb"));
48     test(s, s.begin()+2, '6', S("a561432dcb"));
49     test(s, s.begin()+3, '7', S("a5671432dcb"));
50     test(s, s.begin()+4, 'A', S("a567A1432dcb"));
51     test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
52     test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
53   }
54 #if TEST_STD_VER >= 11
55   {
56     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
57     S s;
58     test(s, s.begin(), '1', S("1"));
59     test(s, s.begin(), 'a', S("a1"));
60     test(s, s.end(), 'b', S("a1b"));
61     test(s, s.end()-1, 'c', S("a1cb"));
62     test(s, s.end()-2, 'd', S("a1dcb"));
63     test(s, s.end()-3, '2', S("a12dcb"));
64     test(s, s.end()-4, '3', S("a132dcb"));
65     test(s, s.end()-5, '4', S("a1432dcb"));
66     test(s, s.begin()+1, '5', S("a51432dcb"));
67     test(s, s.begin()+2, '6', S("a561432dcb"));
68     test(s, s.begin()+3, '7', S("a5671432dcb"));
69     test(s, s.begin()+4, 'A', S("a567A1432dcb"));
70     test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
71     test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
72   }
73 #endif
74 
75   return true;
76 }
77 
78 int main(int, char**)
79 {
80   test();
81 #if TEST_STD_VER > 17
82   // static_assert(test());
83 #endif
84 
85   return 0;
86 }
87