xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/iter_char.pass.cpp (revision c77cdbac9b121611121adf5806a99aff4812a40c)
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 test(S& s, typename S::const_iterator p, typename S::value_type c, S expected) {
22   bool sufficient_cap             = s.size() < s.capacity();
23   typename S::difference_type pos = p - s.begin();
24   typename S::iterator i          = s.insert(p, c);
25   LIBCPP_ASSERT(s.__invariants());
26   assert(s == expected);
27   assert(i - s.begin() == pos);
28   assert(*i == c);
29   if (sufficient_cap)
30     assert(i == p);
31 }
32 
33 template <class S>
34 TEST_CONSTEXPR_CXX20 void test_string() {
35   S s;
36   test(s, s.begin(), '1', S("1"));
37   test(s, s.begin(), 'a', S("a1"));
38   test(s, s.end(), 'b', S("a1b"));
39   test(s, s.end() - 1, 'c', S("a1cb"));
40   test(s, s.end() - 2, 'd', S("a1dcb"));
41   test(s, s.end() - 3, '2', S("a12dcb"));
42   test(s, s.end() - 4, '3', S("a132dcb"));
43   test(s, s.end() - 5, '4', S("a1432dcb"));
44   test(s, s.begin() + 1, '5', S("a51432dcb"));
45   test(s, s.begin() + 2, '6', S("a561432dcb"));
46   test(s, s.begin() + 3, '7', S("a5671432dcb"));
47   test(s, s.begin() + 4, 'A', S("a567A1432dcb"));
48   test(s, s.begin() + 5, 'B', S("a567AB1432dcb"));
49   test(s, s.begin() + 6, 'C', S("a567ABC1432dcb"));
50   test(s, s.begin(), 'x', S("xa567ABC1432dcb"));
51   test(s, s.begin(), 'x', S("xxa567ABC1432dcb"));
52   test(s, s.begin(), 'x', S("xxxa567ABC1432dcb"));
53   test(s, s.begin(), 'x', S("xxxxa567ABC1432dcb"));
54   test(s, s.begin(), 'x', S("xxxxxa567ABC1432dcb"));
55   test(s, s.begin(), 'x', S("xxxxxxa567ABC1432dcb"));
56   test(s, s.begin(), 'x', S("xxxxxxxa567ABC1432dcb"));
57   test(s, s.begin(), 'x', S("xxxxxxxxa567ABC1432dcb"));
58   test(s, s.begin(), 'x', S("xxxxxxxxxa567ABC1432dcb"));
59   test(s, s.begin(), 'x', S("xxxxxxxxxxa567ABC1432dcb"));
60   test(s, s.begin(), 'x', S("xxxxxxxxxxxa567ABC1432dcb"));
61   test(s, s.begin(), 'x', S("xxxxxxxxxxxxa567ABC1432dcb"));
62   test(s, s.begin(), 'x', S("xxxxxxxxxxxxxa567ABC1432dcb"));
63   test(s, s.begin(), 'x', S("xxxxxxxxxxxxxxa567ABC1432dcb"));
64   test(s, s.begin(), 'x', S("xxxxxxxxxxxxxxxa567ABC1432dcb"));
65   test(s, s.begin(), 'x', S("xxxxxxxxxxxxxxxxa567ABC1432dcb"));
66   test(s, s.begin() + 1, 'x', S("xxxxxxxxxxxxxxxxxa567ABC1432dcb"));
67 }
68 
69 TEST_CONSTEXPR_CXX20 bool test() {
70   test_string<std::string>();
71 #if TEST_STD_VER >= 11
72   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
73 #endif
74 
75   return true;
76 }
77 
78 int main(int, char**) {
79   test();
80 #if TEST_STD_VER > 17
81   static_assert(test());
82 #endif
83 
84   return 0;
85 }
86