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, |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 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 | |
Marshall Clow | 1b92188 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 21 | #include "test_allocator.h" |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 22 | #include "min_allocator.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | |
| 24 | template <class S> |
| 25 | void |
| 26 | test(S str, unsigned pos) |
| 27 | { |
| 28 | typedef typename S::traits_type T; |
| 29 | typedef typename S::allocator_type A; |
| 30 | try |
| 31 | { |
| 32 | S s2(str, pos); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 33 | assert(s2.__invariants()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 34 | assert(pos <= str.size()); |
| 35 | unsigned rlen = str.size() - pos; |
| 36 | assert(s2.size() == rlen); |
| 37 | assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); |
| 38 | assert(s2.get_allocator() == A()); |
| 39 | assert(s2.capacity() >= s2.size()); |
| 40 | } |
| 41 | catch (std::out_of_range&) |
| 42 | { |
| 43 | assert(pos > str.size()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | template <class S> |
| 48 | void |
| 49 | test(S str, unsigned pos, unsigned n) |
| 50 | { |
| 51 | typedef typename S::traits_type T; |
| 52 | typedef typename S::allocator_type A; |
| 53 | try |
| 54 | { |
| 55 | S s2(str, pos, n); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 56 | assert(s2.__invariants()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 57 | assert(pos <= str.size()); |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 58 | unsigned rlen = std::min<unsigned>(str.size() - pos, n); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 59 | assert(s2.size() == rlen); |
| 60 | assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); |
| 61 | assert(s2.get_allocator() == A()); |
| 62 | assert(s2.capacity() >= s2.size()); |
| 63 | } |
| 64 | catch (std::out_of_range&) |
| 65 | { |
| 66 | assert(pos > str.size()); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | template <class S> |
| 71 | void |
| 72 | test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a) |
| 73 | { |
| 74 | typedef typename S::traits_type T; |
| 75 | typedef typename S::allocator_type A; |
| 76 | try |
| 77 | { |
| 78 | S s2(str, pos, n, a); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 79 | assert(s2.__invariants()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 80 | assert(pos <= str.size()); |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 81 | unsigned rlen = std::min<unsigned>(str.size() - pos, n); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 82 | assert(s2.size() == rlen); |
| 83 | assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); |
| 84 | assert(s2.get_allocator() == a); |
| 85 | assert(s2.capacity() >= s2.size()); |
| 86 | } |
| 87 | catch (std::out_of_range&) |
| 88 | { |
| 89 | assert(pos > str.size()); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | int main() |
| 94 | { |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 95 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 96 | typedef test_allocator<char> A; |
| 97 | typedef std::basic_string<char, std::char_traits<char>, A> S; |
| 98 | |
| 99 | test(S(A(3)), 0); |
| 100 | test(S(A(3)), 1); |
| 101 | test(S("1", A(5)), 0); |
| 102 | test(S("1", A(5)), 1); |
| 103 | test(S("1", A(5)), 2); |
| 104 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0); |
| 105 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5); |
| 106 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50); |
| 107 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500); |
| 108 | |
| 109 | test(S(A(3)), 0, 0); |
| 110 | test(S(A(3)), 0, 1); |
| 111 | test(S(A(3)), 1, 0); |
| 112 | test(S(A(3)), 1, 1); |
| 113 | test(S(A(3)), 1, 2); |
| 114 | test(S("1", A(5)), 0, 0); |
| 115 | test(S("1", A(5)), 0, 1); |
| 116 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0); |
| 117 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1); |
| 118 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10); |
| 119 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100); |
| 120 | |
| 121 | test(S(A(3)), 0, 0, A(4)); |
| 122 | test(S(A(3)), 0, 1, A(4)); |
| 123 | test(S(A(3)), 1, 0, A(4)); |
| 124 | test(S(A(3)), 1, 1, A(4)); |
| 125 | test(S(A(3)), 1, 2, A(4)); |
| 126 | test(S("1", A(5)), 0, 0, A(6)); |
| 127 | test(S("1", A(5)), 0, 1, A(6)); |
| 128 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8)); |
| 129 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8)); |
| 130 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8)); |
| 131 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8)); |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 132 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 133 | #if __cplusplus >= 201103L |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 134 | { |
| 135 | typedef min_allocator<char> A; |
| 136 | typedef std::basic_string<char, std::char_traits<char>, A> S; |
| 137 | |
| 138 | test(S(A()), 0); |
| 139 | test(S(A()), 1); |
| 140 | test(S("1", A()), 0); |
| 141 | test(S("1", A()), 1); |
| 142 | test(S("1", A()), 2); |
| 143 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0); |
| 144 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5); |
| 145 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50); |
| 146 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500); |
| 147 | |
| 148 | test(S(A()), 0, 0); |
| 149 | test(S(A()), 0, 1); |
| 150 | test(S(A()), 1, 0); |
| 151 | test(S(A()), 1, 1); |
| 152 | test(S(A()), 1, 2); |
| 153 | test(S("1", A()), 0, 0); |
| 154 | test(S("1", A()), 0, 1); |
| 155 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0); |
| 156 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1); |
| 157 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10); |
| 158 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100); |
| 159 | |
| 160 | test(S(A()), 0, 0, A()); |
| 161 | test(S(A()), 0, 1, A()); |
| 162 | test(S(A()), 1, 0, A()); |
| 163 | test(S(A()), 1, 1, A()); |
| 164 | test(S(A()), 1, 2, A()); |
| 165 | test(S("1", A()), 0, 0, A()); |
| 166 | test(S("1", A()), 0, 1, A()); |
| 167 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A()); |
| 168 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A()); |
| 169 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A()); |
| 170 | test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A()); |
| 171 | } |
| 172 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 173 | } |