Update contrib.
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"
24 #include "cppunit/cppunit_proxy.h"
26 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
33 class SetTest : public CPPUNIT_NS::TestCase
35 CPPUNIT_TEST_SUITE(SetTest);
42 CPPUNIT_TEST(specialized_less);
43 CPPUNIT_TEST(implementation_check);
44 CPPUNIT_TEST(allocator_with_state);
45 CPPUNIT_TEST(reverse_iterator_test);
46 #if !defined (STLPORT) || !defined (_STLP_USE_CONTAINERS_EXTENSION)
49 CPPUNIT_TEST(template_methods);
50 CPPUNIT_TEST(set_cov1);
51 CPPUNIT_TEST(set_cov2);
52 CPPUNIT_TEST(set_cov3);
53 CPPUNIT_TEST(multiset_cov1);
54 CPPUNIT_TEST(multiset_cov2);
55 CPPUNIT_TEST(multiset_cov3);
56 CPPUNIT_TEST_SUITE_END();
65 void specialized_less();
66 void implementation_check();
67 void allocator_with_state();
68 void reverse_iterator_test();
69 void template_methods();
78 CPPUNIT_TEST_SUITE_REGISTRATION(SetTest);
82 // tests implementation
86 set<int, less<int> > s;
87 CPPUNIT_ASSERT (s.count(42) == 0);
89 CPPUNIT_ASSERT (s.count(42) == 1);
91 CPPUNIT_ASSERT (s.count(42) == 1);
92 size_t count = s.erase(42);
93 CPPUNIT_ASSERT (count == 1);
98 typedef set<int, less<int> > int_set;
100 pair<int_set::iterator, bool> p = s.insert(42);
101 CPPUNIT_ASSERT (p.second == true);
103 CPPUNIT_ASSERT (p.second == false);
105 int array1 [] = { 1, 3, 6, 7 };
106 s.insert(array1, array1 + 4);
107 CPPUNIT_ASSERT (distance(s.begin(), s.end()) == 5);
111 CPPUNIT_ASSERT (distance(s2.begin(), s2.end()) == 5);
112 CPPUNIT_ASSERT (distance(s.begin(), s.end()) == 0);
117 CPPUNIT_ASSERT (distance(s.begin(), s.end()) == 0);
118 CPPUNIT_ASSERT (distance(s2.begin(), s2.end()) == 0);
119 CPPUNIT_ASSERT (distance(s3.begin(), s3.end()) == 5);
122 void SetTest::erase()
124 set<int, less<int> > s;
127 CPPUNIT_ASSERT( s.empty() );
129 size_t nb = s.erase(1);
130 CPPUNIT_ASSERT(nb == 0);
133 void SetTest::insert()
136 set<int>::iterator i = s.insert( s.end(), 0 );
137 CPPUNIT_ASSERT( *i == 0 );
144 CPPUNIT_ASSERT( s.find(0) == s.end() );
146 set<int> const& crs = s;
148 CPPUNIT_ASSERT( crs.find(0) == crs.end() );
151 void SetTest::bounds()
153 int array1 [] = { 1, 3, 6, 7 };
154 set<int> s(array1, array1 + sizeof(array1) / sizeof(array1[0]));
155 set<int> const& crs = s;
157 set<int>::iterator sit;
158 set<int>::const_iterator scit;
159 pair<set<int>::iterator, set<int>::iterator> pit;
160 pair<set<int>::const_iterator, set<int>::const_iterator> pcit;
162 //Check iterator on mutable set
163 sit = s.lower_bound(2);
164 CPPUNIT_ASSERT( sit != s.end() );
165 CPPUNIT_ASSERT( *sit == 3 );
167 sit = s.upper_bound(5);
168 CPPUNIT_ASSERT( sit != s.end() );
169 CPPUNIT_ASSERT( *sit == 6 );
171 pit = s.equal_range(6);
172 CPPUNIT_ASSERT( pit.first != pit.second );
173 CPPUNIT_ASSERT( pit.first != s.end() );
174 CPPUNIT_ASSERT( *pit.first == 6 );
175 CPPUNIT_ASSERT( pit.second != s.end() );
176 CPPUNIT_ASSERT( *pit.second == 7 );
178 pit = s.equal_range(4);
179 CPPUNIT_ASSERT( pit.first == pit.second );
180 CPPUNIT_ASSERT( pit.first != s.end() );
181 CPPUNIT_ASSERT( *pit.first == 6 );
182 CPPUNIT_ASSERT( pit.second != s.end() );
183 CPPUNIT_ASSERT( *pit.second == 6 );
185 //Check const_iterator on mutable set
186 scit = s.lower_bound(2);
187 CPPUNIT_ASSERT( scit != s.end() );
188 CPPUNIT_ASSERT( *scit == 3 );
190 scit = s.upper_bound(5);
191 CPPUNIT_ASSERT( scit != s.end() );
192 CPPUNIT_ASSERT( *scit == 6 );
194 #ifdef _STLP_MEMBER_TEMPLATES
195 pcit = s.equal_range(6);
196 CPPUNIT_ASSERT( pcit.first != pcit.second );
197 CPPUNIT_ASSERT( pcit.first != s.end() );
198 CPPUNIT_ASSERT( *pcit.first == 6 );
199 CPPUNIT_ASSERT( pcit.second != s.end() );
200 CPPUNIT_ASSERT( *pcit.second == 7 );
203 //Check const_iterator on const set
204 scit = crs.lower_bound(2);
205 CPPUNIT_ASSERT( scit != crs.end() );
206 CPPUNIT_ASSERT( *scit == 3 );
208 scit = crs.upper_bound(5);
209 CPPUNIT_ASSERT( scit != crs.end() );
210 CPPUNIT_ASSERT( *scit == 6 );
212 pcit = crs.equal_range(6);
213 CPPUNIT_ASSERT( pcit.first != pcit.second );
214 CPPUNIT_ASSERT( pcit.first != crs.end() );
215 CPPUNIT_ASSERT( *pcit.first == 6 );
216 CPPUNIT_ASSERT( pcit.second != crs.end() );
217 CPPUNIT_ASSERT( *pcit.second == 7 );
223 SetTestClass (int data) : _data(data)
236 struct less<SetTestClass> {
237 bool operator () (SetTestClass const& lhs, SetTestClass const& rhs) const {
238 return lhs.data() < rhs.data();
243 void SetTest::specialized_less()
246 s.insert(SetTestClass(1));
247 s.insert(SetTestClass(3));
248 s.insert(SetTestClass(2));
249 s.insert(SetTestClass(0));
251 set<SetTestClass>::iterator sit(s.begin()), sitEnd(s.end());
253 for (; sit != sitEnd; ++sit, ++i) {
254 CPPUNIT_ASSERT( sit->data() == i );
258 void SetTest::implementation_check()
262 set<int>::iterator it = tree.begin();
263 int const& int_ref = *it++;
264 CPPUNIT_ASSERT( int_ref == 1 );
266 CPPUNIT_ASSERT( it == tree.end() );
267 CPPUNIT_ASSERT( it != tree.begin() );
269 set<int>::const_iterator cit = tree.begin();
270 int const& int_cref = *cit++;
271 CPPUNIT_ASSERT( int_cref == 1 );
274 void SetTest::reverse_iterator_test()
281 set<int>::reverse_iterator rit(tree.rbegin());
282 CPPUNIT_ASSERT( *(rit++) == 2 );
283 CPPUNIT_ASSERT( *(rit++) == 1 );
284 CPPUNIT_ASSERT( rit == tree.rend() );
288 set<int> const& ctree = tree;
289 set<int>::const_reverse_iterator rit(ctree.rbegin());
290 CPPUNIT_ASSERT( *(rit++) == 2 );
291 CPPUNIT_ASSERT( *(rit++) == 1 );
292 CPPUNIT_ASSERT( rit == ctree.rend() );
296 void SetTest::allocator_with_state()
299 StackAllocator<int> stack1(buf1, buf1 + sizeof(buf1));
302 StackAllocator<int> stack2(buf2, buf2 + sizeof(buf2));
305 typedef set<int, less<int>, StackAllocator<int> > SetInt;
309 SetInt sint1(intLess, stack1);
310 for (i = 0; i < 5; ++i)
312 SetInt sint1Cpy(sint1);
314 SetInt sint2(intLess, stack2);
317 SetInt sint2Cpy(sint2);
321 CPPUNIT_ASSERT( sint1.get_allocator().swaped() );
322 CPPUNIT_ASSERT( sint2.get_allocator().swaped() );
324 CPPUNIT_ASSERT( sint1 == sint2Cpy );
325 CPPUNIT_ASSERT( sint2 == sint1Cpy );
326 CPPUNIT_ASSERT( sint1.get_allocator() == stack2 );
327 CPPUNIT_ASSERT( sint2.get_allocator() == stack1 );
329 CPPUNIT_ASSERT( stack1.ok() );
330 CPPUNIT_ASSERT( stack2.ok() );
331 stack1.reset(); stack2.reset();
334 SetInt sint1(intLess, stack1);
335 SetInt sint1Cpy(sint1);
337 SetInt sint2(intLess, stack2);
338 for (i = 0; i < 10; ++i)
340 SetInt sint2Cpy(sint2);
344 CPPUNIT_ASSERT( sint1.get_allocator().swaped() );
345 CPPUNIT_ASSERT( sint2.get_allocator().swaped() );
347 CPPUNIT_ASSERT( sint1 == sint2Cpy );
348 CPPUNIT_ASSERT( sint2 == sint1Cpy );
349 CPPUNIT_ASSERT( sint1.get_allocator() == stack2 );
350 CPPUNIT_ASSERT( sint2.get_allocator() == stack1 );
352 CPPUNIT_ASSERT( stack1.ok() );
353 CPPUNIT_ASSERT( stack2.ok() );
354 stack1.reset(); stack2.reset();
357 SetInt sint1(intLess, stack1);
358 for (i = 0; i < 10; ++i)
360 SetInt sint1Cpy(sint1);
362 SetInt sint2(intLess, stack2);
363 SetInt sint2Cpy(sint2);
367 CPPUNIT_ASSERT( sint1.get_allocator().swaped() );
368 CPPUNIT_ASSERT( sint2.get_allocator().swaped() );
370 CPPUNIT_ASSERT( sint1 == sint2Cpy );
371 CPPUNIT_ASSERT( sint2 == sint1Cpy );
372 CPPUNIT_ASSERT( sint1.get_allocator() == stack2 );
373 CPPUNIT_ASSERT( sint2.get_allocator() == stack1 );
375 CPPUNIT_ASSERT( stack1.ok() );
376 CPPUNIT_ASSERT( stack2.ok() );
377 stack1.reset(); stack2.reset();
383 explicit Key(int data) : m_data(data) {}
390 bool operator () (Key lhs, Key rhs) const
391 { return lhs.m_data < rhs.m_data; }
393 bool operator () (Key lhs, int rhs) const
394 { return lhs.m_data < rhs; }
396 bool operator () (int lhs, Key rhs) const
397 { return lhs < rhs.m_data; }
402 bool operator () (Key const volatile *lhs, Key const volatile *rhs) const
403 { return (*lhs).m_data < (*rhs).m_data; }
405 bool operator () (Key const volatile *lhs, int rhs) const
406 { return (*lhs).m_data < rhs; }
408 bool operator () (int lhs, Key const volatile *rhs) const
409 { return lhs < (*rhs).m_data; }
412 void SetTest::template_methods()
414 #if defined (STLPORT) && defined (_STLP_USE_CONTAINERS_EXTENSION)
416 typedef set<Key, KeyCmp> KeySet;
418 keySet.insert(Key(1));
419 keySet.insert(Key(2));
420 keySet.insert(Key(3));
421 keySet.insert(Key(4));
423 CPPUNIT_ASSERT( keySet.count(Key(1)) == 1 );
424 CPPUNIT_ASSERT( keySet.count(1) == 1 );
425 CPPUNIT_ASSERT( keySet.count(5) == 0 );
427 CPPUNIT_ASSERT( keySet.find(2) != keySet.end() );
428 CPPUNIT_ASSERT( keySet.lower_bound(2) != keySet.end() );
429 CPPUNIT_ASSERT( keySet.upper_bound(2) != keySet.end() );
430 CPPUNIT_ASSERT( keySet.equal_range(2) != make_pair(keySet.begin(), keySet.end()) );
432 KeySet const& ckeySet = keySet;
433 CPPUNIT_ASSERT( ckeySet.find(2) != ckeySet.end() );
434 CPPUNIT_ASSERT( ckeySet.lower_bound(2) != ckeySet.end() );
435 CPPUNIT_ASSERT( ckeySet.upper_bound(2) != ckeySet.end() );
436 CPPUNIT_ASSERT( ckeySet.equal_range(2) != make_pair(ckeySet.begin(), ckeySet.end()) );
440 typedef set<Key*, KeyCmpPtr> KeySet;
442 Key key1(1), key2(2), key3(3), key4(4);
443 keySet.insert(&key1);
444 keySet.insert(&key2);
445 keySet.insert(&key3);
446 keySet.insert(&key4);
448 CPPUNIT_ASSERT( keySet.count(1) == 1 );
449 CPPUNIT_ASSERT( keySet.count(5) == 0 );
451 CPPUNIT_ASSERT( keySet.find(2) != keySet.end() );
452 CPPUNIT_ASSERT( keySet.lower_bound(2) != keySet.end() );
453 CPPUNIT_ASSERT( keySet.upper_bound(2) != keySet.end() );
454 CPPUNIT_ASSERT( keySet.equal_range(2) != make_pair(keySet.begin(), keySet.end()) );
456 KeySet const& ckeySet = keySet;
457 CPPUNIT_ASSERT( ckeySet.find(2) != ckeySet.end() );
458 CPPUNIT_ASSERT( ckeySet.lower_bound(2) != ckeySet.end() );
459 CPPUNIT_ASSERT( ckeySet.upper_bound(2) != ckeySet.end() );
460 CPPUNIT_ASSERT( ckeySet.equal_range(2) != make_pair(ckeySet.begin(), ckeySet.end()) );
463 typedef multiset<Key, KeyCmp> KeySet;
465 keySet.insert(Key(1));
466 keySet.insert(Key(2));
467 keySet.insert(Key(3));
468 keySet.insert(Key(4));
470 CPPUNIT_ASSERT( keySet.count(Key(1)) == 1 );
471 CPPUNIT_ASSERT( keySet.count(1) == 1 );
472 CPPUNIT_ASSERT( keySet.count(5) == 0 );
474 CPPUNIT_ASSERT( keySet.find(2) != keySet.end() );
475 CPPUNIT_ASSERT( keySet.lower_bound(2) != keySet.end() );
476 CPPUNIT_ASSERT( keySet.upper_bound(2) != keySet.end() );
477 CPPUNIT_ASSERT( keySet.equal_range(2) != make_pair(keySet.begin(), keySet.end()) );
479 KeySet const& ckeySet = keySet;
480 CPPUNIT_ASSERT( ckeySet.find(2) != ckeySet.end() );
481 CPPUNIT_ASSERT( ckeySet.lower_bound(2) != ckeySet.end() );
482 CPPUNIT_ASSERT( ckeySet.upper_bound(2) != ckeySet.end() );
483 CPPUNIT_ASSERT( ckeySet.equal_range(2) != make_pair(ckeySet.begin(), ckeySet.end()) );
487 typedef multiset<Key const volatile*, KeyCmpPtr> KeySet;
489 Key key1(1), key2(2), key3(3), key4(4);
490 keySet.insert(&key1);
491 keySet.insert(&key2);
492 keySet.insert(&key3);
493 keySet.insert(&key4);
495 CPPUNIT_ASSERT( keySet.count(1) == 1 );
496 CPPUNIT_ASSERT( keySet.count(5) == 0 );
498 CPPUNIT_ASSERT( keySet.find(2) != keySet.end() );
499 CPPUNIT_ASSERT( keySet.lower_bound(2) != keySet.end() );
500 CPPUNIT_ASSERT( keySet.upper_bound(2) != keySet.end() );
501 CPPUNIT_ASSERT( keySet.equal_range(2) != make_pair(keySet.begin(), keySet.end()) );
503 KeySet const& ckeySet = keySet;
504 CPPUNIT_ASSERT( ckeySet.find(2) != ckeySet.end() );
505 CPPUNIT_ASSERT( ckeySet.lower_bound(2) != ckeySet.end() );
506 CPPUNIT_ASSERT( ckeySet.upper_bound(2) != ckeySet.end() );
507 CPPUNIT_ASSERT( ckeySet.equal_range(2) != make_pair(ckeySet.begin(), ckeySet.end()) );
511 void SetTest::set_cov1()
515 set <int, less<int> > s1;
516 set <int, less<int> >::value_compare vc1 = s1.value_comp( );
517 bool result1 = vc1( 2, 3 );
518 CPPUNIT_ASSERT( result1 == true );
520 set <int, greater<int> > s2;
521 set<int, greater<int> >::value_compare vc2 = s2.value_comp( );
522 bool result2 = vc2( 2, 3 );
523 CPPUNIT_ASSERT( result2 == false );
526 set <int>::iterator s2_pIter;
527 set <int> :: size_type i;
528 set <int, less<int> > s1, s2;
536 s2_pIter = s2.begin( );
537 CPPUNIT_ASSERT(*s2_pIter == 10);
540 CPPUNIT_ASSERT(i == 4);
548 void SetTest::set_cov2()
552 set <int, less<int> > s1;
553 set<int, less<int> >::key_compare kc1 = s1.key_comp( ) ;
554 bool result1 = kc1( 2, 3 ) ;
555 CPPUNIT_ASSERT( result1 == true );
559 set <int> :: size_type i;
560 set <int> :: iterator pIter, Iter1, Iter2;
563 for ( i1 = 1 ; i1 < 5 ; i1++ )
565 s1.insert ( i1 * i1 );
567 Iter1 = ++s1.begin( );
569 s1.erase( Iter1, Iter2 );
570 pIter = s1.begin( ) ;
571 CPPUNIT_ASSERT(*pIter == 1);
573 CPPUNIT_ASSERT(*pIter == 16);
575 CPPUNIT_ASSERT(i == 2);
579 void SetTest::set_cov3()
588 CPPUNIT_ASSERT(s1.size()== 2);
590 CPPUNIT_ASSERT(s1.size()== 0);
601 CPPUNIT_ASSERT(val == true);
604 typedef set<int, less<int> > int_set;
606 pair<int_set::iterator, bool> p = s.insert(42);
607 CPPUNIT_ASSERT (p.second == true);
609 CPPUNIT_ASSERT (p.second == false);
611 int array1 [] = { 1, 3, 6, 7 };
612 s.insert(array1, array1 + 4);
613 CPPUNIT_ASSERT (distance(s.begin(), s.end()) == 5);
617 CPPUNIT_ASSERT (distance(s2.begin(), s2.end()) == 5);
618 CPPUNIT_ASSERT (distance(s.begin(), s.end()) == 0);
622 void SetTest::multiset_cov1()
626 multiset<int, less<int> > ms1;
627 multiset <int, less<int> >::value_compare vc1 = ms1.value_comp( );
628 bool result1 = vc1( 2, 3 );
629 CPPUNIT_ASSERT( result1 == true );
631 multiset<int, greater<int> > ms2;
632 multiset<int, greater<int> >::value_compare vc2 = ms2.value_comp( );
633 bool result2 = vc2( 2, 3 );
634 CPPUNIT_ASSERT( result2 == false );
637 multiset <int> ms1, ms2;
638 multiset <int>::iterator ms1_Iter;
639 multiset <int> :: size_type i;
647 ms1_Iter = ms1.begin( );
648 CPPUNIT_ASSERT(*ms1_Iter== 100);
651 CPPUNIT_ASSERT(i == 1);
659 multiset<int>::reverse_iterator rit(tree.rbegin());
660 CPPUNIT_ASSERT( *(rit++) == 2 );
661 CPPUNIT_ASSERT( *(rit++) == 1 );
662 CPPUNIT_ASSERT( rit == tree.rend() );
666 multiset<int> const& ctree = tree;
667 multiset<int>::const_reverse_iterator rit(ctree.rbegin());
668 CPPUNIT_ASSERT( *(rit++) == 2 );
669 CPPUNIT_ASSERT( *(rit++) == 1 );
670 CPPUNIT_ASSERT( rit == ctree.rend() );
675 void SetTest::multiset_cov2()
679 multiset<int>::iterator ms2_pIter;
680 multiset<int> :: size_type i;
681 multiset<int> ms1, ms2;
689 ms2_pIter = ms2.begin( );
690 CPPUNIT_ASSERT(*ms2_pIter == 10);
693 CPPUNIT_ASSERT(i == 4);
701 multiset<int>::key_compare kc1 = ms1.key_comp( ) ;
702 bool result1 = kc1( 2, 3 ) ;
703 CPPUNIT_ASSERT( result1 == true );
707 StackAllocator<int> stack1(buf1, buf1 + sizeof(buf1));
710 StackAllocator<int> stack2(buf2, buf2 + sizeof(buf2));
713 typedef multiset<int, less<int>, StackAllocator<int> > SetInt;
716 SetInt sint1(intLess, stack1);
717 for (i = 0; i < 5; ++i)
719 SetInt sint1Cpy(sint1);
721 SetInt sint2(intLess, stack2);
724 SetInt sint2Cpy(sint2);
728 CPPUNIT_ASSERT( sint1.get_allocator().swaped() );
729 CPPUNIT_ASSERT( sint2.get_allocator().swaped() );
731 CPPUNIT_ASSERT( sint1 == sint2Cpy );
732 CPPUNIT_ASSERT( sint2 == sint1Cpy );
733 CPPUNIT_ASSERT( sint1.get_allocator() == stack2 );
734 CPPUNIT_ASSERT( sint2.get_allocator() == stack1 );
736 CPPUNIT_ASSERT( stack1.ok() );
737 CPPUNIT_ASSERT( stack2.ok() );
738 stack1.reset(); stack2.reset();
742 void SetTest::multiset_cov3()
747 multiset<int> :: size_type i;
748 multiset<int> :: iterator pIter, Iter1, Iter2;
751 for ( i1 = 1 ; i1 < 5 ; i1++ )
753 s1.insert ( i1 * i1 );
755 Iter1 = ++s1.begin( );
757 s1.erase( Iter1, Iter2 );
758 pIter = s1.begin( ) ;
759 CPPUNIT_ASSERT(*pIter == 1);
761 CPPUNIT_ASSERT(*pIter == 16);
763 CPPUNIT_ASSERT(i == 2);
771 CPPUNIT_ASSERT(s1.size()== 2);
773 CPPUNIT_ASSERT(s1.size()== 0);
777 multiset<int>::iterator i = s.insert( s.end(), 0 );
778 CPPUNIT_ASSERT( *i == 0 );
783 int array1 [] = { 1, 3, 6, 7 };
784 s.insert(array1, array1 + 4);
785 CPPUNIT_ASSERT (distance(s.begin(), s.end()) == 4);
789 multiset <int> ms1, ms2;
790 multiset <int>::iterator ms1_Iter;
791 multiset <int> :: size_type i;
799 ms1_Iter = ms1.begin( );
800 CPPUNIT_ASSERT(*ms1_Iter== 100);
803 CPPUNIT_ASSERT(i == 1);
806 multiset <int> ms1, ms2;
810 bool val = ms1 < ms2;
811 CPPUNIT_ASSERT( val == true);