| 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 | // basic_string<charT,traits,Allocator>& |
| 12 | // insert(size_type pos, string_view sv); // constexpr since C++20 |
| 13 | |
| 14 | #include <string> |
| 15 | #include <stdexcept> |
| 16 | #include <cassert> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #include "min_allocator.h" |
| 20 | |
| 21 | template <class S, class SV> |
| 22 | TEST_CONSTEXPR_CXX20 void test(S s, typename S::size_type pos, SV sv, S expected) { |
| 23 | const typename S::size_type old_size = s.size(); |
| 24 | S s0 = s; |
| 25 | if (pos <= old_size) { |
| 26 | s.insert(pos, sv); |
| 27 | LIBCPP_ASSERT(s.__invariants()); |
| 28 | assert(s == expected); |
| 29 | } |
| 30 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 31 | else if (!TEST_IS_CONSTANT_EVALUATED) { |
| 32 | try { |
| 33 | s.insert(pos, sv); |
| 34 | assert(false); |
| 35 | } catch (std::out_of_range&) { |
| 36 | assert(pos > old_size); |
| 37 | assert(s == s0); |
| 38 | } |
| 39 | } |
| 40 | #endif |
| 41 | } |
| 42 | |
| 43 | template <class S> |
| 44 | TEST_CONSTEXPR_CXX20 void test_string() { |
| 45 | typedef std::string_view SV; |
| 46 | test(S("" ), 0, SV("" ), S("" )); |
| 47 | test(S("" ), 0, SV("12345" ), S("12345" )); |
| 48 | test(S("" ), 0, SV("1234567890" ), S("1234567890" )); |
| 49 | test(S("" ), 0, SV("12345678901234567890" ), S("12345678901234567890" )); |
| 50 | test(S("" ), 1, SV("" ), S("can't happen" )); |
| 51 | test(S("" ), 1, SV("12345" ), S("can't happen" )); |
| 52 | test(S("" ), 1, SV("1234567890" ), S("can't happen" )); |
| 53 | test(S("" ), 1, SV("12345678901234567890" ), S("can't happen" )); |
| 54 | test(S("abcde" ), 0, SV("" ), S("abcde" )); |
| 55 | test(S("abcde" ), 0, SV("12345" ), S("12345abcde" )); |
| 56 | test(S("abcde" ), 0, SV("1234567890" ), S("1234567890abcde" )); |
| 57 | test(S("abcde" ), 0, SV("12345678901234567890" ), S("12345678901234567890abcde" )); |
| 58 | test(S("abcde" ), 1, SV("" ), S("abcde" )); |
| 59 | test(S("abcde" ), 1, SV("12345" ), S("a12345bcde" )); |
| 60 | test(S("abcde" ), 1, SV("1234567890" ), S("a1234567890bcde" )); |
| 61 | test(S("abcde" ), 1, SV("12345678901234567890" ), S("a12345678901234567890bcde" )); |
| 62 | test(S("abcde" ), 2, SV("" ), S("abcde" )); |
| 63 | test(S("abcde" ), 2, SV("12345" ), S("ab12345cde" )); |
| 64 | test(S("abcde" ), 2, SV("1234567890" ), S("ab1234567890cde" )); |
| 65 | test(S("abcde" ), 2, SV("12345678901234567890" ), S("ab12345678901234567890cde" )); |
| 66 | test(S("abcde" ), 4, SV("" ), S("abcde" )); |
| 67 | test(S("abcde" ), 4, SV("12345" ), S("abcd12345e" )); |
| 68 | test(S("abcde" ), 4, SV("1234567890" ), S("abcd1234567890e" )); |
| 69 | test(S("abcde" ), 4, SV("12345678901234567890" ), S("abcd12345678901234567890e" )); |
| 70 | test(S("abcde" ), 5, SV("" ), S("abcde" )); |
| 71 | test(S("abcde" ), 5, SV("12345" ), S("abcde12345" )); |
| 72 | test(S("abcde" ), 5, SV("1234567890" ), S("abcde1234567890" )); |
| 73 | test(S("abcde" ), 5, SV("12345678901234567890" ), S("abcde12345678901234567890" )); |
| 74 | test(S("abcde" ), 6, SV("" ), S("can't happen" )); |
| 75 | test(S("abcde" ), 6, SV("12345" ), S("can't happen" )); |
| 76 | test(S("abcde" ), 6, SV("1234567890" ), S("can't happen" )); |
| 77 | test(S("abcde" ), 6, SV("12345678901234567890" ), S("can't happen" )); |
| 78 | test(S("abcdefghij" ), 0, SV("" ), S("abcdefghij" )); |
| 79 | test(S("abcdefghij" ), 0, SV("12345" ), S("12345abcdefghij" )); |
| 80 | test(S("abcdefghij" ), 0, SV("1234567890" ), S("1234567890abcdefghij" )); |
| 81 | test(S("abcdefghij" ), 0, SV("12345678901234567890" ), S("12345678901234567890abcdefghij" )); |
| 82 | test(S("abcdefghij" ), 1, SV("" ), S("abcdefghij" )); |
| 83 | test(S("abcdefghij" ), 1, SV("12345" ), S("a12345bcdefghij" )); |
| 84 | test(S("abcdefghij" ), 1, SV("1234567890" ), S("a1234567890bcdefghij" )); |
| 85 | test(S("abcdefghij" ), 1, SV("12345678901234567890" ), S("a12345678901234567890bcdefghij" )); |
| 86 | test(S("abcdefghij" ), 5, SV("" ), S("abcdefghij" )); |
| 87 | test(S("abcdefghij" ), 5, SV("12345" ), S("abcde12345fghij" )); |
| 88 | test(S("abcdefghij" ), 5, SV("1234567890" ), S("abcde1234567890fghij" )); |
| 89 | test(S("abcdefghij" ), 5, SV("12345678901234567890" ), S("abcde12345678901234567890fghij" )); |
| 90 | test(S("abcdefghij" ), 9, SV("" ), S("abcdefghij" )); |
| 91 | test(S("abcdefghij" ), 9, SV("12345" ), S("abcdefghi12345j" )); |
| 92 | test(S("abcdefghij" ), 9, SV("1234567890" ), S("abcdefghi1234567890j" )); |
| 93 | test(S("abcdefghij" ), 9, SV("12345678901234567890" ), S("abcdefghi12345678901234567890j" )); |
| 94 | test(S("abcdefghij" ), 10, SV("" ), S("abcdefghij" )); |
| 95 | test(S("abcdefghij" ), 10, SV("12345" ), S("abcdefghij12345" )); |
| 96 | test(S("abcdefghij" ), 10, SV("1234567890" ), S("abcdefghij1234567890" )); |
| 97 | test(S("abcdefghij" ), 10, SV("12345678901234567890" ), S("abcdefghij12345678901234567890" )); |
| 98 | test(S("abcdefghij" ), 11, SV("" ), S("can't happen" )); |
| 99 | test(S("abcdefghij" ), 11, SV("12345" ), S("can't happen" )); |
| 100 | test(S("abcdefghij" ), 11, SV("1234567890" ), S("can't happen" )); |
| 101 | test(S("abcdefghij" ), 11, SV("12345678901234567890" ), S("can't happen" )); |
| 102 | test(S("abcdefghijklmnopqrst" ), 0, SV("" ), S("abcdefghijklmnopqrst" )); |
| 103 | test(S("abcdefghijklmnopqrst" ), 0, SV("12345" ), S("12345abcdefghijklmnopqrst" )); |
| 104 | test(S("abcdefghijklmnopqrst" ), 0, SV("1234567890" ), S("1234567890abcdefghijklmnopqrst" )); |
| 105 | test(S("abcdefghijklmnopqrst" ), 0, SV("12345678901234567890" ), S("12345678901234567890abcdefghijklmnopqrst" )); |
| 106 | test(S("abcdefghijklmnopqrst" ), 1, SV("" ), S("abcdefghijklmnopqrst" )); |
| 107 | test(S("abcdefghijklmnopqrst" ), 1, SV("12345" ), S("a12345bcdefghijklmnopqrst" )); |
| 108 | test(S("abcdefghijklmnopqrst" ), 1, SV("1234567890" ), S("a1234567890bcdefghijklmnopqrst" )); |
| 109 | test(S("abcdefghijklmnopqrst" ), 1, SV("12345678901234567890" ), S("a12345678901234567890bcdefghijklmnopqrst" )); |
| 110 | test(S("abcdefghijklmnopqrst" ), 10, SV("" ), S("abcdefghijklmnopqrst" )); |
| 111 | test(S("abcdefghijklmnopqrst" ), 10, SV("12345" ), S("abcdefghij12345klmnopqrst" )); |
| 112 | test(S("abcdefghijklmnopqrst" ), 10, SV("1234567890" ), S("abcdefghij1234567890klmnopqrst" )); |
| 113 | test(S("abcdefghijklmnopqrst" ), 10, SV("12345678901234567890" ), S("abcdefghij12345678901234567890klmnopqrst" )); |
| 114 | test(S("abcdefghijklmnopqrst" ), 19, SV("" ), S("abcdefghijklmnopqrst" )); |
| 115 | test(S("abcdefghijklmnopqrst" ), 19, SV("12345" ), S("abcdefghijklmnopqrs12345t" )); |
| 116 | test(S("abcdefghijklmnopqrst" ), 19, SV("1234567890" ), S("abcdefghijklmnopqrs1234567890t" )); |
| 117 | test(S("abcdefghijklmnopqrst" ), 19, SV("12345678901234567890" ), S("abcdefghijklmnopqrs12345678901234567890t" )); |
| 118 | test(S("abcdefghijklmnopqrst" ), 20, SV("" ), S("abcdefghijklmnopqrst" )); |
| 119 | test(S("abcdefghijklmnopqrst" ), 20, SV("12345" ), S("abcdefghijklmnopqrst12345" )); |
| 120 | test(S("abcdefghijklmnopqrst" ), 20, SV("1234567890" ), S("abcdefghijklmnopqrst1234567890" )); |
| 121 | test(S("abcdefghijklmnopqrst" ), 20, SV("12345678901234567890" ), S("abcdefghijklmnopqrst12345678901234567890" )); |
| 122 | test(S("abcdefghijklmnopqrst" ), 21, SV("" ), S("can't happen" )); |
| 123 | test(S("abcdefghijklmnopqrst" ), 21, SV("12345" ), S("can't happen" )); |
| 124 | test(S("abcdefghijklmnopqrst" ), 21, SV("1234567890" ), S("can't happen" )); |
| 125 | test(S("abcdefghijklmnopqrst" ), 21, SV("12345678901234567890" ), S("can't happen" )); |
| 126 | } |
| 127 | |
| 128 | TEST_CONSTEXPR_CXX20 bool test() { |
| 129 | test_string<std::string>(); |
| 130 | #if TEST_STD_VER >= 11 |
| 131 | test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); |
| 132 | #endif |
| 133 | |
| 134 | { // test inserting into self |
| 135 | typedef std::string S; |
| 136 | S s_short = "123/" ; |
| 137 | S s_long = "Lorem ipsum dolor sit amet, consectetur/" ; |
| 138 | |
| 139 | s_short.insert(pos: 0, s: s_short.c_str()); |
| 140 | assert(s_short == "123/123/" ); |
| 141 | s_short.insert(pos: 0, s: s_short.c_str()); |
| 142 | assert(s_short == "123/123/123/123/" ); |
| 143 | s_short.insert(pos: 0, s: s_short.c_str()); |
| 144 | assert(s_short == "123/123/123/123/123/123/123/123/" ); |
| 145 | |
| 146 | s_long.insert(pos: 0, s: s_long.c_str()); |
| 147 | assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/" ); |
| 148 | } |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | int main(int, char**) { |
| 154 | test(); |
| 155 | #if TEST_STD_VER > 17 |
| 156 | static_assert(test()); |
| 157 | #endif |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |