]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/list_operators.cc
combine.c (try_combine): Set JUMP_LABEL for newly created unconditional jump.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / list_operators.cc
1 // Copyright (C) 2001 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without Pred the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 // USA.
18
19 // 23.2.2.4 list operations [lib.list.ops]
20
21 #include <list>
22 #include <testsuite_hooks.h>
23
24 bool test = true;
25
26 // splice(p, x) + remove + reverse
27 void
28 test01()
29 {
30 const int K = 417;
31 const int A[] = {1, 2, 3, 4, 5};
32 const int B[] = {K, K, K, K, K};
33 const int N = sizeof(A) / sizeof(int);
34 const int M = sizeof(B) / sizeof(int);
35
36 std::list<int> list0101(A, A + N);
37 std::list<int> list0102(B, B + M);
38 std::list<int>::iterator p = list0101.begin();
39
40 VERIFY(list0101.size() == N);
41 VERIFY(list0102.size() == M);
42
43 ++p;
44 list0101.splice(p, list0102); // [1 K K K K K 2 3 4 5]
45 VERIFY(list0101.size() == N + M);
46 VERIFY(list0102.size() == 0);
47
48 // remove range from middle
49 list0101.remove(K);
50 VERIFY(list0101.size() == N);
51
52 // remove first element
53 list0101.remove(1);
54 VERIFY(list0101.size() == N - 1);
55
56 // remove last element
57 list0101.remove(5);
58 VERIFY(list0101.size() == N - 2);
59
60 // reverse
61 list0101.reverse();
62 p = list0101.begin();
63 VERIFY(*p == 4); ++p;
64 VERIFY(*p == 3); ++p;
65 VERIFY(*p == 2); ++p;
66 VERIFY(p == list0101.end());
67 }
68
69 // splice(p, x, i) + remove_if + operator==
70 void
71 test02()
72 {
73 const int A[] = {1, 2, 3, 4, 5};
74 const int B[] = {2, 1, 3, 4, 5};
75 const int C[] = {1, 3, 4, 5, 2};
76 const int N = sizeof(A) / sizeof(int);
77 std::list<int> list0201(A, A + N);
78 std::list<int> list0202(A, A + N);
79 std::list<int> list0203(B, B + N);
80 std::list<int> list0204(C, C + N);
81 std::list<int>::iterator i = list0201.begin();
82
83 // result should be unchanged
84 list0201.splice(list0201.begin(), list0201, i);
85 VERIFY(list0201 == list0202);
86
87 // result should be [2 1 3 4 5]
88 ++i;
89 list0201.splice(list0201.begin(), list0201, i);
90 VERIFY(list0201 != list0202);
91 VERIFY(list0201 == list0203);
92
93 // result should be [1 3 4 5 2]
94 list0201.splice(list0201.end(), list0201, i);
95 VERIFY(list0201 == list0204);
96 }
97
98 // splice(p, x, f, l) + sort + merge + unique
99 void
100 test03()
101 {
102 const int A[] = {103, 203, 603, 303, 403, 503};
103 const int B[] = {417, 417, 417, 417, 417};
104 const int E[] = {103, 417, 417, 203, 603, 303, 403, 503};
105 const int F[] = {103, 203, 303, 403, 417, 417, 503, 603};
106 const int C[] = {103, 203, 303, 403, 417, 417, 417, 417, 417, 503, 603};
107 const int D[] = {103, 203, 303, 403, 417, 503, 603};
108 const int N = sizeof(A) / sizeof(int);
109 const int M = sizeof(B) / sizeof(int);
110 const int P = sizeof(C) / sizeof(int);
111 const int Q = sizeof(D) / sizeof(int);
112 const int R = sizeof(E) / sizeof(int);
113
114 std::list<int> list0301(A, A + N);
115 std::list<int> list0302(B, B + M);
116 std::list<int> list0303(C, C + P);
117 std::list<int> list0304(D, D + Q);
118 std::list<int> list0305(E, E + R);
119 std::list<int> list0306(F, F + R);
120 std::list<int>::iterator p = list0301.begin();
121 std::list<int>::iterator q = list0302.begin();
122
123 ++p; ++q; ++q;
124 list0301.splice(p, list0302, list0302.begin(), q);
125 VERIFY(list0301 == list0305);
126 VERIFY(list0301.size() == N + 2);
127 VERIFY(list0302.size() == M - 2);
128
129 list0301.sort();
130 VERIFY(list0301 == list0306);
131
132 list0301.merge(list0302);
133 VERIFY(list0301.size() == N + M);
134 VERIFY(list0302.size() == 0);
135 VERIFY(list0301 == list0303);
136
137 list0301.unique();
138 VERIFY(list0301 == list0304);
139 }
140
141 // A comparison predicate to order by rightmost digit. Tracks call counts for
142 // performance checks.
143 struct CompLastLt
144 {
145 bool operator()(const int x, const int y) { ++itsCount; return x % 10 < y % 10; }
146 static int count() { return itsCount; }
147 static void reset() { itsCount = 0; }
148 static int itsCount;
149 };
150
151 int CompLastLt::itsCount;
152
153 struct CompLastEq
154 {
155 bool operator()(const int x, const int y) { ++itsCount; return x % 10 == y % 10; }
156 static int count() { return itsCount; }
157 static void reset() { itsCount = 0; }
158 static int itsCount;
159 };
160
161 int CompLastEq::itsCount;
162
163 // sort(pred) + merge(pred) + unique(pred)
164 // also checks performance requirements
165 void
166 test04()
167 {
168 const int A[] = {1, 2, 3, 4, 5, 6};
169 const int B[] = {12, 15, 13, 14, 11};
170 const int C[] = {11, 12, 13, 14, 15};
171 const int D[] = {1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6};
172 const int N = sizeof(A) / sizeof(int);
173 const int M = sizeof(B) / sizeof(int);
174 const int Q = sizeof(D) / sizeof(int);
175
176 std::list<int> list0401(A, A + N);
177 std::list<int> list0402(B, B + M);
178 std::list<int> list0403(C, C + M);
179 std::list<int> list0404(D, D + Q);
180 std::list<int> list0405(A, A + N);
181
182 // sort B
183 CompLastLt lt;
184
185 CompLastLt::reset();
186 list0402.sort(lt);
187 VERIFY(list0402 == list0403);
188
189 CompLastLt::reset();
190 list0401.merge(list0402, lt);
191 VERIFY(list0401 == list0404);
192 VERIFY(lt.count() <= (N + M - 1));
193
194 CompLastEq eq;
195
196 CompLastEq::reset();
197 list0401.unique(eq);
198 VERIFY(list0401 == list0405);
199 VERIFY(eq.count() == (N + M - 1));
200 }
201
202 main(int argc, char* argv[])
203 {
204 test01();
205 test02();
206 test03();
207 test04();
208
209 return !test;
210 }
211 // vi:set sw=2 ts=2: