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