1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <string> 11 12 // template<class InputIterator> 13 // iterator insert(const_iterator p, InputIterator first, InputIterator last); 14 15 #if _LIBCPP_DEBUG >= 1 16 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) 17 #endif 18 19 #include <string> 20 #include <cassert> 21 22 #include "../../input_iterator.h" 23 #include "min_allocator.h" 24 25 template <class S, class It> 26 void 27 test(S s, typename S::difference_type pos, It first, It last, S expected) 28 { 29 typename S::const_iterator p = s.cbegin() + pos; 30 typename S::iterator i = s.insert(p, first, last); 31 assert(s.__invariants()); 32 assert(i - s.begin() == pos); 33 assert(s == expected); 34 } 35 36 int main() 37 { 38 { 39 typedef std::string S; 40 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 41 test(S(), 0, s, s, S()); 42 test(S(), 0, s, s+1, S("A")); 43 test(S(), 0, s, s+10, S("ABCDEFGHIJ")); 44 test(S(), 0, s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 45 46 test(S("12345"), 0, s, s, S("12345")); 47 test(S("12345"), 1, s, s+1, S("1A2345")); 48 test(S("12345"), 4, s, s+10, S("1234ABCDEFGHIJ5")); 49 test(S("12345"), 5, s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 50 51 test(S("1234567890"), 0, s, s, S("1234567890")); 52 test(S("1234567890"), 1, s, s+1, S("1A234567890")); 53 test(S("1234567890"), 10, s, s+10, S("1234567890ABCDEFGHIJ")); 54 test(S("1234567890"), 8, s, s+52, S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90")); 55 56 test(S("12345678901234567890"), 3, s, s, S("12345678901234567890")); 57 test(S("12345678901234567890"), 3, s, s+1, S("123A45678901234567890")); 58 test(S("12345678901234567890"), 15, s, s+10, S("123456789012345ABCDEFGHIJ67890")); 59 test(S("12345678901234567890"), 20, s, s+52, 60 S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 61 62 test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S()); 63 test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("A")); 64 test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("ABCDEFGHIJ")); 65 test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 66 67 test(S("12345"), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S("12345")); 68 test(S("12345"), 1, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("1A2345")); 69 test(S("12345"), 4, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("1234ABCDEFGHIJ5")); 70 test(S("12345"), 5, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 71 72 test(S("1234567890"), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S("1234567890")); 73 test(S("1234567890"), 1, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("1A234567890")); 74 test(S("1234567890"), 10, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("1234567890ABCDEFGHIJ")); 75 test(S("1234567890"), 8, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90")); 76 77 test(S("12345678901234567890"), 3, input_iterator<const char*>(s), input_iterator<const char*>(s), S("12345678901234567890")); 78 test(S("12345678901234567890"), 3, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("123A45678901234567890")); 79 test(S("12345678901234567890"), 15, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("123456789012345ABCDEFGHIJ67890")); 80 test(S("12345678901234567890"), 20, input_iterator<const char*>(s), input_iterator<const char*>(s+52), 81 S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 82 } 83 #if __cplusplus >= 201103L 84 { 85 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 86 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 87 test(S(), 0, s, s, S()); 88 test(S(), 0, s, s+1, S("A")); 89 test(S(), 0, s, s+10, S("ABCDEFGHIJ")); 90 test(S(), 0, s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 91 92 test(S("12345"), 0, s, s, S("12345")); 93 test(S("12345"), 1, s, s+1, S("1A2345")); 94 test(S("12345"), 4, s, s+10, S("1234ABCDEFGHIJ5")); 95 test(S("12345"), 5, s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 96 97 test(S("1234567890"), 0, s, s, S("1234567890")); 98 test(S("1234567890"), 1, s, s+1, S("1A234567890")); 99 test(S("1234567890"), 10, s, s+10, S("1234567890ABCDEFGHIJ")); 100 test(S("1234567890"), 8, s, s+52, S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90")); 101 102 test(S("12345678901234567890"), 3, s, s, S("12345678901234567890")); 103 test(S("12345678901234567890"), 3, s, s+1, S("123A45678901234567890")); 104 test(S("12345678901234567890"), 15, s, s+10, S("123456789012345ABCDEFGHIJ67890")); 105 test(S("12345678901234567890"), 20, s, s+52, 106 S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 107 108 test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S()); 109 test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("A")); 110 test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("ABCDEFGHIJ")); 111 test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 112 113 test(S("12345"), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S("12345")); 114 test(S("12345"), 1, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("1A2345")); 115 test(S("12345"), 4, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("1234ABCDEFGHIJ5")); 116 test(S("12345"), 5, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 117 118 test(S("1234567890"), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S("1234567890")); 119 test(S("1234567890"), 1, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("1A234567890")); 120 test(S("1234567890"), 10, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("1234567890ABCDEFGHIJ")); 121 test(S("1234567890"), 8, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90")); 122 123 test(S("12345678901234567890"), 3, input_iterator<const char*>(s), input_iterator<const char*>(s), S("12345678901234567890")); 124 test(S("12345678901234567890"), 3, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("123A45678901234567890")); 125 test(S("12345678901234567890"), 15, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("123456789012345ABCDEFGHIJ67890")); 126 test(S("12345678901234567890"), 20, input_iterator<const char*>(s), input_iterator<const char*>(s+52), 127 S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 128 } 129 #endif 130 #if _LIBCPP_DEBUG >= 1 131 { 132 std::string v; 133 std::string v2; 134 char a[] = "123"; 135 const int N = sizeof(a)/sizeof(a[0]); 136 std::string::iterator i = v.insert(v2.cbegin() + 10, a, a+N); 137 assert(false); 138 } 139 #endif 140 } 141