First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
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"
23 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
27 #include "cppunit/cppunit_proxy.h"
29 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
36 class DequeTest : public CPPUNIT_NS::TestCase
38 CPPUNIT_TEST_SUITE(DequeTest);
43 CPPUNIT_TEST(auto_ref);
44 CPPUNIT_TEST(allocator_with_state);
45 #if defined (STLPORT) && defined (_STLP_NO_MEMBER_TEMPLATES)
48 CPPUNIT_TEST(optimizations_check);
49 CPPUNIT_TEST(deque_cov1);
50 CPPUNIT_TEST(deque_cov2);
51 CPPUNIT_TEST_SUITE_END();
59 void allocator_with_state();
60 void optimizations_check();
65 CPPUNIT_TEST_SUITE_REGISTRATION(DequeTest);
68 // tests implementation
70 void DequeTest::deque1()
78 CPPUNIT_ASSERT( d[0] == 1 );
79 CPPUNIT_ASSERT( d[1] == 4 );
80 CPPUNIT_ASSERT( d[2] == 9 );
81 CPPUNIT_ASSERT( d[3] == 16 );
86 CPPUNIT_ASSERT( d[0] == 4 );
87 CPPUNIT_ASSERT( d[1] == 9 );
88 CPPUNIT_ASSERT( d[2] == 25 );
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)));
100 void DequeTest::insert()
106 CPPUNIT_ASSERT( d.size() == 3 );
108 deque<int>::iterator dit;
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 );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
177 void DequeTest::at() {
179 deque<int> const& cd = d;
182 CPPUNIT_ASSERT( d.at(0) == 10 );
184 CPPUNIT_ASSERT( cd.at(0) == 20 );
186 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
190 CPPUNIT_ASSERT(false);
192 catch (out_of_range const&) {
196 CPPUNIT_ASSERT(false);
202 void DequeTest::auto_ref()
206 for (i = 0; i < 5; ++i) {
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);
217 for (i = 0; i < 5; ++i) {
218 CPPUNIT_ASSERT( d_d_int[i] == ref );
222 void DequeTest::allocator_with_state()
225 StackAllocator<int> stack1(buf1, buf1 + sizeof(buf1));
228 StackAllocator<int> stack2(buf2, buf2 + sizeof(buf2));
231 typedef deque<int, StackAllocator<int> > DequeInt;
232 DequeInt dint1(10, 0, stack1);
233 DequeInt dint1Cpy(dint1);
235 DequeInt dint2(10, 1, stack2);
236 DequeInt dint2Cpy(dint2);
240 CPPUNIT_ASSERT( dint1.get_allocator().swaped() );
241 CPPUNIT_ASSERT( dint2.get_allocator().swaped() );
243 CPPUNIT_ASSERT( dint1 == dint2Cpy );
244 CPPUNIT_ASSERT( dint2 == dint1Cpy );
245 CPPUNIT_ASSERT( dint1.get_allocator() == stack2 );
246 CPPUNIT_ASSERT( dint2.get_allocator() == stack1 );
248 CPPUNIT_ASSERT( stack1.ok() );
249 CPPUNIT_ASSERT( stack2.ok() );
256 struct PointEx : public Point {
257 PointEx() : builtFromBase(false) {}
258 PointEx(const Point&) : builtFromBase(true) {}
263 #if defined (STLPORT)
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;
276 //This test check that deque implementation do not over optimize
277 //operation as PointEx copy constructor is trivial
278 void DequeTest::optimizations_check()
280 #if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES)
282 CPPUNIT_ASSERT( d1.size() == 1 );
284 deque<PointEx> d2(d1.begin(), d1.end());
285 CPPUNIT_ASSERT( d2.size() == 1 );
286 CPPUNIT_ASSERT( d2[0].builtFromBase == true );
288 d2.insert(d2.end(), d1.begin(), d1.end());
289 CPPUNIT_ASSERT( d2.size() == 2 );
290 CPPUNIT_ASSERT( d2[1].builtFromBase == true );
294 void DequeTest::erase()
305 deque<int>::iterator it(dint.begin() + 1);
306 CPPUNIT_ASSERT( *it == 1 );
308 dint.erase(dint.begin());
309 CPPUNIT_ASSERT( *it == 1 );
312 CPPUNIT_ASSERT( *it == 5 );
314 dint.erase(dint.end() - 1);
315 CPPUNIT_ASSERT( *it == 5 );
320 it = dint.begin() + 2;
321 CPPUNIT_ASSERT( *it == 2 );
323 dint.erase(dint.begin(), dint.begin() + 2);
324 CPPUNIT_ASSERT( *it == 2 );
327 CPPUNIT_ASSERT( *it == 4 );
329 dint.erase(dint.end() - 2, dint.end());
330 CPPUNIT_ASSERT( *it == 4 );
332 void DequeTest::deque_cov1()
337 deque<int>::reverse_iterator rit;
339 for (int i=1; i<=5; i++)
340 mydeque.push_back(i);
342 rit = mydeque.rbegin();
343 CPPUNIT_ASSERT(*rit == 5);
344 rit = mydeque.rend();
346 CPPUNIT_ASSERT(*rit == 1);
350 deque<int> const& cd = d;
351 deque<int>::const_reverse_iterator rit;
353 for (int i=1; i<=5; i++)
357 CPPUNIT_ASSERT(*rit == 5);
360 CPPUNIT_ASSERT(*rit == 1);
364 mydeque.max_size(); // test for compiling
368 deque<int> const& cd = mydeque;
369 for (int i=1; i<=5; i++)
370 mydeque.push_back(i);
372 CPPUNIT_ASSERT(cd.back() == 5) ;
376 void DequeTest::deque_cov2()
381 deque<int>::iterator it;
382 mydeque.push_back(10);
383 mydeque.push_back(20);
384 mydeque.push_back(30);
386 mydeque.resize(4,100);
389 CPPUNIT_ASSERT(*it == 10) ;
391 CPPUNIT_ASSERT(*it == 20) ;
393 CPPUNIT_ASSERT(*it == 100) ;
395 CPPUNIT_ASSERT(*it == 100) ;
397 CPPUNIT_ASSERT(*it == 0) ;
405 CPPUNIT_ASSERT(c1.size() == 3) ;
406 CPPUNIT_ASSERT(c2.size() == 3) ;
408 CPPUNIT_ASSERT(c1.size() == 0) ;
411 deque <int> c1, c2, c3;
412 deque <int>::iterator c1_Iter;
418 c1_Iter = c1.begin( );
419 CPPUNIT_ASSERT(*c1_Iter == 100) ;