| 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 pos1, const basic_string& str); // 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, S 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("" ), S("" )); |
| 48 | test(S("" ), 0, S("12345" ), S("12345" )); |
| 49 | test(S("" ), 0, S("1234567890" ), S("1234567890" )); |
| 50 | test(S("" ), 0, S("12345678901234567890" ), S("12345678901234567890" )); |
| 51 | test(S("" ), 1, S("" ), S("can't happen" )); |
| 52 | test(S("" ), 1, S("12345" ), S("can't happen" )); |
| 53 | test(S("" ), 1, S("1234567890" ), S("can't happen" )); |
| 54 | test(S("" ), 1, S("12345678901234567890" ), S("can't happen" )); |
| 55 | test(S("abcde" ), 0, S("" ), S("abcde" )); |
| 56 | test(S("abcde" ), 0, S("12345" ), S("12345abcde" )); |
| 57 | test(S("abcde" ), 0, S("1234567890" ), S("1234567890abcde" )); |
| 58 | test(S("abcde" ), 0, S("12345678901234567890" ), S("12345678901234567890abcde" )); |
| 59 | test(S("abcde" ), 1, S("" ), S("abcde" )); |
| 60 | test(S("abcde" ), 1, S("12345" ), S("a12345bcde" )); |
| 61 | test(S("abcde" ), 1, S("1234567890" ), S("a1234567890bcde" )); |
| 62 | test(S("abcde" ), 1, S("12345678901234567890" ), S("a12345678901234567890bcde" )); |
| 63 | test(S("abcde" ), 2, S("" ), S("abcde" )); |
| 64 | test(S("abcde" ), 2, S("12345" ), S("ab12345cde" )); |
| 65 | test(S("abcde" ), 2, S("1234567890" ), S("ab1234567890cde" )); |
| 66 | test(S("abcde" ), 2, S("12345678901234567890" ), S("ab12345678901234567890cde" )); |
| 67 | test(S("abcde" ), 4, S("" ), S("abcde" )); |
| 68 | test(S("abcde" ), 4, S("12345" ), S("abcd12345e" )); |
| 69 | test(S("abcde" ), 4, S("1234567890" ), S("abcd1234567890e" )); |
| 70 | test(S("abcde" ), 4, S("12345678901234567890" ), S("abcd12345678901234567890e" )); |
| 71 | test(S("abcde" ), 5, S("" ), S("abcde" )); |
| 72 | test(S("abcde" ), 5, S("12345" ), S("abcde12345" )); |
| 73 | test(S("abcde" ), 5, S("1234567890" ), S("abcde1234567890" )); |
| 74 | test(S("abcde" ), 5, S("12345678901234567890" ), S("abcde12345678901234567890" )); |
| 75 | test(S("abcde" ), 6, S("" ), S("can't happen" )); |
| 76 | test(S("abcde" ), 6, S("12345" ), S("can't happen" )); |
| 77 | test(S("abcde" ), 6, S("1234567890" ), S("can't happen" )); |
| 78 | test(S("abcde" ), 6, S("12345678901234567890" ), S("can't happen" )); |
| 79 | test(S("abcdefghij" ), 0, S("" ), S("abcdefghij" )); |
| 80 | test(S("abcdefghij" ), 0, S("12345" ), S("12345abcdefghij" )); |
| 81 | test(S("abcdefghij" ), 0, S("1234567890" ), S("1234567890abcdefghij" )); |
| 82 | test(S("abcdefghij" ), 0, S("12345678901234567890" ), S("12345678901234567890abcdefghij" )); |
| 83 | test(S("abcdefghij" ), 1, S("" ), S("abcdefghij" )); |
| 84 | test(S("abcdefghij" ), 1, S("12345" ), S("a12345bcdefghij" )); |
| 85 | test(S("abcdefghij" ), 1, S("1234567890" ), S("a1234567890bcdefghij" )); |
| 86 | test(S("abcdefghij" ), 1, S("12345678901234567890" ), S("a12345678901234567890bcdefghij" )); |
| 87 | test(S("abcdefghij" ), 5, S("" ), S("abcdefghij" )); |
| 88 | test(S("abcdefghij" ), 5, S("12345" ), S("abcde12345fghij" )); |
| 89 | test(S("abcdefghij" ), 5, S("1234567890" ), S("abcde1234567890fghij" )); |
| 90 | test(S("abcdefghij" ), 5, S("12345678901234567890" ), S("abcde12345678901234567890fghij" )); |
| 91 | test(S("abcdefghij" ), 9, S("" ), S("abcdefghij" )); |
| 92 | test(S("abcdefghij" ), 9, S("12345" ), S("abcdefghi12345j" )); |
| 93 | test(S("abcdefghij" ), 9, S("1234567890" ), S("abcdefghi1234567890j" )); |
| 94 | test(S("abcdefghij" ), 9, S("12345678901234567890" ), S("abcdefghi12345678901234567890j" )); |
| 95 | test(S("abcdefghij" ), 10, S("" ), S("abcdefghij" )); |
| 96 | test(S("abcdefghij" ), 10, S("12345" ), S("abcdefghij12345" )); |
| 97 | test(S("abcdefghij" ), 10, S("1234567890" ), S("abcdefghij1234567890" )); |
| 98 | test(S("abcdefghij" ), 10, S("12345678901234567890" ), S("abcdefghij12345678901234567890" )); |
| 99 | test(S("abcdefghij" ), 11, S("" ), S("can't happen" )); |
| 100 | test(S("abcdefghij" ), 11, S("12345" ), S("can't happen" )); |
| 101 | test(S("abcdefghij" ), 11, S("1234567890" ), S("can't happen" )); |
| 102 | test(S("abcdefghij" ), 11, S("12345678901234567890" ), S("can't happen" )); |
| 103 | test(S("abcdefghijklmnopqrst" ), 0, S("" ), S("abcdefghijklmnopqrst" )); |
| 104 | test(S("abcdefghijklmnopqrst" ), 0, S("12345" ), S("12345abcdefghijklmnopqrst" )); |
| 105 | test(S("abcdefghijklmnopqrst" ), 0, S("1234567890" ), S("1234567890abcdefghijklmnopqrst" )); |
| 106 | test(S("abcdefghijklmnopqrst" ), 0, S("12345678901234567890" ), S("12345678901234567890abcdefghijklmnopqrst" )); |
| 107 | test(S("abcdefghijklmnopqrst" ), 1, S("" ), S("abcdefghijklmnopqrst" )); |
| 108 | test(S("abcdefghijklmnopqrst" ), 1, S("12345" ), S("a12345bcdefghijklmnopqrst" )); |
| 109 | test(S("abcdefghijklmnopqrst" ), 1, S("1234567890" ), S("a1234567890bcdefghijklmnopqrst" )); |
| 110 | test(S("abcdefghijklmnopqrst" ), 1, S("12345678901234567890" ), S("a12345678901234567890bcdefghijklmnopqrst" )); |
| 111 | test(S("abcdefghijklmnopqrst" ), 10, S("" ), S("abcdefghijklmnopqrst" )); |
| 112 | test(S("abcdefghijklmnopqrst" ), 10, S("12345" ), S("abcdefghij12345klmnopqrst" )); |
| 113 | test(S("abcdefghijklmnopqrst" ), 10, S("1234567890" ), S("abcdefghij1234567890klmnopqrst" )); |
| 114 | test(S("abcdefghijklmnopqrst" ), 10, S("12345678901234567890" ), S("abcdefghij12345678901234567890klmnopqrst" )); |
| 115 | test(S("abcdefghijklmnopqrst" ), 19, S("" ), S("abcdefghijklmnopqrst" )); |
| 116 | test(S("abcdefghijklmnopqrst" ), 19, S("12345" ), S("abcdefghijklmnopqrs12345t" )); |
| 117 | test(S("abcdefghijklmnopqrst" ), 19, S("1234567890" ), S("abcdefghijklmnopqrs1234567890t" )); |
| 118 | test(S("abcdefghijklmnopqrst" ), 19, S("12345678901234567890" ), S("abcdefghijklmnopqrs12345678901234567890t" )); |
| 119 | test(S("abcdefghijklmnopqrst" ), 20, S("" ), S("abcdefghijklmnopqrst" )); |
| 120 | test(S("abcdefghijklmnopqrst" ), 20, S("12345" ), S("abcdefghijklmnopqrst12345" )); |
| 121 | test(S("abcdefghijklmnopqrst" ), 20, S("1234567890" ), S("abcdefghijklmnopqrst1234567890" )); |
| 122 | test(S("abcdefghijklmnopqrst" ), 20, S("12345678901234567890" ), S("abcdefghijklmnopqrst12345678901234567890" )); |
| 123 | test(S("abcdefghijklmnopqrst" ), 21, S("" ), S("can't happen" )); |
| 124 | test(S("abcdefghijklmnopqrst" ), 21, S("12345" ), S("can't happen" )); |
| 125 | test(S("abcdefghijklmnopqrst" ), 21, S("1234567890" ), S("can't happen" )); |
| 126 | test(S("abcdefghijklmnopqrst" ), 21, S("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 | #if TEST_STD_VER >= 11 |
| 137 | { // LWG 2946 |
| 138 | std::string s; |
| 139 | s.insert(0, {"abc" , 1}); |
| 140 | assert(s.size() == 1); |
| 141 | assert(s == "a" ); |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | int main(int, char**) { |
| 149 | test(); |
| 150 | #if TEST_STD_VER > 17 |
| 151 | static_assert(test()); |
| 152 | #endif |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |