| 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 |
// size_type copy(charT* s, size_type n, size_type pos = 0) const; // constexpr since C++20 |
| 12 |
|
| 13 |
#include <string> |
| 14 |
#include <stdexcept> |
| 15 |
#include <algorithm> |
| 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 str, typename S::value_type* s, typename S::size_type n, typename S::size_type pos) { |
| 24 |
const S& cs = str; |
| 25 |
if (pos <= cs.size()) { |
| 26 |
typename S::size_type r = cs.copy(s, n, pos); |
| 27 |
typename S::size_type rlen = std::min(n, cs.size() - pos); |
| 28 |
assert(r == rlen); |
| 29 |
LIBCPP_ASSERT(is_string_asan_correct(str)); |
| 30 |
LIBCPP_ASSERT(is_string_asan_correct(cs)); |
| 31 |
|
| 32 |
for (r = 0; r < rlen; ++r) |
| 33 |
assert(S::traits_type::eq(cs[pos + r], s[r])); |
| 34 |
} |
| 35 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 36 |
else if (!TEST_IS_CONSTANT_EVALUATED) { |
| 37 |
try { |
| 38 |
typename S::size_type r = cs.copy(s, n, pos); |
| 39 |
((void)r); // Prevent unused warning |
| 40 |
assert(false); |
| 41 |
} catch (std::out_of_range&) { |
| 42 |
assert(pos > str.size()); |
| 43 |
} |
| 44 |
} |
| 45 |
#endif |
| 46 |
} |
| 47 |
|
| 48 |
template <class S> |
| 49 |
TEST_CONSTEXPR_CXX20 void test_string() { |
| 50 |
char s[50]; |
| 51 |
test(S(""), s, 0, 0); |
| 52 |
test(S(""), s, 0, 1); |
| 53 |
test(S(""), s, 1, 0); |
| 54 |
test(S("abcde"), s, 0, 0); |
| 55 |
test(S("abcde"), s, 0, 1); |
| 56 |
test(S("abcde"), s, 0, 2); |
| 57 |
test(S("abcde"), s, 0, 4); |
| 58 |
test(S("abcde"), s, 0, 5); |
| 59 |
test(S("abcde"), s, 0, 6); |
| 60 |
test(S("abcde"), s, 1, 0); |
| 61 |
test(S("abcde"), s, 1, 1); |
| 62 |
test(S("abcde"), s, 1, 2); |
| 63 |
test(S("abcde"), s, 1, 4); |
| 64 |
test(S("abcde"), s, 1, 5); |
| 65 |
test(S("abcde"), s, 2, 0); |
| 66 |
test(S("abcde"), s, 2, 1); |
| 67 |
test(S("abcde"), s, 2, 2); |
| 68 |
test(S("abcde"), s, 2, 4); |
| 69 |
test(S("abcde"), s, 4, 0); |
| 70 |
test(S("abcde"), s, 4, 1); |
| 71 |
test(S("abcde"), s, 4, 2); |
| 72 |
test(S("abcde"), s, 5, 0); |
| 73 |
test(S("abcde"), s, 5, 1); |
| 74 |
test(S("abcde"), s, 6, 0); |
| 75 |
test(S("abcdefghijklmnopqrst"), s, 0, 0); |
| 76 |
test(S("abcdefghijklmnopqrst"), s, 0, 1); |
| 77 |
test(S("abcdefghijklmnopqrst"), s, 0, 2); |
| 78 |
test(S("abcdefghijklmnopqrst"), s, 0, 10); |
| 79 |
test(S("abcdefghijklmnopqrst"), s, 0, 19); |
| 80 |
test(S("abcdefghijklmnopqrst"), s, 0, 20); |
| 81 |
test(S("abcdefghijklmnopqrst"), s, 0, 21); |
| 82 |
test(S("abcdefghijklmnopqrst"), s, 1, 0); |
| 83 |
test(S("abcdefghijklmnopqrst"), s, 1, 1); |
| 84 |
test(S("abcdefghijklmnopqrst"), s, 1, 2); |
| 85 |
test(S("abcdefghijklmnopqrst"), s, 1, 9); |
| 86 |
test(S("abcdefghijklmnopqrst"), s, 1, 18); |
| 87 |
test(S("abcdefghijklmnopqrst"), s, 1, 19); |
| 88 |
test(S("abcdefghijklmnopqrst"), s, 1, 20); |
| 89 |
test(S("abcdefghijklmnopqrst"), s, 2, 0); |
| 90 |
test(S("abcdefghijklmnopqrst"), s, 2, 1); |
| 91 |
test(S("abcdefghijklmnopqrst"), s, 2, 2); |
| 92 |
test(S("abcdefghijklmnopqrst"), s, 2, 9); |
| 93 |
test(S("abcdefghijklmnopqrst"), s, 2, 17); |
| 94 |
test(S("abcdefghijklmnopqrst"), s, 2, 18); |
| 95 |
test(S("abcdefghijklmnopqrst"), s, 2, 19); |
| 96 |
test(S("abcdefghijklmnopqrst"), s, 10, 0); |
| 97 |
test(S("abcdefghijklmnopqrst"), s, 10, 1); |
| 98 |
test(S("abcdefghijklmnopqrst"), s, 10, 2); |
| 99 |
test(S("abcdefghijklmnopqrst"), s, 10, 5); |
| 100 |
test(S("abcdefghijklmnopqrst"), s, 10, 9); |
| 101 |
test(S("abcdefghijklmnopqrst"), s, 10, 10); |
| 102 |
test(S("abcdefghijklmnopqrst"), s, 10, 11); |
| 103 |
test(S("abcdefghijklmnopqrst"), s, 19, 0); |
| 104 |
test(S("abcdefghijklmnopqrst"), s, 19, 1); |
| 105 |
test(S("abcdefghijklmnopqrst"), s, 19, 2); |
| 106 |
test(S("abcdefghijklmnopqrst"), s, 20, 0); |
| 107 |
test(S("abcdefghijklmnopqrst"), s, 20, 1); |
| 108 |
test(S("abcdefghijklmnopqrst"), s, 21, 0); |
| 109 |
test(S("abcdefghijklmnopqrstabcdefghijklmnopqrst"), s, 40, 1); |
| 110 |
test(S("abcdefghijklmnopqrstabcdefghijklmnopqrst"), s, 40, 0); |
| 111 |
} |
| 112 |
|
| 113 |
TEST_CONSTEXPR_CXX20 bool test() { |
| 114 |
test_string<std::string>(); |
| 115 |
#if TEST_STD_VER >= 11 |
| 116 |
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); |
| 117 |
test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>(); |
| 118 |
#endif |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
int main(int, char**) { |
| 124 |
test(); |
| 125 |
#if TEST_STD_VER > 17 |
| 126 |
static_assert(test()); |
| 127 |
#endif |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |