| 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 |
// UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14) |
| 10 |
|
| 11 |
// <string_view> |
| 12 |
|
| 13 |
// size_type copy(charT* s, size_type n, size_type pos = 0) const; |
| 14 |
|
| 15 |
// Throws: out_of_range if pos > size(). |
| 16 |
// Remarks: Let rlen be the smaller of n and size() - pos. |
| 17 |
// Requires: [s, s+rlen) is a valid range. |
| 18 |
// Effects: Equivalent to std::copy_n(begin() + pos, rlen, s). |
| 19 |
// Returns: rlen. |
| 20 |
|
| 21 |
#include <string_view> |
| 22 |
#include <algorithm> |
| 23 |
#include <cassert> |
| 24 |
#include <stdexcept> |
| 25 |
|
| 26 |
#include "test_macros.h" |
| 27 |
|
| 28 |
template <typename CharT> |
| 29 |
void test1(std::basic_string_view<CharT> sv, std::size_t n, size_t pos) { |
| 30 |
const std::size_t rlen = std::min(n, sv.size() - pos); |
| 31 |
|
| 32 |
CharT* dest1 = new CharT[rlen + 1]; |
| 33 |
dest1[rlen] = 0; |
| 34 |
CharT* dest2 = new CharT[rlen + 1]; |
| 35 |
dest2[rlen] = 0; |
| 36 |
|
| 37 |
if (pos > sv.size()) { |
| 38 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 39 |
try { |
| 40 |
sv.copy(dest1, n, pos); |
| 41 |
assert(false); |
| 42 |
} catch (const std::out_of_range&) { |
| 43 |
} catch (...) { |
| 44 |
assert(false); |
| 45 |
} |
| 46 |
#endif |
| 47 |
} else { |
| 48 |
sv.copy(dest1, n, pos); |
| 49 |
std::copy_n(sv.begin() + pos, rlen, dest2); |
| 50 |
for (std::size_t i = 0; i <= rlen; ++i) |
| 51 |
assert(dest1[i] == dest2[i]); |
| 52 |
} |
| 53 |
delete[] dest1; |
| 54 |
delete[] dest2; |
| 55 |
} |
| 56 |
|
| 57 |
template <typename CharT> |
| 58 |
void test(const CharT* s) { |
| 59 |
typedef std::basic_string_view<CharT> string_view_t; |
| 60 |
|
| 61 |
string_view_t sv1(s); |
| 62 |
|
| 63 |
test1(sv1, 0, 0); |
| 64 |
test1(sv1, 1, 0); |
| 65 |
test1(sv1, 20, 0); |
| 66 |
test1(sv1, sv1.size(), 0); |
| 67 |
test1(sv1, 20, string_view_t::npos); |
| 68 |
|
| 69 |
test1(sv1, 0, 3); |
| 70 |
test1(sv1, 2, 3); |
| 71 |
test1(sv1, 100, 3); |
| 72 |
test1(sv1, 100, string_view_t::npos); |
| 73 |
|
| 74 |
test1(sv1, sv1.size(), string_view_t::npos); |
| 75 |
|
| 76 |
test1(sv1, sv1.size() + 1, 0); |
| 77 |
test1(sv1, sv1.size() + 1, 1); |
| 78 |
test1(sv1, sv1.size() + 1, string_view_t::npos); |
| 79 |
} |
| 80 |
|
| 81 |
template <typename CharT> |
| 82 |
TEST_CONSTEXPR_CXX20 bool test_constexpr_copy(const CharT* abcde, const CharT* ghijk, const CharT* bcdjk) { |
| 83 |
CharT buf[6] = {}; |
| 84 |
std::basic_string_view<CharT> lval(ghijk); |
| 85 |
lval.copy(buf, 6); |
| 86 |
std::basic_string_view<CharT>(abcde).copy(buf, 3, 1); |
| 87 |
assert(std::basic_string_view<CharT>(buf) == bcdjk); |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
int main(int, char**) { |
| 92 |
test("ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE"); |
| 93 |
test("ABCDE"); |
| 94 |
test("a"); |
| 95 |
test(""); |
| 96 |
|
| 97 |
#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 98 |
test(L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE"); |
| 99 |
test(L"ABCDE"); |
| 100 |
test(L"a"); |
| 101 |
test(L""); |
| 102 |
#endif |
| 103 |
|
| 104 |
#if TEST_STD_VER >= 11 |
| 105 |
test(u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE"); |
| 106 |
test(u"ABCDE"); |
| 107 |
test(u"a"); |
| 108 |
test(u""); |
| 109 |
|
| 110 |
test(U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE"); |
| 111 |
test(U"ABCDE"); |
| 112 |
test(U"a"); |
| 113 |
test(U""); |
| 114 |
#endif |
| 115 |
|
| 116 |
test_constexpr_copy("ABCDE", "GHIJK", "BCDJK"); |
| 117 |
#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 118 |
test_constexpr_copy(L"ABCDE", L"GHIJK", L"BCDJK"); |
| 119 |
#endif |
| 120 |
#if TEST_STD_VER >= 11 |
| 121 |
test_constexpr_copy(u"ABCDE", u"GHIJK", u"BCDJK"); |
| 122 |
test_constexpr_copy(U"ABCDE", U"GHIJK", U"BCDJK"); |
| 123 |
#endif |
| 124 |
#if TEST_STD_VER >= 17 |
| 125 |
test_constexpr_copy(u8"ABCDE", u8"GHIJK", u8"BCDJK"); |
| 126 |
#endif |
| 127 |
#if TEST_STD_VER >= 20 |
| 128 |
static_assert(test_constexpr_copy("ABCDE", "GHIJK", "BCDJK")); |
| 129 |
# ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 130 |
static_assert(test_constexpr_copy(L"ABCDE", L"GHIJK", L"BCDJK")); |
| 131 |
# endif |
| 132 |
static_assert(test_constexpr_copy(u"ABCDE", u"GHIJK", u"BCDJK")); |
| 133 |
static_assert(test_constexpr_copy(U"ABCDE", U"GHIJK", U"BCDJK")); |
| 134 |
static_assert(test_constexpr_copy(u8"ABCDE", u8"GHIJK", u8"BCDJK")); |
| 135 |
#endif |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |