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