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