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 | |
| 12 | // iterator insert(const_iterator p, charT c); |
| 13 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 14 | #if _LIBCPP_DEBUG >= 1 |
| 15 | #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) |
| 16 | #endif |
| 17 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <stdexcept> |
| 20 | #include <cassert> |
| 21 | |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 22 | #include "min_allocator.h" |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 23 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 24 | template <class S> |
| 25 | void |
| 26 | test(S& s, typename S::const_iterator p, typename S::value_type c, S expected) |
| 27 | { |
| 28 | bool sufficient_cap = s.size() < s.capacity(); |
| 29 | typename S::difference_type pos = p - s.begin(); |
| 30 | typename S::iterator i = s.insert(p, c); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 31 | assert(s.__invariants()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 32 | assert(s == expected); |
| 33 | assert(i - s.begin() == pos); |
| 34 | assert(*i == c); |
| 35 | if (sufficient_cap) |
| 36 | assert(i == p); |
| 37 | } |
| 38 | |
| 39 | int main() |
| 40 | { |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 41 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | typedef std::string S; |
| 43 | S s; |
| 44 | test(s, s.begin(), '1', S("1")); |
| 45 | test(s, s.begin(), 'a', S("a1")); |
| 46 | test(s, s.end(), 'b', S("a1b")); |
| 47 | test(s, s.end()-1, 'c', S("a1cb")); |
| 48 | test(s, s.end()-2, 'd', S("a1dcb")); |
| 49 | test(s, s.end()-3, '2', S("a12dcb")); |
| 50 | test(s, s.end()-4, '3', S("a132dcb")); |
| 51 | test(s, s.end()-5, '4', S("a1432dcb")); |
| 52 | test(s, s.begin()+1, '5', S("a51432dcb")); |
| 53 | test(s, s.begin()+2, '6', S("a561432dcb")); |
| 54 | test(s, s.begin()+3, '7', S("a5671432dcb")); |
| 55 | test(s, s.begin()+4, 'A', S("a567A1432dcb")); |
| 56 | test(s, s.begin()+5, 'B', S("a567AB1432dcb")); |
| 57 | test(s, s.begin()+6, 'C', S("a567ABC1432dcb")); |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 58 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 59 | #if __cplusplus >= 201103L |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 60 | { |
| 61 | typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; |
| 62 | S s; |
| 63 | test(s, s.begin(), '1', S("1")); |
| 64 | test(s, s.begin(), 'a', S("a1")); |
| 65 | test(s, s.end(), 'b', S("a1b")); |
| 66 | test(s, s.end()-1, 'c', S("a1cb")); |
| 67 | test(s, s.end()-2, 'd', S("a1dcb")); |
| 68 | test(s, s.end()-3, '2', S("a12dcb")); |
| 69 | test(s, s.end()-4, '3', S("a132dcb")); |
| 70 | test(s, s.end()-5, '4', S("a1432dcb")); |
| 71 | test(s, s.begin()+1, '5', S("a51432dcb")); |
| 72 | test(s, s.begin()+2, '6', S("a561432dcb")); |
| 73 | test(s, s.begin()+3, '7', S("a5671432dcb")); |
| 74 | test(s, s.begin()+4, 'A', S("a567A1432dcb")); |
| 75 | test(s, s.begin()+5, 'B', S("a567AB1432dcb")); |
| 76 | test(s, s.begin()+6, 'C', S("a567ABC1432dcb")); |
| 77 | } |
| 78 | #endif |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 79 | #if _LIBCPP_DEBUG >= 1 |
| 80 | { |
| 81 | typedef std::string S; |
| 82 | S s; |
| 83 | S s2; |
| 84 | s.insert(s2.begin(), '1'); |
| 85 | assert(false); |
| 86 | } |
| 87 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 88 | } |