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