Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame^] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <string> |
| 11 | |
Howard Hinnant | 6e0a1f4 | 2010-08-22 00:47:54 +0000 | [diff] [blame] | 12 | // basic_string(const basic_string<charT,traits,Allocator>& str, |
| 13 | // size_type pos, size_type n = npos, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | // const Allocator& a = Allocator()); |
| 15 | |
| 16 | #include <string> |
| 17 | #include <stdexcept> |
| 18 | #include <algorithm> |
| 19 | #include <cassert> |
| 20 | |
| 21 | #include "../test_allocator.h" |
| 22 | |
| 23 | template <class S> |
| 24 | void |
| 25 | test(S str, unsigned pos) |
| 26 | { |
| 27 | typedef typename S::traits_type T; |
| 28 | typedef typename S::allocator_type A; |
| 29 | try |
| 30 | { |
| 31 | S s2(str, pos); |
| 32 | assert(s2.__invariants()); |
| 33 | assert(pos <= str.size()); |
| 34 | unsigned rlen = str.size() - pos; |
| 35 | assert(s2.size() == rlen); |
| 36 | assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); |
| 37 | assert(s2.get_allocator() == A()); |
| 38 | assert(s2.capacity() >= s2.size()); |
| 39 | } |
| 40 | catch (std::out_of_range&) |
| 41 | { |
| 42 | assert(pos > str.size()); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | template <class S> |
| 47 | void |
| 48 | test(S str, unsigned pos, unsigned n) |
| 49 | { |
| 50 | typedef typename S::traits_type T; |
| 51 | typedef typename S::allocator_type A; |
| 52 | try |
| 53 | { |
| 54 | S s2(str, pos, n); |
| 55 | assert(s2.__invariants()); |
| 56 | assert(pos <= str.size()); |
| 57 | unsigned rlen = std::min(str.size() - pos, n); |
| 58 | assert(s2.size() == rlen); |
| 59 | assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); |
| 60 | assert(s2.get_allocator() == A()); |
| 61 | assert(s2.capacity() >= s2.size()); |
| 62 | } |
| 63 | catch (std::out_of_range&) |
| 64 | { |
| 65 | assert(pos > str.size()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | template <class S> |
| 70 | void |
| 71 | test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a) |
| 72 | { |
| 73 | typedef typename S::traits_type T; |
| 74 | typedef typename S::allocator_type A; |
| 75 | try |
| 76 | { |
| 77 | S s2(str, pos, n, a); |
| 78 | assert(s2.__invariants()); |
| 79 | assert(pos <= str.size()); |
| 80 | unsigned rlen = std::min(str.size() - pos, n); |
| 81 | assert(s2.size() == rlen); |
| 82 | assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); |
| 83 | assert(s2.get_allocator() == a); |
| 84 | assert(s2.capacity() >= s2.size()); |
| 85 | } |
| 86 | catch (std::out_of_range&) |
| 87 | { |
| 88 | assert(pos > str.size()); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | int main() |
| 93 | { |
| 94 | typedef test_allocator<char> A; |
| 95 | typedef std::basic_string<char, std::char_traits<char>, A> S; |
| 96 | |
| 97 | test(S(A(3)), 0); |
| 98 | test(S(A(3)), 1); |
| 99 | test(S("1", A(5)), 0); |
| 100 | test(S("1", A(5)), 1); |
| 101 | test(S("1", A(5)), 2); |
| 102 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0); |
| 103 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5); |
| 104 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50); |
| 105 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500); |
| 106 | |
| 107 | test(S(A(3)), 0, 0); |
| 108 | test(S(A(3)), 0, 1); |
| 109 | test(S(A(3)), 1, 0); |
| 110 | test(S(A(3)), 1, 1); |
| 111 | test(S(A(3)), 1, 2); |
| 112 | test(S("1", A(5)), 0, 0); |
| 113 | test(S("1", A(5)), 0, 1); |
| 114 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0); |
| 115 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1); |
| 116 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10); |
| 117 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100); |
| 118 | |
| 119 | test(S(A(3)), 0, 0, A(4)); |
| 120 | test(S(A(3)), 0, 1, A(4)); |
| 121 | test(S(A(3)), 1, 0, A(4)); |
| 122 | test(S(A(3)), 1, 1, A(4)); |
| 123 | test(S(A(3)), 1, 2, A(4)); |
| 124 | test(S("1", A(5)), 0, 0, A(6)); |
| 125 | test(S("1", A(5)), 0, 1, A(6)); |
| 126 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8)); |
| 127 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8)); |
| 128 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8)); |
| 129 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8)); |
| 130 | } |