|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 //Has to be first for StackAllocator swap overload to be taken |
|
17 //into account (at least using GCC 4.0.1) |
|
18 #include "stack_allocator.h" |
|
19 #include <e32std.h> |
|
20 |
|
21 #include <deque> |
|
22 #include <algorithm> |
|
23 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) |
|
24 # include <stdexcept> |
|
25 #endif |
|
26 |
|
27 #include "cppunit/cppunit_proxy.h" |
|
28 |
|
29 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) |
|
30 using namespace std; |
|
31 #endif |
|
32 |
|
33 // |
|
34 // TestCase class |
|
35 // |
|
36 class DequeTest : public CPPUNIT_NS::TestCase |
|
37 { |
|
38 CPPUNIT_TEST_SUITE(DequeTest); |
|
39 CPPUNIT_TEST(deque1); |
|
40 CPPUNIT_TEST(at); |
|
41 CPPUNIT_TEST(insert); |
|
42 CPPUNIT_TEST(erase); |
|
43 CPPUNIT_TEST(auto_ref); |
|
44 CPPUNIT_TEST(allocator_with_state); |
|
45 #if defined (STLPORT) && defined (_STLP_NO_MEMBER_TEMPLATES) |
|
46 CPPUNIT_IGNORE; |
|
47 #endif |
|
48 CPPUNIT_TEST(optimizations_check); |
|
49 CPPUNIT_TEST(deque_cov1); |
|
50 CPPUNIT_TEST(deque_cov2); |
|
51 CPPUNIT_TEST_SUITE_END(); |
|
52 |
|
53 protected: |
|
54 void deque1(); |
|
55 void insert(); |
|
56 void erase(); |
|
57 void at(); |
|
58 void auto_ref(); |
|
59 void allocator_with_state(); |
|
60 void optimizations_check(); |
|
61 void deque_cov1(); |
|
62 void deque_cov2(); |
|
63 }; |
|
64 |
|
65 CPPUNIT_TEST_SUITE_REGISTRATION(DequeTest); |
|
66 |
|
67 // |
|
68 // tests implementation |
|
69 // |
|
70 void DequeTest::deque1() |
|
71 { |
|
72 deque<int> d; |
|
73 d.push_back(4); |
|
74 d.push_back(9); |
|
75 d.push_back(16); |
|
76 d.push_front(1); |
|
77 |
|
78 CPPUNIT_ASSERT( d[0] == 1 ); |
|
79 CPPUNIT_ASSERT( d[1] == 4 ); |
|
80 CPPUNIT_ASSERT( d[2] == 9 ); |
|
81 CPPUNIT_ASSERT( d[3] == 16 ); |
|
82 |
|
83 d.pop_front(); |
|
84 d[2] = 25; |
|
85 |
|
86 CPPUNIT_ASSERT( d[0] == 4 ); |
|
87 CPPUNIT_ASSERT( d[1] == 9 ); |
|
88 CPPUNIT_ASSERT( d[2] == 25 ); |
|
89 |
|
90 //Some compile time tests: |
|
91 deque<int>::iterator dit = d.begin(); |
|
92 deque<int>::const_iterator cdit(d.begin()); |
|
93 CPPUNIT_ASSERT( (dit - cdit) == 0 ); |
|
94 CPPUNIT_ASSERT( (cdit - dit) == 0 ); |
|
95 CPPUNIT_ASSERT( (dit - dit) == 0 ); |
|
96 CPPUNIT_ASSERT( (cdit - cdit) == 0 ); |
|
97 CPPUNIT_ASSERT(!((dit < cdit) || (dit > cdit) || (dit != cdit) || !(dit <= cdit) || !(dit >= cdit))); |
|
98 } |
|
99 |
|
100 void DequeTest::insert() |
|
101 { |
|
102 deque<int> d; |
|
103 d.push_back(0); |
|
104 d.push_back(1); |
|
105 d.push_back(2); |
|
106 CPPUNIT_ASSERT( d.size() == 3 ); |
|
107 |
|
108 deque<int>::iterator dit; |
|
109 |
|
110 //Insertion before begin: |
|
111 dit = d.insert(d.begin(), 3); |
|
112 CPPUNIT_ASSERT( dit != d.end() ); |
|
113 CPPUNIT_CHECK( *dit == 3 ); |
|
114 CPPUNIT_ASSERT( d.size() == 4 ); |
|
115 CPPUNIT_ASSERT( d[0] == 3 ); |
|
116 |
|
117 //Insertion after begin: |
|
118 dit = d.insert(d.begin() + 1, 4); |
|
119 CPPUNIT_ASSERT( dit != d.end() ); |
|
120 CPPUNIT_CHECK( *dit == 4 ); |
|
121 CPPUNIT_ASSERT( d.size() == 5 ); |
|
122 CPPUNIT_ASSERT( d[1] == 4 ); |
|
123 |
|
124 //Insertion at end: |
|
125 dit = d.insert(d.end(), 5); |
|
126 CPPUNIT_ASSERT( dit != d.end() ); |
|
127 CPPUNIT_CHECK( *dit == 5 ); |
|
128 CPPUNIT_ASSERT( d.size() == 6 ); |
|
129 CPPUNIT_ASSERT( d[5] == 5 ); |
|
130 |
|
131 //Insertion before last element: |
|
132 dit = d.insert(d.end() - 1, 6); |
|
133 CPPUNIT_ASSERT( dit != d.end() ); |
|
134 CPPUNIT_CHECK( *dit == 6 ); |
|
135 CPPUNIT_ASSERT( d.size() == 7 ); |
|
136 CPPUNIT_ASSERT( d[5] == 6 ); |
|
137 |
|
138 //Insertion of several elements before begin |
|
139 d.insert(d.begin(), 2, 7); |
|
140 CPPUNIT_ASSERT( d.size() == 9 ); |
|
141 CPPUNIT_ASSERT( d[0] == 7 ); |
|
142 CPPUNIT_ASSERT( d[1] == 7 ); |
|
143 |
|
144 //Insertion of several elements after begin |
|
145 //There is more elements to insert than elements before insertion position |
|
146 d.insert(d.begin() + 1, 2, 8); |
|
147 CPPUNIT_ASSERT( d.size() == 11 ); |
|
148 CPPUNIT_ASSERT( d[1] == 8 ); |
|
149 CPPUNIT_ASSERT( d[2] == 8 ); |
|
150 |
|
151 //There is less elements to insert than elements before insertion position |
|
152 d.insert(d.begin() + 3, 2, 9); |
|
153 CPPUNIT_ASSERT( d.size() == 13 ); |
|
154 CPPUNIT_ASSERT( d[3] == 9 ); |
|
155 CPPUNIT_ASSERT( d[4] == 9 ); |
|
156 |
|
157 //Insertion of several elements at end: |
|
158 d.insert(d.end(), 2, 10); |
|
159 CPPUNIT_ASSERT( d.size() == 15 ); |
|
160 CPPUNIT_ASSERT( d[14] == 10 ); |
|
161 CPPUNIT_ASSERT( d[13] == 10 ); |
|
162 |
|
163 //Insertion of several elements before last: |
|
164 //There is more elements to insert than elements after insertion position |
|
165 d.insert(d.end() - 1, 2, 11); |
|
166 CPPUNIT_ASSERT( d.size() == 17 ); |
|
167 CPPUNIT_ASSERT( d[15] == 11 ); |
|
168 CPPUNIT_ASSERT( d[14] == 11 ); |
|
169 |
|
170 //There is less elements to insert than elements after insertion position |
|
171 d.insert(d.end() - 3, 2, 12); |
|
172 CPPUNIT_ASSERT( d.size() == 19 ); |
|
173 CPPUNIT_ASSERT( d[15] == 12 ); |
|
174 CPPUNIT_ASSERT( d[14] == 12 ); |
|
175 } |
|
176 |
|
177 void DequeTest::at() { |
|
178 deque<int> d; |
|
179 deque<int> const& cd = d; |
|
180 |
|
181 d.push_back(10); |
|
182 CPPUNIT_ASSERT( d.at(0) == 10 ); |
|
183 d.at(0) = 20; |
|
184 CPPUNIT_ASSERT( cd.at(0) == 20 ); |
|
185 |
|
186 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) |
|
187 for (;;) { |
|
188 try { |
|
189 d.at(1) = 20; |
|
190 CPPUNIT_ASSERT(false); |
|
191 } |
|
192 catch (out_of_range const&) { |
|
193 return; |
|
194 } |
|
195 catch (...) { |
|
196 CPPUNIT_ASSERT(false); |
|
197 } |
|
198 } |
|
199 #endif |
|
200 } |
|
201 |
|
202 void DequeTest::auto_ref() |
|
203 { |
|
204 int i; |
|
205 deque<int> ref; |
|
206 for (i = 0; i < 5; ++i) { |
|
207 ref.push_back(i); |
|
208 } |
|
209 |
|
210 deque<deque<int> > d_d_int(1, ref); |
|
211 d_d_int.push_back(d_d_int[0]); |
|
212 d_d_int.push_back(ref); |
|
213 d_d_int.push_back(d_d_int[0]); |
|
214 d_d_int.push_back(d_d_int[0]); |
|
215 d_d_int.push_back(ref); |
|
216 |
|
217 for (i = 0; i < 5; ++i) { |
|
218 CPPUNIT_ASSERT( d_d_int[i] == ref ); |
|
219 } |
|
220 } |
|
221 |
|
222 void DequeTest::allocator_with_state() |
|
223 { |
|
224 char buf1[1024]; |
|
225 StackAllocator<int> stack1(buf1, buf1 + sizeof(buf1)); |
|
226 |
|
227 char buf2[1024]; |
|
228 StackAllocator<int> stack2(buf2, buf2 + sizeof(buf2)); |
|
229 |
|
230 { |
|
231 typedef deque<int, StackAllocator<int> > DequeInt; |
|
232 DequeInt dint1(10, 0, stack1); |
|
233 DequeInt dint1Cpy(dint1); |
|
234 |
|
235 DequeInt dint2(10, 1, stack2); |
|
236 DequeInt dint2Cpy(dint2); |
|
237 |
|
238 dint1.swap(dint2); |
|
239 |
|
240 CPPUNIT_ASSERT( dint1.get_allocator().swaped() ); |
|
241 CPPUNIT_ASSERT( dint2.get_allocator().swaped() ); |
|
242 |
|
243 CPPUNIT_ASSERT( dint1 == dint2Cpy ); |
|
244 CPPUNIT_ASSERT( dint2 == dint1Cpy ); |
|
245 CPPUNIT_ASSERT( dint1.get_allocator() == stack2 ); |
|
246 CPPUNIT_ASSERT( dint2.get_allocator() == stack1 ); |
|
247 } |
|
248 CPPUNIT_ASSERT( stack1.ok() ); |
|
249 CPPUNIT_ASSERT( stack2.ok() ); |
|
250 } |
|
251 |
|
252 struct Point { |
|
253 int x, y; |
|
254 }; |
|
255 |
|
256 struct PointEx : public Point { |
|
257 PointEx() : builtFromBase(false) {} |
|
258 PointEx(const Point&) : builtFromBase(true) {} |
|
259 |
|
260 bool builtFromBase; |
|
261 }; |
|
262 |
|
263 #if defined (STLPORT) |
|
264 namespace std { |
|
265 template <> |
|
266 struct __type_traits<PointEx> { |
|
267 typedef __false_type has_trivial_default_constructor; |
|
268 typedef __true_type has_trivial_copy_constructor; |
|
269 typedef __true_type has_trivial_assignment_operator; |
|
270 typedef __true_type has_trivial_destructor; |
|
271 typedef __true_type is_POD_type; |
|
272 }; |
|
273 } |
|
274 #endif |
|
275 |
|
276 //This test check that deque implementation do not over optimize |
|
277 //operation as PointEx copy constructor is trivial |
|
278 void DequeTest::optimizations_check() |
|
279 { |
|
280 #if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES) |
|
281 deque<Point> d1(1); |
|
282 CPPUNIT_ASSERT( d1.size() == 1 ); |
|
283 |
|
284 deque<PointEx> d2(d1.begin(), d1.end()); |
|
285 CPPUNIT_ASSERT( d2.size() == 1 ); |
|
286 CPPUNIT_ASSERT( d2[0].builtFromBase == true ); |
|
287 |
|
288 d2.insert(d2.end(), d1.begin(), d1.end()); |
|
289 CPPUNIT_ASSERT( d2.size() == 2 ); |
|
290 CPPUNIT_ASSERT( d2[1].builtFromBase == true ); |
|
291 #endif |
|
292 } |
|
293 |
|
294 void DequeTest::erase() |
|
295 { |
|
296 deque<int> dint; |
|
297 dint.push_back(3); |
|
298 dint.push_front(2); |
|
299 dint.push_back(4); |
|
300 dint.push_front(1); |
|
301 dint.push_back(5); |
|
302 dint.push_front(0); |
|
303 dint.push_back(6); |
|
304 |
|
305 deque<int>::iterator it(dint.begin() + 1); |
|
306 CPPUNIT_ASSERT( *it == 1 ); |
|
307 |
|
308 dint.erase(dint.begin()); |
|
309 CPPUNIT_ASSERT( *it == 1 ); |
|
310 |
|
311 it = dint.end() - 2; |
|
312 CPPUNIT_ASSERT( *it == 5 ); |
|
313 |
|
314 dint.erase(dint.end() - 1); |
|
315 CPPUNIT_ASSERT( *it == 5 ); |
|
316 |
|
317 dint.push_back(6); |
|
318 dint.push_front(0); |
|
319 |
|
320 it = dint.begin() + 2; |
|
321 CPPUNIT_ASSERT( *it == 2 ); |
|
322 |
|
323 dint.erase(dint.begin(), dint.begin() + 2); |
|
324 CPPUNIT_ASSERT( *it == 2 ); |
|
325 |
|
326 it = dint.end() - 3; |
|
327 CPPUNIT_ASSERT( *it == 4 ); |
|
328 |
|
329 dint.erase(dint.end() - 2, dint.end()); |
|
330 CPPUNIT_ASSERT( *it == 4 ); |
|
331 } |
|
332 void DequeTest::deque_cov1() |
|
333 { |
|
334 __UHEAP_MARK; |
|
335 { |
|
336 deque<int> mydeque; |
|
337 deque<int>::reverse_iterator rit; |
|
338 |
|
339 for (int i=1; i<=5; i++) |
|
340 mydeque.push_back(i); |
|
341 |
|
342 rit = mydeque.rbegin(); |
|
343 CPPUNIT_ASSERT(*rit == 5); |
|
344 rit = mydeque.rend(); |
|
345 rit--; |
|
346 CPPUNIT_ASSERT(*rit == 1); |
|
347 } |
|
348 { |
|
349 deque<int> d; |
|
350 deque<int> const& cd = d; |
|
351 deque<int>::const_reverse_iterator rit; |
|
352 |
|
353 for (int i=1; i<=5; i++) |
|
354 d.push_back(i); |
|
355 |
|
356 rit = cd.rbegin(); |
|
357 CPPUNIT_ASSERT(*rit == 5); |
|
358 rit = cd.rend(); |
|
359 rit--; |
|
360 CPPUNIT_ASSERT(*rit == 1); |
|
361 } |
|
362 { |
|
363 deque<int> mydeque; |
|
364 mydeque.max_size(); // test for compiling |
|
365 } |
|
366 { |
|
367 deque<int> mydeque; |
|
368 deque<int> const& cd = mydeque; |
|
369 for (int i=1; i<=5; i++) |
|
370 mydeque.push_back(i); |
|
371 |
|
372 CPPUNIT_ASSERT(cd.back() == 5) ; |
|
373 } |
|
374 __UHEAP_MARKEND; |
|
375 } |
|
376 void DequeTest::deque_cov2() |
|
377 { |
|
378 __UHEAP_MARK; |
|
379 { |
|
380 deque<int> mydeque; |
|
381 deque<int>::iterator it; |
|
382 mydeque.push_back(10); |
|
383 mydeque.push_back(20); |
|
384 mydeque.push_back(30); |
|
385 mydeque.resize(2); |
|
386 mydeque.resize(4,100); |
|
387 mydeque.resize(5); |
|
388 it=mydeque.begin(); |
|
389 CPPUNIT_ASSERT(*it == 10) ; |
|
390 it++; |
|
391 CPPUNIT_ASSERT(*it == 20) ; |
|
392 it++; |
|
393 CPPUNIT_ASSERT(*it == 100) ; |
|
394 it++; |
|
395 CPPUNIT_ASSERT(*it == 100) ; |
|
396 it++; |
|
397 CPPUNIT_ASSERT(*it == 0) ; |
|
398 } |
|
399 { |
|
400 deque <int> c1,c2; |
|
401 c1.push_back( 10 ); |
|
402 c1.push_back( 20 ); |
|
403 c1.push_back( 30 ); |
|
404 c2=c1; |
|
405 CPPUNIT_ASSERT(c1.size() == 3) ; |
|
406 CPPUNIT_ASSERT(c2.size() == 3) ; |
|
407 c1.clear(); |
|
408 CPPUNIT_ASSERT(c1.size() == 0) ; |
|
409 } |
|
410 { |
|
411 deque <int> c1, c2, c3; |
|
412 deque <int>::iterator c1_Iter; |
|
413 c1.push_back( 1 ); |
|
414 c1.push_back( 2 ); |
|
415 c1.push_back( 3 ); |
|
416 c3.push_back( 100 ); |
|
417 swap( c1 , c3 ); |
|
418 c1_Iter = c1.begin( ); |
|
419 CPPUNIT_ASSERT(*c1_Iter == 100) ; |
|
420 } |
|
421 __UHEAP_MARKEND; |
|
422 } |