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

source code of libcxx/test/std/strings/string.view/string.view.ops/copy.pass.cpp