xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/iter_char.pass.cpp (revision 425620ccdd47e56b59512913cdc71e116f951e4e)
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 // XFAIL: LIBCXX-AIX-FIXME
10 
11 // <string>
12 
13 // iterator insert(const_iterator p, charT c); // constexpr since C++20
14 
15 #include <string>
16 #include <stdexcept>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "min_allocator.h"
21 
22 template <class S>
23 TEST_CONSTEXPR_CXX20 void
24 test(S& s, typename S::const_iterator p, typename S::value_type c, S expected)
25 {
26     bool sufficient_cap = s.size() < s.capacity();
27     typename S::difference_type pos = p - s.begin();
28     typename S::iterator i = s.insert(p, c);
29     LIBCPP_ASSERT(s.__invariants());
30     assert(s == expected);
31     assert(i - s.begin() == pos);
32     assert(*i == c);
33     if (sufficient_cap)
34         assert(i == p);
35 }
36 
37 TEST_CONSTEXPR_CXX20 bool test() {
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   return true;
78 }
79 
80 int main(int, char**)
81 {
82   test();
83 #if TEST_STD_VER > 17
84   static_assert(test());
85 #endif
86 
87   return 0;
88 }
89