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<charT,traits,Allocator>& |
Marshall Clow | a93b5e2 | 2014-03-04 19:17:19 +0000 | [diff] [blame] | 13 | // append(const basic_string<charT,traits>& str, size_type pos, size_type n = npos); |
| 14 | // the "= npos" was added for C++14 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 15 | |
| 16 | #include <string> |
| 17 | #include <stdexcept> |
| 18 | #include <cassert> |
| 19 | |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 20 | #include "min_allocator.h" |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 21 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | template <class S> |
| 23 | void |
| 24 | test(S s, S str, typename S::size_type pos, typename S::size_type n, S expected) |
| 25 | { |
| 26 | try |
| 27 | { |
| 28 | s.append(str, pos, n); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 29 | assert(s.__invariants()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 30 | assert(pos <= str.size()); |
| 31 | assert(s == expected); |
| 32 | } |
| 33 | catch (std::out_of_range&) |
| 34 | { |
| 35 | assert(pos > str.size()); |
| 36 | } |
| 37 | } |
| 38 | |
Marshall Clow | a93b5e2 | 2014-03-04 19:17:19 +0000 | [diff] [blame] | 39 | template <class S> |
| 40 | void |
| 41 | test_npos(S s, S str, typename S::size_type pos, S expected) |
| 42 | { |
| 43 | try |
| 44 | { |
| 45 | s.append(str, pos); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 46 | assert(s.__invariants()); |
Marshall Clow | a93b5e2 | 2014-03-04 19:17:19 +0000 | [diff] [blame] | 47 | assert(pos <= str.size()); |
| 48 | assert(s == expected); |
| 49 | } |
| 50 | catch (std::out_of_range&) |
| 51 | { |
| 52 | assert(pos > str.size()); |
| 53 | } |
| 54 | } |
| 55 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 56 | int main() |
| 57 | { |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 58 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 59 | typedef std::string S; |
| 60 | test(S(), S(), 0, 0, S()); |
| 61 | test(S(), S(), 1, 0, S()); |
| 62 | test(S(), S("12345"), 0, 3, S("123")); |
| 63 | test(S(), S("12345"), 1, 4, S("2345")); |
| 64 | test(S(), S("12345"), 3, 15, S("45")); |
| 65 | test(S(), S("12345"), 5, 15, S("")); |
| 66 | test(S(), S("12345"), 6, 15, S("not happening")); |
| 67 | test(S(), S("12345678901234567890"), 0, 0, S()); |
| 68 | test(S(), S("12345678901234567890"), 1, 1, S("2")); |
| 69 | test(S(), S("12345678901234567890"), 2, 3, S("345")); |
| 70 | test(S(), S("12345678901234567890"), 12, 13, S("34567890")); |
| 71 | test(S(), S("12345678901234567890"), 21, 13, S("not happening")); |
| 72 | |
| 73 | test(S("12345"), S(), 0, 0, S("12345")); |
| 74 | test(S("12345"), S("12345"), 2, 2, S("1234534")); |
| 75 | test(S("12345"), S("1234567890"), 0, 100, S("123451234567890")); |
| 76 | |
| 77 | test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890")); |
| 78 | test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234")); |
| 79 | test(S("12345678901234567890"), S("12345678901234567890"), 5, 10, |
| 80 | S("123456789012345678906789012345")); |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 81 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 82 | #if __cplusplus >= 201103L |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 83 | { |
| 84 | typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; |
| 85 | test(S(), S(), 0, 0, S()); |
| 86 | test(S(), S(), 1, 0, S()); |
| 87 | test(S(), S("12345"), 0, 3, S("123")); |
| 88 | test(S(), S("12345"), 1, 4, S("2345")); |
| 89 | test(S(), S("12345"), 3, 15, S("45")); |
| 90 | test(S(), S("12345"), 5, 15, S("")); |
| 91 | test(S(), S("12345"), 6, 15, S("not happening")); |
| 92 | test(S(), S("12345678901234567890"), 0, 0, S()); |
| 93 | test(S(), S("12345678901234567890"), 1, 1, S("2")); |
| 94 | test(S(), S("12345678901234567890"), 2, 3, S("345")); |
| 95 | test(S(), S("12345678901234567890"), 12, 13, S("34567890")); |
| 96 | test(S(), S("12345678901234567890"), 21, 13, S("not happening")); |
| 97 | |
| 98 | test(S("12345"), S(), 0, 0, S("12345")); |
| 99 | test(S("12345"), S("12345"), 2, 2, S("1234534")); |
| 100 | test(S("12345"), S("1234567890"), 0, 100, S("123451234567890")); |
| 101 | |
| 102 | test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890")); |
| 103 | test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234")); |
| 104 | test(S("12345678901234567890"), S("12345678901234567890"), 5, 10, |
| 105 | S("123456789012345678906789012345")); |
| 106 | } |
| 107 | #endif |
Marshall Clow | a93b5e2 | 2014-03-04 19:17:19 +0000 | [diff] [blame] | 108 | { |
| 109 | typedef std::string S; |
| 110 | test_npos(S(), S(), 0, S()); |
| 111 | test_npos(S(), S(), 1, S()); |
| 112 | test_npos(S(), S("12345"), 0, S("12345")); |
| 113 | test_npos(S(), S("12345"), 1, S("2345")); |
| 114 | test_npos(S(), S("12345"), 3, S("45")); |
| 115 | test_npos(S(), S("12345"), 5, S("")); |
| 116 | test_npos(S(), S("12345"), 6, S("not happening")); |
| 117 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 118 | } |