1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <string_view>
11
12 // size_type copy(charT* s, size_type n, size_type pos = 0) const;
13
14 // Throws: out_of_range if pos > size().
15 // Remarks: Let rlen be the smaller of n and size() - pos.
16 // Requires: [s, s+rlen) is a valid range.
17 // Effects: Equivalent to std::copy_n(begin() + pos, rlen, s).
18 // Returns: rlen.
19
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
main()81 int main () {
82 test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
83 test ( "ABCDE");
84 test ( "a" );
85 test ( "" );
86
87 test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
88 test ( L"ABCDE" );
89 test ( L"a" );
90 test ( L"" );
91
92 #if TEST_STD_VER >= 11
93 test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
94 test ( u"ABCDE" );
95 test ( u"a" );
96 test ( u"" );
97
98 test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
99 test ( U"ABCDE" );
100 test ( U"a" );
101 test ( U"" );
102 #endif
103 }
104