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 // GCC's __builtin_strlen isn't constexpr yet
10 // XFAIL: gcc-11 && !(c++11 || c++14 || c++17)
11 // UNSUPPORTED: LIBCXX-DEBUG-FIXME
12 
13 // <string_view>
14 
15 // size_type copy(charT* s, size_type n, size_type pos = 0) const;
16 
17 // Throws: out_of_range if pos > size().
18 // Remarks: Let rlen be the smaller of n and size() - pos.
19 // Requires: [s, s+rlen) is a valid range.
20 // Effects: Equivalent to std::copy_n(begin() + pos, rlen, s).
21 // Returns: rlen.
22 
23 
24 #include <string_view>
25 #include <algorithm>
26 #include <cassert>
27 #include <stdexcept>
28 
29 #include "test_macros.h"
30 
31 template<typename CharT>
32 void test1 ( std::basic_string_view<CharT> sv, size_t n, size_t pos ) {
33     const size_t rlen = std::min ( n, sv.size() - pos );
34 
35     CharT *dest1 = new CharT [rlen + 1];    dest1[rlen] = 0;
36     CharT *dest2 = new CharT [rlen + 1];    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 ( size_t i = 0; i <= rlen; ++i )
52             assert ( dest1[i] == dest2[i] );
53     }
54     delete [] dest1;
55     delete [] dest2;
56 }
57 
58 
59 template<typename CharT>
60 void test ( const CharT *s ) {
61     typedef std::basic_string_view<CharT> string_view_t;
62 
63     string_view_t sv1 ( s );
64 
65     test1(sv1,  0, 0);
66     test1(sv1,  1, 0);
67     test1(sv1, 20, 0);
68     test1(sv1, sv1.size(), 0);
69     test1(sv1, 20, string_view_t::npos);
70 
71     test1(sv1,   0, 3);
72     test1(sv1,   2, 3);
73     test1(sv1, 100, 3);
74     test1(sv1, 100, string_view_t::npos);
75 
76     test1(sv1, sv1.size(), string_view_t::npos);
77 
78     test1(sv1, sv1.size() + 1, 0);
79     test1(sv1, sv1.size() + 1, 1);
80     test1(sv1, sv1.size() + 1, string_view_t::npos);
81 }
82 
83 template<typename CharT>
84 TEST_CONSTEXPR_CXX20 bool test_constexpr_copy(const CharT *abcde, const CharT *ghijk, const CharT *bcdjk)
85 {
86     CharT buf[6] = {};
87     std::basic_string_view<CharT> lval(ghijk); lval.copy(buf, 6);
88     std::basic_string_view<CharT>(abcde).copy(buf, 3, 1);
89     assert(std::basic_string_view<CharT>(buf) == bcdjk);
90     return true;
91 }
92 
93 int main(int, char**) {
94     test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
95     test ( "ABCDE");
96     test ( "a" );
97     test ( "" );
98 
99     test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
100     test ( L"ABCDE" );
101     test ( L"a" );
102     test ( L"" );
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     test_constexpr_copy(L"ABCDE", L"GHIJK", L"BCDJK");
118 #if TEST_STD_VER >= 11
119     test_constexpr_copy(u"ABCDE", u"GHIJK", u"BCDJK");
120     test_constexpr_copy(U"ABCDE", U"GHIJK", U"BCDJK");
121 #endif
122 #if TEST_STD_VER >= 17
123     test_constexpr_copy(u8"ABCDE", u8"GHIJK", u8"BCDJK");
124 #endif
125 #if TEST_STD_VER >= 20
126     static_assert(test_constexpr_copy("ABCDE", "GHIJK", "BCDJK"));
127     static_assert(test_constexpr_copy(L"ABCDE", L"GHIJK", L"BCDJK"));
128     static_assert(test_constexpr_copy(u"ABCDE", u"GHIJK", u"BCDJK"));
129     static_assert(test_constexpr_copy(U"ABCDE", U"GHIJK", U"BCDJK"));
130     static_assert(test_constexpr_copy(u8"ABCDE", u8"GHIJK", u8"BCDJK"));
131 #endif
132 
133   return 0;
134 }
135