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 // <string_view> 10 11 // size_type copy(charT* s, size_type n, size_type pos = 0) const; 12 13 // Throws: out_of_range if pos > size(). 14 // Remarks: Let rlen be the smaller of n and size() - pos. 15 // Requires: [s, s+rlen) is a valid range. 16 // Effects: Equivalent to std::copy_n(begin() + pos, rlen, s). 17 // Returns: rlen. 18 19 20 #include <string_view> 21 #include <algorithm> 22 #include <cassert> 23 #include <stdexcept> 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 template<typename CharT> 80 TEST_CONSTEXPR_CXX20 bool test_constexpr_copy(const CharT *abcde, const CharT *ghijk, const CharT *bcdjk) 81 { 82 CharT buf[6] = {}; 83 std::basic_string_view<CharT> lval(ghijk); lval.copy(buf, 6); 84 std::basic_string_view<CharT>(abcde).copy(buf, 3, 1); 85 assert(std::basic_string_view<CharT>(buf) == bcdjk); 86 return true; 87 } 88 89 int main(int, char**) { 90 test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 91 test ( "ABCDE"); 92 test ( "a" ); 93 test ( "" ); 94 95 test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 96 test ( L"ABCDE" ); 97 test ( L"a" ); 98 test ( L"" ); 99 100 #if TEST_STD_VER >= 11 101 test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 102 test ( u"ABCDE" ); 103 test ( u"a" ); 104 test ( u"" ); 105 106 test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 107 test ( U"ABCDE" ); 108 test ( U"a" ); 109 test ( U"" ); 110 #endif 111 112 test_constexpr_copy("ABCDE", "GHIJK", "BCDJK"); 113 test_constexpr_copy(L"ABCDE", L"GHIJK", L"BCDJK"); 114 #if TEST_STD_VER >= 11 115 test_constexpr_copy(u"ABCDE", u"GHIJK", u"BCDJK"); 116 test_constexpr_copy(U"ABCDE", U"GHIJK", U"BCDJK"); 117 #endif 118 #if TEST_STD_VER >= 17 119 test_constexpr_copy(u8"ABCDE", u8"GHIJK", u8"BCDJK"); 120 #endif 121 #if TEST_STD_VER >= 20 122 static_assert(test_constexpr_copy("ABCDE", "GHIJK", "BCDJK")); 123 static_assert(test_constexpr_copy(L"ABCDE", L"GHIJK", L"BCDJK")); 124 static_assert(test_constexpr_copy(u"ABCDE", u"GHIJK", u"BCDJK")); 125 static_assert(test_constexpr_copy(U"ABCDE", U"GHIJK", U"BCDJK")); 126 static_assert(test_constexpr_copy(u8"ABCDE", u8"GHIJK", u8"BCDJK")); 127 #endif 128 129 return 0; 130 } 131