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> 10 11 // size_type copy(charT* s, size_type n, size_type pos = 0) const; // constexpr since C++20 12 13 #include <string> 14 #include <stdexcept> 15 #include <algorithm> 16 #include <cassert> 17 18 #include "test_macros.h" 19 #include "min_allocator.h" 20 21 template <class S> 22 TEST_CONSTEXPR_CXX20 void 23 test(S str, typename S::value_type* s, typename S::size_type n, 24 typename S::size_type pos) 25 { 26 const S& cs = str; 27 if (pos <= cs.size()) 28 { 29 typename S::size_type r = cs.copy(s, n, pos); 30 typename S::size_type rlen = std::min(n, cs.size() - pos); 31 assert(r == rlen); 32 for (r = 0; r < rlen; ++r) 33 assert(S::traits_type::eq(cs[pos+r], s[r])); 34 } 35 #ifndef TEST_HAS_NO_EXCEPTIONS 36 else if (!TEST_IS_CONSTANT_EVALUATED) 37 { 38 try 39 { 40 typename S::size_type r = cs.copy(s, n, pos); 41 ((void)r); // Prevent unused warning 42 assert(false); 43 } 44 catch (std::out_of_range&) 45 { 46 assert(pos > str.size()); 47 } 48 } 49 #endif 50 } 51 52 template <class S> 53 TEST_CONSTEXPR_CXX20 void test_string() { 54 char s[50]; 55 test(S(""), s, 0, 0); 56 test(S(""), s, 0, 1); 57 test(S(""), s, 1, 0); 58 test(S("abcde"), s, 0, 0); 59 test(S("abcde"), s, 0, 1); 60 test(S("abcde"), s, 0, 2); 61 test(S("abcde"), s, 0, 4); 62 test(S("abcde"), s, 0, 5); 63 test(S("abcde"), s, 0, 6); 64 test(S("abcde"), s, 1, 0); 65 test(S("abcde"), s, 1, 1); 66 test(S("abcde"), s, 1, 2); 67 test(S("abcde"), s, 1, 4); 68 test(S("abcde"), s, 1, 5); 69 test(S("abcde"), s, 2, 0); 70 test(S("abcde"), s, 2, 1); 71 test(S("abcde"), s, 2, 2); 72 test(S("abcde"), s, 2, 4); 73 test(S("abcde"), s, 4, 0); 74 test(S("abcde"), s, 4, 1); 75 test(S("abcde"), s, 4, 2); 76 test(S("abcde"), s, 5, 0); 77 test(S("abcde"), s, 5, 1); 78 test(S("abcde"), s, 6, 0); 79 test(S("abcdefghijklmnopqrst"), s, 0, 0); 80 test(S("abcdefghijklmnopqrst"), s, 0, 1); 81 test(S("abcdefghijklmnopqrst"), s, 0, 2); 82 test(S("abcdefghijklmnopqrst"), s, 0, 10); 83 test(S("abcdefghijklmnopqrst"), s, 0, 19); 84 test(S("abcdefghijklmnopqrst"), s, 0, 20); 85 test(S("abcdefghijklmnopqrst"), s, 0, 21); 86 test(S("abcdefghijklmnopqrst"), s, 1, 0); 87 test(S("abcdefghijklmnopqrst"), s, 1, 1); 88 test(S("abcdefghijklmnopqrst"), s, 1, 2); 89 test(S("abcdefghijklmnopqrst"), s, 1, 9); 90 test(S("abcdefghijklmnopqrst"), s, 1, 18); 91 test(S("abcdefghijklmnopqrst"), s, 1, 19); 92 test(S("abcdefghijklmnopqrst"), s, 1, 20); 93 test(S("abcdefghijklmnopqrst"), s, 2, 0); 94 test(S("abcdefghijklmnopqrst"), s, 2, 1); 95 test(S("abcdefghijklmnopqrst"), s, 2, 2); 96 test(S("abcdefghijklmnopqrst"), s, 2, 9); 97 test(S("abcdefghijklmnopqrst"), s, 2, 17); 98 test(S("abcdefghijklmnopqrst"), s, 2, 18); 99 test(S("abcdefghijklmnopqrst"), s, 2, 19); 100 test(S("abcdefghijklmnopqrst"), s, 10, 0); 101 test(S("abcdefghijklmnopqrst"), s, 10, 1); 102 test(S("abcdefghijklmnopqrst"), s, 10, 2); 103 test(S("abcdefghijklmnopqrst"), s, 10, 5); 104 test(S("abcdefghijklmnopqrst"), s, 10, 9); 105 test(S("abcdefghijklmnopqrst"), s, 10, 10); 106 test(S("abcdefghijklmnopqrst"), s, 10, 11); 107 test(S("abcdefghijklmnopqrst"), s, 19, 0); 108 test(S("abcdefghijklmnopqrst"), s, 19, 1); 109 test(S("abcdefghijklmnopqrst"), s, 19, 2); 110 test(S("abcdefghijklmnopqrst"), s, 20, 0); 111 test(S("abcdefghijklmnopqrst"), s, 20, 1); 112 test(S("abcdefghijklmnopqrst"), s, 21, 0); 113 } 114 115 TEST_CONSTEXPR_CXX20 bool test() { 116 test_string<std::string>(); 117 #if TEST_STD_VER >= 11 118 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); 119 #endif 120 121 return true; 122 } 123 124 int main(int, char**) 125 { 126 test(); 127 #if TEST_STD_VER > 17 128 static_assert(test()); 129 #endif 130 131 return 0; 132 } 133