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> 11 12 // size_type copy(charT* s, size_type n, size_type pos = 0) const; 13 14 #include <string> 15 #include <stdexcept> 16 #include <algorithm> 17 #include <cassert> 18 19 #include "min_allocator.h" 20 21 template <class S> 22 void 23 test(S str, typename S::value_type* s, typename S::size_type n, 24 typename S::size_type pos) 25 { 26 try 27 { 28 const S& cs = str; 29 typename S::size_type r = cs.copy(s, n, pos); 30 assert(pos <= cs.size()); 31 typename S::size_type rlen = std::min(n, cs.size() - pos); 32 assert(r == rlen); 33 for (r = 0; r < rlen; ++r) 34 assert(S::traits_type::eq(cs[pos+r], s[r])); 35 } 36 catch (std::out_of_range&) 37 { 38 assert(pos > str.size()); 39 } 40 } 41 42 int main() 43 { 44 { 45 typedef std::string S; 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 } 106 #if __cplusplus >= 201103L 107 { 108 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 109 char s[50]; 110 test(S(""), s, 0, 0); 111 test(S(""), s, 0, 1); 112 test(S(""), s, 1, 0); 113 test(S("abcde"), s, 0, 0); 114 test(S("abcde"), s, 0, 1); 115 test(S("abcde"), s, 0, 2); 116 test(S("abcde"), s, 0, 4); 117 test(S("abcde"), s, 0, 5); 118 test(S("abcde"), s, 0, 6); 119 test(S("abcde"), s, 1, 0); 120 test(S("abcde"), s, 1, 1); 121 test(S("abcde"), s, 1, 2); 122 test(S("abcde"), s, 1, 4); 123 test(S("abcde"), s, 1, 5); 124 test(S("abcde"), s, 2, 0); 125 test(S("abcde"), s, 2, 1); 126 test(S("abcde"), s, 2, 2); 127 test(S("abcde"), s, 2, 4); 128 test(S("abcde"), s, 4, 0); 129 test(S("abcde"), s, 4, 1); 130 test(S("abcde"), s, 4, 2); 131 test(S("abcde"), s, 5, 0); 132 test(S("abcde"), s, 5, 1); 133 test(S("abcde"), s, 6, 0); 134 test(S("abcdefghijklmnopqrst"), s, 0, 0); 135 test(S("abcdefghijklmnopqrst"), s, 0, 1); 136 test(S("abcdefghijklmnopqrst"), s, 0, 2); 137 test(S("abcdefghijklmnopqrst"), s, 0, 10); 138 test(S("abcdefghijklmnopqrst"), s, 0, 19); 139 test(S("abcdefghijklmnopqrst"), s, 0, 20); 140 test(S("abcdefghijklmnopqrst"), s, 0, 21); 141 test(S("abcdefghijklmnopqrst"), s, 1, 0); 142 test(S("abcdefghijklmnopqrst"), s, 1, 1); 143 test(S("abcdefghijklmnopqrst"), s, 1, 2); 144 test(S("abcdefghijklmnopqrst"), s, 1, 9); 145 test(S("abcdefghijklmnopqrst"), s, 1, 18); 146 test(S("abcdefghijklmnopqrst"), s, 1, 19); 147 test(S("abcdefghijklmnopqrst"), s, 1, 20); 148 test(S("abcdefghijklmnopqrst"), s, 2, 0); 149 test(S("abcdefghijklmnopqrst"), s, 2, 1); 150 test(S("abcdefghijklmnopqrst"), s, 2, 2); 151 test(S("abcdefghijklmnopqrst"), s, 2, 9); 152 test(S("abcdefghijklmnopqrst"), s, 2, 17); 153 test(S("abcdefghijklmnopqrst"), s, 2, 18); 154 test(S("abcdefghijklmnopqrst"), s, 2, 19); 155 test(S("abcdefghijklmnopqrst"), s, 10, 0); 156 test(S("abcdefghijklmnopqrst"), s, 10, 1); 157 test(S("abcdefghijklmnopqrst"), s, 10, 2); 158 test(S("abcdefghijklmnopqrst"), s, 10, 5); 159 test(S("abcdefghijklmnopqrst"), s, 10, 9); 160 test(S("abcdefghijklmnopqrst"), s, 10, 10); 161 test(S("abcdefghijklmnopqrst"), s, 10, 11); 162 test(S("abcdefghijklmnopqrst"), s, 19, 0); 163 test(S("abcdefghijklmnopqrst"), s, 19, 1); 164 test(S("abcdefghijklmnopqrst"), s, 19, 2); 165 test(S("abcdefghijklmnopqrst"), s, 20, 0); 166 test(S("abcdefghijklmnopqrst"), s, 20, 1); 167 test(S("abcdefghijklmnopqrst"), s, 21, 0); 168 } 169 #endif 170 } 171