| 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, size_type n, charT c); // constexpr since C++20 |
| 12 | |
| 13 | #include <string> |
| 14 | #include <cassert> |
| 15 | |
| 16 | #include "test_macros.h" |
| 17 | #include "min_allocator.h" |
| 18 | #include "asan_testing.h" |
| 19 | |
| 20 | template <class S> |
| 21 | TEST_CONSTEXPR_CXX20 void |
| 22 | test(S s, typename S::difference_type pos, typename S::size_type n, typename S::value_type c, S expected) { |
| 23 | typename S::const_iterator p = s.cbegin() + pos; |
| 24 | typename S::iterator i = s.insert(p, n, c); |
| 25 | LIBCPP_ASSERT(s.__invariants()); |
| 26 | assert(i - s.begin() == pos); |
| 27 | assert(s == expected); |
| 28 | LIBCPP_ASSERT(is_string_asan_correct(s)); |
| 29 | } |
| 30 | |
| 31 | template <class S> |
| 32 | TEST_CONSTEXPR_CXX20 void test_string() { |
| 33 | test(S("" ), 0, 0, '1', S("" )); |
| 34 | test(S("" ), 0, 5, '1', S("11111" )); |
| 35 | test(S("" ), 0, 10, '1', S("1111111111" )); |
| 36 | test(S("" ), 0, 20, '1', S("11111111111111111111" )); |
| 37 | test(S("abcde" ), 0, 0, '1', S("abcde" )); |
| 38 | test(S("abcde" ), 0, 5, '1', S("11111abcde" )); |
| 39 | test(S("abcde" ), 0, 10, '1', S("1111111111abcde" )); |
| 40 | test(S("abcde" ), 0, 20, '1', S("11111111111111111111abcde" )); |
| 41 | test(S("abcde" ), 1, 0, '1', S("abcde" )); |
| 42 | test(S("abcde" ), 1, 5, '1', S("a11111bcde" )); |
| 43 | test(S("abcde" ), 1, 10, '1', S("a1111111111bcde" )); |
| 44 | test(S("abcde" ), 1, 20, '1', S("a11111111111111111111bcde" )); |
| 45 | test(S("abcde" ), 2, 0, '1', S("abcde" )); |
| 46 | test(S("abcde" ), 2, 5, '1', S("ab11111cde" )); |
| 47 | test(S("abcde" ), 2, 10, '1', S("ab1111111111cde" )); |
| 48 | test(S("abcde" ), 2, 20, '1', S("ab11111111111111111111cde" )); |
| 49 | test(S("abcde" ), 4, 0, '1', S("abcde" )); |
| 50 | test(S("abcde" ), 4, 5, '1', S("abcd11111e" )); |
| 51 | test(S("abcde" ), 4, 10, '1', S("abcd1111111111e" )); |
| 52 | test(S("abcde" ), 4, 20, '1', S("abcd11111111111111111111e" )); |
| 53 | test(S("abcde" ), 5, 0, '1', S("abcde" )); |
| 54 | test(S("abcde" ), 5, 5, '1', S("abcde11111" )); |
| 55 | test(S("abcde" ), 5, 10, '1', S("abcde1111111111" )); |
| 56 | test(S("abcde" ), 5, 20, '1', S("abcde11111111111111111111" )); |
| 57 | test(S("abcdefghij" ), 0, 0, '1', S("abcdefghij" )); |
| 58 | test(S("abcdefghij" ), 0, 5, '1', S("11111abcdefghij" )); |
| 59 | test(S("abcdefghij" ), 0, 10, '1', S("1111111111abcdefghij" )); |
| 60 | test(S("abcdefghij" ), 0, 20, '1', S("11111111111111111111abcdefghij" )); |
| 61 | test(S("abcdefghij" ), 1, 0, '1', S("abcdefghij" )); |
| 62 | test(S("abcdefghij" ), 1, 5, '1', S("a11111bcdefghij" )); |
| 63 | test(S("abcdefghij" ), 1, 10, '1', S("a1111111111bcdefghij" )); |
| 64 | test(S("abcdefghij" ), 1, 20, '1', S("a11111111111111111111bcdefghij" )); |
| 65 | test(S("abcdefghij" ), 5, 0, '1', S("abcdefghij" )); |
| 66 | test(S("abcdefghij" ), 5, 5, '1', S("abcde11111fghij" )); |
| 67 | test(S("abcdefghij" ), 5, 10, '1', S("abcde1111111111fghij" )); |
| 68 | test(S("abcdefghij" ), 5, 20, '1', S("abcde11111111111111111111fghij" )); |
| 69 | test(S("abcdefghij" ), 9, 0, '1', S("abcdefghij" )); |
| 70 | test(S("abcdefghij" ), 9, 5, '1', S("abcdefghi11111j" )); |
| 71 | test(S("abcdefghij" ), 9, 10, '1', S("abcdefghi1111111111j" )); |
| 72 | test(S("abcdefghij" ), 9, 20, '1', S("abcdefghi11111111111111111111j" )); |
| 73 | test(S("abcdefghij" ), 10, 0, '1', S("abcdefghij" )); |
| 74 | test(S("abcdefghij" ), 10, 5, '1', S("abcdefghij11111" )); |
| 75 | test(S("abcdefghij" ), 10, 10, '1', S("abcdefghij1111111111" )); |
| 76 | test(S("abcdefghij" ), 10, 20, '1', S("abcdefghij11111111111111111111" )); |
| 77 | test(S("abcdefghijklmnopqrst" ), 0, 0, '1', S("abcdefghijklmnopqrst" )); |
| 78 | test(S("abcdefghijklmnopqrst" ), 0, 5, '1', S("11111abcdefghijklmnopqrst" )); |
| 79 | test(S("abcdefghijklmnopqrst" ), 0, 10, '1', S("1111111111abcdefghijklmnopqrst" )); |
| 80 | test(S("abcdefghijklmnopqrst" ), 0, 20, '1', S("11111111111111111111abcdefghijklmnopqrst" )); |
| 81 | test(S("abcdefghijklmnopqrst" ), 1, 0, '1', S("abcdefghijklmnopqrst" )); |
| 82 | test(S("abcdefghijklmnopqrst" ), 1, 5, '1', S("a11111bcdefghijklmnopqrst" )); |
| 83 | test(S("abcdefghijklmnopqrst" ), 1, 10, '1', S("a1111111111bcdefghijklmnopqrst" )); |
| 84 | test(S("abcdefghijklmnopqrst" ), 1, 20, '1', S("a11111111111111111111bcdefghijklmnopqrst" )); |
| 85 | test(S("abcdefghijklmnopqrst" ), 10, 0, '1', S("abcdefghijklmnopqrst" )); |
| 86 | test(S("abcdefghijklmnopqrst" ), 10, 5, '1', S("abcdefghij11111klmnopqrst" )); |
| 87 | test(S("abcdefghijklmnopqrst" ), 10, 10, '1', S("abcdefghij1111111111klmnopqrst" )); |
| 88 | test(S("abcdefghijklmnopqrst" ), 10, 20, '1', S("abcdefghij11111111111111111111klmnopqrst" )); |
| 89 | test(S("abcdefghijklmnopqrst" ), 19, 0, '1', S("abcdefghijklmnopqrst" )); |
| 90 | test(S("abcdefghijklmnopqrst" ), 19, 5, '1', S("abcdefghijklmnopqrs11111t" )); |
| 91 | test(S("abcdefghijklmnopqrst" ), 19, 10, '1', S("abcdefghijklmnopqrs1111111111t" )); |
| 92 | test(S("abcdefghijklmnopqrst" ), 19, 20, '1', S("abcdefghijklmnopqrs11111111111111111111t" )); |
| 93 | test(S("abcdefghijklmnopqrst" ), 20, 0, '1', S("abcdefghijklmnopqrst" )); |
| 94 | test(S("abcdefghijklmnopqrst" ), 20, 5, '1', S("abcdefghijklmnopqrst11111" )); |
| 95 | test(S("abcdefghijklmnopqrst" ), 20, 10, '1', S("abcdefghijklmnopqrst1111111111" )); |
| 96 | test(S("abcdefghijklmnopqrst" ), 20, 20, '1', S("abcdefghijklmnopqrst11111111111111111111" )); |
| 97 | } |
| 98 | |
| 99 | TEST_CONSTEXPR_CXX20 bool test() { |
| 100 | test_string<std::string>(); |
| 101 | #if TEST_STD_VER >= 11 |
| 102 | test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); |
| 103 | test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>(); |
| 104 | #endif |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | int main(int, char**) { |
| 110 | test(); |
| 111 | #if TEST_STD_VER > 17 |
| 112 | static_assert(test()); |
| 113 | #endif |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |