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 // iterator erase(const_iterator first, const_iterator last);
13
14 #include <string>
15 #include <cassert>
16
17 #include "test_macros.h"
18 #include "min_allocator.h"
19
20 template <class S>
21 void
test(S s,typename S::difference_type pos,typename S::difference_type n,S expected)22 test(S s, typename S::difference_type pos, typename S::difference_type n, S expected)
23 {
24 typename S::const_iterator first = s.cbegin() + pos;
25 typename S::const_iterator last = s.cbegin() + pos + n;
26 typename S::iterator i = s.erase(first, last);
27 LIBCPP_ASSERT(s.__invariants());
28 assert(s[s.size()] == typename S::value_type());
29 assert(s == expected);
30 assert(i - s.begin() == pos);
31 }
32
main()33 int main()
34 {
35 {
36 typedef std::string S;
37 test(S(""), 0, 0, S(""));
38 test(S("abcde"), 0, 0, S("abcde"));
39 test(S("abcde"), 0, 1, S("bcde"));
40 test(S("abcde"), 0, 2, S("cde"));
41 test(S("abcde"), 0, 4, S("e"));
42 test(S("abcde"), 0, 5, S(""));
43 test(S("abcde"), 1, 0, S("abcde"));
44 test(S("abcde"), 1, 1, S("acde"));
45 test(S("abcde"), 1, 2, S("ade"));
46 test(S("abcde"), 1, 3, S("ae"));
47 test(S("abcde"), 1, 4, S("a"));
48 test(S("abcde"), 2, 0, S("abcde"));
49 test(S("abcde"), 2, 1, S("abde"));
50 test(S("abcde"), 2, 2, S("abe"));
51 test(S("abcde"), 2, 3, S("ab"));
52 test(S("abcde"), 4, 0, S("abcde"));
53 test(S("abcde"), 4, 1, S("abcd"));
54 test(S("abcde"), 5, 0, S("abcde"));
55 test(S("abcdefghij"), 0, 0, S("abcdefghij"));
56 test(S("abcdefghij"), 0, 1, S("bcdefghij"));
57 test(S("abcdefghij"), 0, 5, S("fghij"));
58 test(S("abcdefghij"), 0, 9, S("j"));
59 test(S("abcdefghij"), 0, 10, S(""));
60 test(S("abcdefghij"), 1, 0, S("abcdefghij"));
61 test(S("abcdefghij"), 1, 1, S("acdefghij"));
62 test(S("abcdefghij"), 1, 4, S("afghij"));
63 test(S("abcdefghij"), 1, 8, S("aj"));
64 test(S("abcdefghij"), 1, 9, S("a"));
65 test(S("abcdefghij"), 5, 0, S("abcdefghij"));
66 test(S("abcdefghij"), 5, 1, S("abcdeghij"));
67 test(S("abcdefghij"), 5, 2, S("abcdehij"));
68 test(S("abcdefghij"), 5, 4, S("abcdej"));
69 test(S("abcdefghij"), 5, 5, S("abcde"));
70 test(S("abcdefghij"), 9, 0, S("abcdefghij"));
71 test(S("abcdefghij"), 9, 1, S("abcdefghi"));
72 test(S("abcdefghij"), 10, 0, S("abcdefghij"));
73 test(S("abcdefghijklmnopqrst"), 0, 0, S("abcdefghijklmnopqrst"));
74 test(S("abcdefghijklmnopqrst"), 0, 1, S("bcdefghijklmnopqrst"));
75 test(S("abcdefghijklmnopqrst"), 0, 10, S("klmnopqrst"));
76 test(S("abcdefghijklmnopqrst"), 0, 19, S("t"));
77 test(S("abcdefghijklmnopqrst"), 0, 20, S(""));
78 test(S("abcdefghijklmnopqrst"), 1, 0, S("abcdefghijklmnopqrst"));
79 test(S("abcdefghijklmnopqrst"), 1, 1, S("acdefghijklmnopqrst"));
80 test(S("abcdefghijklmnopqrst"), 1, 9, S("aklmnopqrst"));
81 test(S("abcdefghijklmnopqrst"), 1, 18, S("at"));
82 test(S("abcdefghijklmnopqrst"), 1, 19, S("a"));
83 test(S("abcdefghijklmnopqrst"), 10, 0, S("abcdefghijklmnopqrst"));
84 test(S("abcdefghijklmnopqrst"), 10, 1, S("abcdefghijlmnopqrst"));
85 test(S("abcdefghijklmnopqrst"), 10, 5, S("abcdefghijpqrst"));
86 test(S("abcdefghijklmnopqrst"), 10, 9, S("abcdefghijt"));
87 test(S("abcdefghijklmnopqrst"), 10, 10, S("abcdefghij"));
88 test(S("abcdefghijklmnopqrst"), 19, 0, S("abcdefghijklmnopqrst"));
89 test(S("abcdefghijklmnopqrst"), 19, 1, S("abcdefghijklmnopqrs"));
90 test(S("abcdefghijklmnopqrst"), 20, 0, S("abcdefghijklmnopqrst"));
91 }
92 #if TEST_STD_VER >= 11
93 {
94 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
95 test(S(""), 0, 0, S(""));
96 test(S("abcde"), 0, 0, S("abcde"));
97 test(S("abcde"), 0, 1, S("bcde"));
98 test(S("abcde"), 0, 2, S("cde"));
99 test(S("abcde"), 0, 4, S("e"));
100 test(S("abcde"), 0, 5, S(""));
101 test(S("abcde"), 1, 0, S("abcde"));
102 test(S("abcde"), 1, 1, S("acde"));
103 test(S("abcde"), 1, 2, S("ade"));
104 test(S("abcde"), 1, 3, S("ae"));
105 test(S("abcde"), 1, 4, S("a"));
106 test(S("abcde"), 2, 0, S("abcde"));
107 test(S("abcde"), 2, 1, S("abde"));
108 test(S("abcde"), 2, 2, S("abe"));
109 test(S("abcde"), 2, 3, S("ab"));
110 test(S("abcde"), 4, 0, S("abcde"));
111 test(S("abcde"), 4, 1, S("abcd"));
112 test(S("abcde"), 5, 0, S("abcde"));
113 test(S("abcdefghij"), 0, 0, S("abcdefghij"));
114 test(S("abcdefghij"), 0, 1, S("bcdefghij"));
115 test(S("abcdefghij"), 0, 5, S("fghij"));
116 test(S("abcdefghij"), 0, 9, S("j"));
117 test(S("abcdefghij"), 0, 10, S(""));
118 test(S("abcdefghij"), 1, 0, S("abcdefghij"));
119 test(S("abcdefghij"), 1, 1, S("acdefghij"));
120 test(S("abcdefghij"), 1, 4, S("afghij"));
121 test(S("abcdefghij"), 1, 8, S("aj"));
122 test(S("abcdefghij"), 1, 9, S("a"));
123 test(S("abcdefghij"), 5, 0, S("abcdefghij"));
124 test(S("abcdefghij"), 5, 1, S("abcdeghij"));
125 test(S("abcdefghij"), 5, 2, S("abcdehij"));
126 test(S("abcdefghij"), 5, 4, S("abcdej"));
127 test(S("abcdefghij"), 5, 5, S("abcde"));
128 test(S("abcdefghij"), 9, 0, S("abcdefghij"));
129 test(S("abcdefghij"), 9, 1, S("abcdefghi"));
130 test(S("abcdefghij"), 10, 0, S("abcdefghij"));
131 test(S("abcdefghijklmnopqrst"), 0, 0, S("abcdefghijklmnopqrst"));
132 test(S("abcdefghijklmnopqrst"), 0, 1, S("bcdefghijklmnopqrst"));
133 test(S("abcdefghijklmnopqrst"), 0, 10, S("klmnopqrst"));
134 test(S("abcdefghijklmnopqrst"), 0, 19, S("t"));
135 test(S("abcdefghijklmnopqrst"), 0, 20, S(""));
136 test(S("abcdefghijklmnopqrst"), 1, 0, S("abcdefghijklmnopqrst"));
137 test(S("abcdefghijklmnopqrst"), 1, 1, S("acdefghijklmnopqrst"));
138 test(S("abcdefghijklmnopqrst"), 1, 9, S("aklmnopqrst"));
139 test(S("abcdefghijklmnopqrst"), 1, 18, S("at"));
140 test(S("abcdefghijklmnopqrst"), 1, 19, S("a"));
141 test(S("abcdefghijklmnopqrst"), 10, 0, S("abcdefghijklmnopqrst"));
142 test(S("abcdefghijklmnopqrst"), 10, 1, S("abcdefghijlmnopqrst"));
143 test(S("abcdefghijklmnopqrst"), 10, 5, S("abcdefghijpqrst"));
144 test(S("abcdefghijklmnopqrst"), 10, 9, S("abcdefghijt"));
145 test(S("abcdefghijklmnopqrst"), 10, 10, S("abcdefghij"));
146 test(S("abcdefghijklmnopqrst"), 19, 0, S("abcdefghijklmnopqrst"));
147 test(S("abcdefghijklmnopqrst"), 19, 1, S("abcdefghijklmnopqrs"));
148 test(S("abcdefghijklmnopqrst"), 20, 0, S("abcdefghijklmnopqrst"));
149 }
150 #endif
151 }
152