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