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 <experimental/string_view>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 
26 template<typename CharT>
test1(std::experimental::basic_string_view<CharT> sv,size_t n,size_t pos)27 void test1 ( std::experimental::basic_string_view<CharT> sv, size_t n, size_t pos ) {
28     const size_t rlen = std::min ( n, sv.size() - pos );
29 
30     CharT *dest1 = new CharT [rlen + 1];    dest1[rlen] = 0;
31     CharT *dest2 = new CharT [rlen + 1];    dest2[rlen] = 0;
32 
33     if (pos > sv.size()) {
34 #ifndef TEST_HAS_NO_EXCEPTIONS
35         try {
36             sv.copy(dest1, n, pos);
37             assert(false);
38         } catch (const std::out_of_range&) {
39         } catch (...) {
40             assert(false);
41         }
42 #endif
43     } else {
44         sv.copy(dest1, n, pos);
45         std::copy_n(sv.begin() + pos, rlen, dest2);
46         for ( size_t i = 0; i <= rlen; ++i )
47             assert ( dest1[i] == dest2[i] );
48     }
49     delete [] dest1;
50     delete [] dest2;
51 }
52 
53 
54 template<typename CharT>
test(const CharT * s)55 void test ( const CharT *s ) {
56     typedef std::experimental::basic_string_view<CharT> string_view_t;
57 
58     string_view_t sv1 ( s );
59 
60     test1(sv1,  0, 0);
61     test1(sv1,  1, 0);
62     test1(sv1, 20, 0);
63     test1(sv1, sv1.size(), 0);
64     test1(sv1, 20, string_view_t::npos);
65 
66     test1(sv1,   0, 3);
67     test1(sv1,   2, 3);
68     test1(sv1, 100, 3);
69     test1(sv1, 100, string_view_t::npos);
70 
71     test1(sv1, sv1.size(), string_view_t::npos);
72 
73     test1(sv1, sv1.size() + 1, 0);
74     test1(sv1, sv1.size() + 1, 1);
75     test1(sv1, sv1.size() + 1, string_view_t::npos);
76 
77 }
78 
main()79 int main () {
80     test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
81     test ( "ABCDE");
82     test ( "a" );
83     test ( "" );
84 
85     test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
86     test ( L"ABCDE" );
87     test ( L"a" );
88     test ( L"" );
89 
90 #if TEST_STD_VER >= 11
91     test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
92     test ( u"ABCDE" );
93     test ( u"a" );
94     test ( u"" );
95 
96     test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
97     test ( U"ABCDE" );
98     test ( U"a" );
99     test ( U"" );
100 #endif
101 }
102