Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. 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 | template <class S> |
| 20 | void |
| 21 | test(S str, typename S::value_type* s, typename S::size_type n, |
| 22 | typename S::size_type pos) |
| 23 | { |
| 24 | try |
| 25 | { |
| 26 | const S& cs = str; |
| 27 | typename S::size_type r = cs.copy(s, n, pos); |
| 28 | assert(pos <= cs.size()); |
| 29 | typename S::size_type rlen = std::min(n, cs.size() - pos); |
| 30 | assert(r == rlen); |
| 31 | for (r = 0; r < rlen; ++r) |
| 32 | assert(S::traits_type::eq(cs[pos+r], s[r])); |
| 33 | } |
| 34 | catch (std::out_of_range&) |
| 35 | { |
| 36 | assert(pos > str.size()); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | int main() |
| 41 | { |
| 42 | typedef std::string S; |
| 43 | char s[50]; |
| 44 | test(S(""), s, 0, 0); |
| 45 | test(S(""), s, 0, 1); |
| 46 | test(S(""), s, 1, 0); |
| 47 | test(S("abcde"), s, 0, 0); |
| 48 | test(S("abcde"), s, 0, 1); |
| 49 | test(S("abcde"), s, 0, 2); |
| 50 | test(S("abcde"), s, 0, 4); |
| 51 | test(S("abcde"), s, 0, 5); |
| 52 | test(S("abcde"), s, 0, 6); |
| 53 | test(S("abcde"), s, 1, 0); |
| 54 | test(S("abcde"), s, 1, 1); |
| 55 | test(S("abcde"), s, 1, 2); |
| 56 | test(S("abcde"), s, 1, 4); |
| 57 | test(S("abcde"), s, 1, 5); |
| 58 | test(S("abcde"), s, 2, 0); |
| 59 | test(S("abcde"), s, 2, 1); |
| 60 | test(S("abcde"), s, 2, 2); |
| 61 | test(S("abcde"), s, 2, 4); |
| 62 | test(S("abcde"), s, 4, 0); |
| 63 | test(S("abcde"), s, 4, 1); |
| 64 | test(S("abcde"), s, 4, 2); |
| 65 | test(S("abcde"), s, 5, 0); |
| 66 | test(S("abcde"), s, 5, 1); |
| 67 | test(S("abcde"), s, 6, 0); |
| 68 | test(S("abcdefghijklmnopqrst"), s, 0, 0); |
| 69 | test(S("abcdefghijklmnopqrst"), s, 0, 1); |
| 70 | test(S("abcdefghijklmnopqrst"), s, 0, 2); |
| 71 | test(S("abcdefghijklmnopqrst"), s, 0, 10); |
| 72 | test(S("abcdefghijklmnopqrst"), s, 0, 19); |
| 73 | test(S("abcdefghijklmnopqrst"), s, 0, 20); |
| 74 | test(S("abcdefghijklmnopqrst"), s, 0, 21); |
| 75 | test(S("abcdefghijklmnopqrst"), s, 1, 0); |
| 76 | test(S("abcdefghijklmnopqrst"), s, 1, 1); |
| 77 | test(S("abcdefghijklmnopqrst"), s, 1, 2); |
| 78 | test(S("abcdefghijklmnopqrst"), s, 1, 9); |
| 79 | test(S("abcdefghijklmnopqrst"), s, 1, 18); |
| 80 | test(S("abcdefghijklmnopqrst"), s, 1, 19); |
| 81 | test(S("abcdefghijklmnopqrst"), s, 1, 20); |
| 82 | test(S("abcdefghijklmnopqrst"), s, 2, 0); |
| 83 | test(S("abcdefghijklmnopqrst"), s, 2, 1); |
| 84 | test(S("abcdefghijklmnopqrst"), s, 2, 2); |
| 85 | test(S("abcdefghijklmnopqrst"), s, 2, 9); |
| 86 | test(S("abcdefghijklmnopqrst"), s, 2, 17); |
| 87 | test(S("abcdefghijklmnopqrst"), s, 2, 18); |
| 88 | test(S("abcdefghijklmnopqrst"), s, 2, 19); |
| 89 | test(S("abcdefghijklmnopqrst"), s, 10, 0); |
| 90 | test(S("abcdefghijklmnopqrst"), s, 10, 1); |
| 91 | test(S("abcdefghijklmnopqrst"), s, 10, 2); |
| 92 | test(S("abcdefghijklmnopqrst"), s, 10, 5); |
| 93 | test(S("abcdefghijklmnopqrst"), s, 10, 9); |
| 94 | test(S("abcdefghijklmnopqrst"), s, 10, 10); |
| 95 | test(S("abcdefghijklmnopqrst"), s, 10, 11); |
| 96 | test(S("abcdefghijklmnopqrst"), s, 19, 0); |
| 97 | test(S("abcdefghijklmnopqrst"), s, 19, 1); |
| 98 | test(S("abcdefghijklmnopqrst"), s, 19, 2); |
| 99 | test(S("abcdefghijklmnopqrst"), s, 20, 0); |
| 100 | test(S("abcdefghijklmnopqrst"), s, 20, 1); |
| 101 | test(S("abcdefghijklmnopqrst"), s, 21, 0); |
| 102 | } |