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