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.
21 # include <unordered_map>
22 # include <unordered_set>
27 #include "cppunit/cppunit_proxy.h"
29 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
36 class UnorderedTest : public CPPUNIT_NS::TestCase
38 CPPUNIT_TEST_SUITE(UnorderedTest);
39 #if !defined (STLPORT)
43 CPPUNIT_TEST(umultiset);
49 CPPUNIT_TEST(umultimap);
53 CPPUNIT_TEST(user_case);
55 CPPUNIT_TEST(hash_policy);
56 CPPUNIT_TEST(buckets);
60 CPPUNIT_TEST(equal_range);
61 #if !defined (_STLP_USE_CONTAINERS_EXTENSION)
64 CPPUNIT_TEST(template_methods);
65 CPPUNIT_TEST(unordered_set_cov1);
66 CPPUNIT_TEST(unordered_set_cov2);
67 CPPUNIT_TEST(unordered_set_cov3);
68 CPPUNIT_TEST(unordered_map_cov1);
69 CPPUNIT_TEST(unordered_map_cov2);
70 CPPUNIT_TEST(unordered_map_cov3);
71 CPPUNIT_TEST(unordered_map_cov4);
72 CPPUNIT_TEST(unordered_map_cov5);
73 CPPUNIT_TEST(unordered_multimap_cov1);
74 CPPUNIT_TEST(unordered_multimap_cov2);
75 CPPUNIT_TEST(unordered_multimap_cov3);
76 CPPUNIT_TEST(unordered_multimap_cov4);
77 CPPUNIT_TEST(unordered_multimap_cov5);
78 CPPUNIT_TEST(unordered_multiset_cov1);
79 CPPUNIT_TEST(unordered_multiset_cov2);
80 CPPUNIT_TEST(unordered_multiset_cov3);
81 CPPUNIT_TEST(unordered_multiset_cov4);
82 CPPUNIT_TEST_SUITE_END();
93 void template_methods();
94 void unordered_set_cov1();
95 void unordered_set_cov2();
96 void unordered_set_cov3();
97 void unordered_map_cov1();
98 void unordered_map_cov2();
99 void unordered_map_cov3();
100 void unordered_map_cov4();
101 void unordered_map_cov5();
102 void unordered_multimap_cov1();
103 void unordered_multimap_cov2();
104 void unordered_multimap_cov3();
105 void unordered_multimap_cov4();
106 void unordered_multimap_cov5();
107 void unordered_multiset_cov1();
108 void unordered_multiset_cov2();
109 void unordered_multiset_cov3();
110 void unordered_multiset_cov4();
113 CPPUNIT_TEST_SUITE_REGISTRATION(UnorderedTest);
115 const int NB_ELEMS = 2000;
118 // tests implementation
120 void UnorderedTest::uset()
122 #if defined (STLPORT)
123 typedef unordered_set<int, hash<int>, equal_to<int> > usettype;
126 //Small compilation check of the copy constructor:
128 //And assignment operator
132 pair<usettype::iterator, bool> ret;
133 for (i = 0; i < NB_ELEMS; ++i) {
135 CPPUNIT_ASSERT( ret.second );
136 CPPUNIT_ASSERT( *ret.first == i );
139 CPPUNIT_ASSERT( !ret.second );
140 CPPUNIT_ASSERT( *ret.first == i );
145 usettype::local_iterator lit, litEnd;
146 for (i = 0; i < NB_ELEMS; ++i) {
147 lit = us.begin(us.bucket(i));
148 litEnd = us.end(us.bucket(i));
150 usettype::size_type bucket_pos = us.bucket(*lit);
151 for (; lit != litEnd; ++lit) {
152 CPPUNIT_ASSERT( us.bucket(*lit) == bucket_pos );
153 us_val.push_back(*lit);
157 //A compilation time check to uncomment from time to time
159 //usettype::iterator it;
160 //CPPUNIT_ASSERT( it != lit );
163 sort(us_val.begin(), us_val.end());
164 for (i = 0; i < NB_ELEMS; ++i) {
165 CPPUNIT_ASSERT( us_val[i] == i );
170 void UnorderedTest::umultiset()
172 #if defined (STLPORT)
173 typedef unordered_multiset<int, hash<int>, equal_to<int> > usettype;
177 usettype::iterator ret;
178 for (i = 0; i < NB_ELEMS; ++i) {
180 CPPUNIT_ASSERT( *ret == i );
183 CPPUNIT_ASSERT( *ret == i );
186 CPPUNIT_ASSERT( us.size() == 2 * NB_ELEMS );
189 usettype::local_iterator lit, litEnd;
190 for (i = 0; i < NB_ELEMS; ++i) {
191 lit = us.begin(us.bucket(i));
192 litEnd = us.end(us.bucket(i));
194 usettype::size_type bucket_pos = us.bucket(*lit);
195 for (; lit != litEnd; ++lit) {
196 CPPUNIT_ASSERT( us.bucket(*lit) == bucket_pos );
197 us_val.push_back(*lit);
201 sort(us_val.begin(), us_val.end());
202 for (i = 0; i < NB_ELEMS; ++i) {
203 CPPUNIT_ASSERT( us_val[2 * i] == i );
204 CPPUNIT_ASSERT( us_val[2 * i + 1] == i );
209 void UnorderedTest::umap()
211 #if defined (STLPORT) && !defined (__DMC__)
212 typedef unordered_map<int, int, hash<int>, equal_to<int> > umaptype;
215 //Compilation check of the [] operator:
221 //An other compilation check
222 typedef unordered_map<int, umaptype> uumaptype;
224 umaptype const& uref = uus[0];
225 umaptype ucopy = uus[0];
232 pair<umaptype::iterator, bool> ret;
233 for (i = 0; i < NB_ELEMS; ++i) {
234 umaptype::value_type p1(i, i);
236 CPPUNIT_ASSERT( ret.second );
237 CPPUNIT_ASSERT( *ret.first == p1 );
239 umaptype::value_type p2(i, i + 1);
241 CPPUNIT_ASSERT( !ret.second );
242 CPPUNIT_ASSERT( *ret.first == p1 );
246 //Lets look for some values to see if everything is normal:
247 umaptype::iterator umit;
248 for (int j = 0; j < NB_ELEMS; j += NB_ELEMS / 100) {
251 CPPUNIT_ASSERT( umit != us.end() );
252 CPPUNIT_ASSERT( (*umit).second == j );
256 CPPUNIT_ASSERT( us.size() == (size_t)NB_ELEMS );
257 vector<pair<int, int> > us_val;
259 umaptype::local_iterator lit, litEnd;
260 for (i = 0; i < NB_ELEMS; ++i) {
261 lit = us.begin(us.bucket(i));
262 litEnd = us.end(us.bucket(i));
264 umaptype::size_type bucket_pos = us.bucket((*lit).first);
265 for (; lit != litEnd; ++lit) {
266 CPPUNIT_ASSERT( us.bucket((*lit).first) == bucket_pos );
267 us_val.push_back(make_pair((*lit).first, (*lit).second));
271 sort(us_val.begin(), us_val.end());
272 for (i = 0; i < NB_ELEMS; ++i) {
273 CPPUNIT_ASSERT( us_val[i] == make_pair(i, i) );
278 void UnorderedTest::umultimap()
280 #if defined (STLPORT)
281 typedef unordered_multimap<int, int, hash<int>, equal_to<int> > umaptype;
285 umaptype::iterator ret;
286 for (i = 0; i < NB_ELEMS; ++i) {
287 umaptype::value_type p(i, i);
289 CPPUNIT_ASSERT( *ret == p );
292 CPPUNIT_ASSERT( *ret == p );
295 CPPUNIT_ASSERT( us.size() == 2 * NB_ELEMS );
296 typedef pair<int, int> ptype;
297 vector<ptype> us_val;
299 umaptype::local_iterator lit, litEnd;
300 for (i = 0; i < NB_ELEMS; ++i) {
301 lit = us.begin(us.bucket(i));
302 litEnd = us.end(us.bucket(i));
304 umaptype::size_type bucket_pos = us.bucket((*lit).first);
305 for (; lit != litEnd; ++lit) {
306 CPPUNIT_ASSERT( us.bucket((*lit).first) == bucket_pos );
307 us_val.push_back(ptype((*lit).first, (*lit).second));
311 sort(us_val.begin(), us_val.end());
312 for (i = 0; i < NB_ELEMS; ++i) {
314 CPPUNIT_ASSERT( us_val[i * 2] == p );
315 CPPUNIT_ASSERT( us_val[i * 2 + 1] == p );
320 void UnorderedTest::user_case()
322 #if defined (STLPORT) && !defined (__DMC__)
323 typedef unordered_map<int, string> UnorderedMap1;
324 typedef unordered_map<int, UnorderedMap1> UnorderedMap2;
329 foo.insert(UnorderedMap1::value_type(1, string("test1")));
330 foo.insert(UnorderedMap1::value_type(2, string("test2")));
331 foo.insert(UnorderedMap1::value_type(3, string("test3")));
332 foo.insert(UnorderedMap1::value_type(4, string("test4")));
333 foo.insert(UnorderedMap1::value_type(5, string("test5")));
335 bar.insert(UnorderedMap2::value_type(0, foo));
336 UnorderedMap2::iterator it = bar.find(0);
337 CPPUNIT_ASSERT( it != bar.end() );
339 UnorderedMap1 &body = it->second;
340 UnorderedMap1::iterator cur = body.find(3);
341 CPPUNIT_ASSERT( cur != body.end() );
343 body.erase(body.begin(), body.end());
344 CPPUNIT_ASSERT( body.empty() );
348 void UnorderedTest::hash_policy()
350 #if defined (STLPORT)
351 unordered_set<int> int_uset;
353 CPPUNIT_ASSERT( int_uset.max_load_factor() == 1.0f );
354 CPPUNIT_ASSERT( int_uset.load_factor() == 0.0f );
356 size_t nbInserts = int_uset.bucket_count() - 1;
357 for (int i = 0; (size_t)i < nbInserts; ++i) {
360 CPPUNIT_ASSERT( int_uset.size() == nbInserts );
362 int_uset.max_load_factor(0.5f);
364 CPPUNIT_ASSERT( int_uset.load_factor() < int_uset.max_load_factor() );
366 size_t bucketsHint = int_uset.bucket_count() + 1;
367 int_uset.rehash(bucketsHint);
368 CPPUNIT_ASSERT( int_uset.bucket_count() >= bucketsHint );
370 CPPUNIT_ASSERT( int_uset.key_eq()(10, 10) );
371 CPPUNIT_ASSERT( int_uset.hash_function()(10) == 10 );
375 void UnorderedTest::buckets()
377 #if defined (STLPORT)
378 unordered_set<int> int_uset;
380 CPPUNIT_ASSERT( int_uset.bucket_count() < int_uset.max_bucket_count() );
383 size_t nbBuckets = int_uset.bucket_count();
384 size_t nbInserts = int_uset.bucket_count() - 1;
385 for (i = 0; (size_t)i < nbInserts; ++i) {
388 CPPUNIT_ASSERT( nbBuckets == int_uset.bucket_count() );
390 size_t bucketSizes = 0;
391 for (i = 0; (size_t)i < nbBuckets; ++i) {
392 bucketSizes += int_uset.bucket_size(i);
394 CPPUNIT_ASSERT( bucketSizes == int_uset.size() );
398 void UnorderedTest::equal_range()
400 #if defined (STLPORT) && !defined (__DMC__)
401 typedef unordered_multiset<size_t> umset;
405 iumset.max_load_factor(10.0f);
407 size_t nbBuckets = iumset.bucket_count();
409 for (size_t i = 0; i < nbBuckets; ++i) {
411 iumset.insert(i + nbBuckets);
412 iumset.insert(i + 2 * nbBuckets);
413 iumset.insert(i + 3 * nbBuckets);
414 iumset.insert(i + 4 * nbBuckets);
417 CPPUNIT_ASSERT( nbBuckets == iumset.bucket_count() );
418 CPPUNIT_ASSERT( iumset.size() == 5 * nbBuckets );
420 pair<umset::iterator, umset::iterator> p = iumset.equal_range(1);
421 CPPUNIT_ASSERT( p.first != p.second );
423 size_t nbElems = iumset.size();
424 nbElems -= distance(p.first, p.second);
425 for (umset::iterator j = p.first; j != p.second;) {
429 CPPUNIT_ASSERT( nbElems == iumset.size() );
431 p = iumset.equal_range(2);
432 CPPUNIT_ASSERT( p.first != p.second );
433 nbElems -= distance(p.first, p.second);
434 iumset.erase(p.first, p.second);
435 CPPUNIT_ASSERT( nbElems == iumset.size() );
439 //More specific test that tries to put many values in the same bucket
443 const size_t nbBuckets = iumset.bucket_count();
444 const size_t targetedBucket = nbBuckets / 2;
446 //Lets put 10 values in the targeted bucket:
447 for (i = 0; i < 10; ++i) {
448 iumset.insert(targetedBucket + (i * nbBuckets));
451 //We put again 10 values in the targeted bucket and in reverse order:
452 for (i = 9; i <= 10; --i) {
453 iumset.insert(targetedBucket + (i * nbBuckets));
456 //Now we put some more elements until hash container is resized:
458 while (iumset.bucket_count() == nbBuckets) {
462 //CPPUNIT_ASSERT( iumset.bucket_size(targetedBucket) == 21 );
464 pair<umset::iterator, umset::iterator> p = iumset.equal_range(targetedBucket);
465 CPPUNIT_ASSERT( p.first != p.second );
466 CPPUNIT_ASSERT( distance(p.first, p.second) == 3 );
471 for (int runs = 0; runs < 2; ++runs) {
472 size_t magic = rand();
475 for (int i = 0; i < 10000; ++i) {
476 if ((rand() % 500) == 0) {
482 while ((r = rand()) == magic);
487 if ((float)(hum.size() + 1) / (float)hum.bucket_count() > hum.max_load_factor()) {
488 cout << "Hash container dump: Nb elems: " << hum.size() << ", Nb buckets: " << hum.bucket_count() << "\n";
489 for (size_t b = 0; b < hum.bucket_count(); ++b) {
490 if (hum.bucket_size(b) != 0) {
491 umset::local_iterator litBegin(hum.begin(b)), litEnd(hum.end(b));
492 cout << "B" << b << ": ";
493 for (umset::local_iterator lit = litBegin; lit != litEnd; ++lit) {
494 if (lit != litBegin) {
506 CPPUNIT_ASSERT( hum.count(magic) == c );
515 explicit Key(int data) : m_data(data) {}
522 size_t operator () (Key key) const
523 { return (size_t)key.m_data; }
525 size_t operator () (int data) const
526 { return (size_t)data; }
531 bool operator () (Key lhs, Key rhs) const
532 { return lhs.m_data == rhs.m_data; }
534 bool operator () (Key lhs, int rhs) const
535 { return lhs.m_data == rhs; }
537 bool operator () (int lhs, Key rhs) const
538 { return lhs == rhs.m_data; }
543 size_t operator () (Key const volatile *key) const
544 { return (size_t)key->m_data; }
546 size_t operator () (int data) const
547 { return (size_t)data; }
552 bool operator () (Key const volatile *lhs, Key const volatile *rhs) const
553 { return lhs->m_data == rhs->m_data; }
555 bool operator () (Key const volatile *lhs, int rhs) const
556 { return lhs->m_data == rhs; }
558 bool operator () (int lhs, Key const volatile *rhs) const
559 { return lhs == rhs->m_data; }
562 void UnorderedTest::template_methods()
564 #if defined (STLPORT) && defined (_STLP_USE_CONTAINERS_EXTENSION)
566 typedef unordered_set<Key, KeyHash, KeyEqual> Container;
573 CPPUNIT_ASSERT( cont.count(Key(1)) == 1 );
574 CPPUNIT_ASSERT( cont.count(1) == 1 );
575 CPPUNIT_ASSERT( cont.count(5) == 0 );
577 CPPUNIT_ASSERT( cont.find(2) != cont.end() );
578 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) );
580 Container const& ccont = cont;
581 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() );
582 CPPUNIT_ASSERT( ccont.bucket(2) == ccont.bucket(2) );
583 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.begin(), ccont.end()) );
587 typedef unordered_set<Key*, KeyHashPtr, KeyEqualPtr> Container;
589 Key key1(1), key2(2), key3(3), key4(4);
595 CPPUNIT_ASSERT( cont.count(1) == 1 );
596 CPPUNIT_ASSERT( cont.count(5) == 0 );
598 CPPUNIT_ASSERT( cont.find(2) != cont.end() );
599 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) );
601 Container const& ccont = cont;
602 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() );
603 CPPUNIT_ASSERT( ccont.bucket(2) == ccont.bucket(2) );
604 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.begin(), ccont.end()) );
607 typedef unordered_multiset<Key, KeyHash, KeyEqual> Container;
614 CPPUNIT_ASSERT( cont.count(Key(1)) == 2 );
615 CPPUNIT_ASSERT( cont.count(1) == 2 );
616 CPPUNIT_ASSERT( cont.count(5) == 0 );
618 CPPUNIT_ASSERT( cont.find(2) != cont.end() );
619 CPPUNIT_ASSERT( cont.equal_range(1) != make_pair(cont.end(), cont.end()) );
621 Container const& ccont = cont;
622 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() );
623 CPPUNIT_ASSERT( ccont.bucket(2) == ccont.bucket(2) );
624 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.end(), ccont.end()) );
628 typedef unordered_multiset<Key const volatile*, KeyHashPtr, KeyEqualPtr> Container;
630 Key key1(1), key2(2), key3(3), key4(4);
636 CPPUNIT_ASSERT( cont.count(1) == 1 );
637 CPPUNIT_ASSERT( cont.count(5) == 0 );
639 CPPUNIT_ASSERT( cont.find(2) != cont.end() );
640 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) );
642 Container const& ccont = cont;
643 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() );
644 CPPUNIT_ASSERT( ccont.bucket(2) == ccont.bucket(2) );
645 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.begin(), ccont.end()) );
649 void UnorderedTest::unordered_set_cov1()
653 typedef unordered_set<char> Myset;
665 Myset::const_iterator it1 = c1.begin();
666 Myset::const_iterator it2 = c2.begin();
667 CPPUNIT_ASSERT( *it1 == 'd' );
668 CPPUNIT_ASSERT( *it2 == 'a' );
670 CPPUNIT_ASSERT( *it1 == 'e' );
671 CPPUNIT_ASSERT( *it2 == 'b' );
673 CPPUNIT_ASSERT( *it1 == 'f' );
674 CPPUNIT_ASSERT( *it2 == 'c' );
677 typedef unordered_set<char> Myset;
689 Myset::const_iterator it1 = c1.begin();
690 Myset::const_iterator it2 = c2.begin();
691 CPPUNIT_ASSERT( *it1 == 'd' );
692 CPPUNIT_ASSERT( *it2 == 'a' );
694 CPPUNIT_ASSERT( *it1 == 'e' );
695 CPPUNIT_ASSERT( *it2 == 'b' );
697 CPPUNIT_ASSERT( *it1 == 'f' );
698 CPPUNIT_ASSERT( *it2 == 'c' );
701 typedef unordered_set<char> Myset;
708 void UnorderedTest::unordered_set_cov2()
712 typedef unordered_set<char> Myset;
720 CPPUNIT_ASSERT(c1.size() == 0 );
721 CPPUNIT_ASSERT(c1.empty() );
724 typedef unordered_set<char> Myset;
731 Myset::const_iterator it = c1.end();
733 //CPPUNIT_ASSERT(*it == 'a' );
738 typedef unordered_set<char> Myset;
745 c1.erase(c1.begin());
746 Myset::iterator it2 = c1.begin();
747 CPPUNIT_ASSERT(*it2 == 'b' );
749 c1.erase(c1.begin(), c1.end());
750 CPPUNIT_ASSERT(c1.size() == 0 );
753 typedef unordered_set<char> Myset;
759 CPPUNIT_ASSERT( c1.erase('b') == 1);
763 void UnorderedTest::unordered_set_cov3()
767 typedef unordered_set<char> Mymap;
768 typedef allocator<std::pair<const char, int> > Myalloc;
771 Mymap::allocator_type al = c1.get_allocator();
772 CPPUNIT_ASSERT ((al == Myalloc()) == true);
778 void UnorderedTest::unordered_map_cov1()
782 typedef unordered_map<char, int> Mymap;
785 c1.insert(Mymap::value_type('a', 1));
786 c1.insert(Mymap::value_type('b', 2));
787 c1.insert(Mymap::value_type('c', 3));
789 Mymap::const_iterator it = c1.begin();
790 CPPUNIT_ASSERT( it->first == 'a');
791 CPPUNIT_ASSERT( it->second == 1);
793 Mymap::const_iterator it1 = c1.end();
795 Mymap::const_local_iterator lit = c1.begin(c1.bucket('a'));
796 CPPUNIT_ASSERT( lit->first == 'a');
797 CPPUNIT_ASSERT( lit->second == 1);
799 Mymap::const_local_iterator lit1 = c1.end(c1.bucket('a'));
802 typedef unordered_map<char, int> Mymap;
805 c1.insert(Mymap::value_type('a', 1));
806 c1.insert(Mymap::value_type('b', 2));
807 c1.insert(Mymap::value_type('c', 3));
810 Mymap::size_type bs = c1.bucket('a');
811 CPPUNIT_ASSERT( c1.bucket_size(bs) == 1);
813 CPPUNIT_ASSERT( c1.count('A') == 0);
814 CPPUNIT_ASSERT( c1.count('b') == 1);
815 CPPUNIT_ASSERT( c1.count('C') == 0);
819 void UnorderedTest::unordered_map_cov2()
823 typedef unordered_map<char, int> Mymap;
826 c1.insert(Mymap::value_type('a', 1));
827 c1.insert(Mymap::value_type('b', 2));
828 c1.insert(Mymap::value_type('c', 3));
830 std::pair<Mymap::iterator, Mymap::iterator> pair1 = c1.equal_range('b');
831 CPPUNIT_ASSERT( pair1.first->first == 'b');
832 CPPUNIT_ASSERT( pair1.first->second == 2);
834 std::pair<Mymap::iterator, Mymap::const_iterator> pair2 = c1.equal_range('c');
835 CPPUNIT_ASSERT( pair2.first->first == 'c');
836 CPPUNIT_ASSERT( pair2.first->second == 3);
839 typedef unordered_map<char, int> Mymap;
842 c1.insert(Mymap::value_type('a', 1));
843 c1.insert(Mymap::value_type('b', 2));
844 c1.insert(Mymap::value_type('c', 3));
846 c1.erase(c1.begin());
847 Mymap::iterator it2 = c1.begin();
848 CPPUNIT_ASSERT( it2->first == 'b');
849 CPPUNIT_ASSERT( it2->second == 2);
851 CPPUNIT_ASSERT( c1.erase('b') == 1);
854 typedef unordered_map<char, int> Mymap;
857 c1.insert(Mymap::value_type('a', 1));
858 c1.insert(Mymap::value_type('b', 2));
859 c1.insert(Mymap::value_type('c', 3));
861 Mymap::iterator it = c1.find('b');
862 CPPUNIT_ASSERT( it->first == 'b');
863 CPPUNIT_ASSERT( it->second == 2);
865 // negative test case where the element to find is not present in the
867 Mymap::iterator it1 = c1.find('d');
868 Mymap::iterator it2 = c1.end();
869 CPPUNIT_ASSERT( it1 == it2);
873 void UnorderedTest::unordered_map_cov3()
877 typedef unordered_map<char, int> Mymap;
878 typedef allocator<std::pair<const char, int> > Myalloc;
881 Mymap::allocator_type al = c1.get_allocator();
882 CPPUNIT_ASSERT ((al == Myalloc()) == true);
884 Mymap::hasher hfn = c1.hash_function();
885 hfn('a');// returns the hash function object
888 typedef unordered_map<char, int> Mymap;
891 c1.insert(Mymap::value_type('a', 1));
892 c1.insert(Mymap::value_type('b', 2));
893 c1.insert(Mymap::value_type('c', 3));
897 c2.insert(c1.begin(), c1.end());
899 CPPUNIT_ASSERT( it2->first == 'a');
900 CPPUNIT_ASSERT( it2->second == 1);
903 typedef unordered_map<char, int> Mymap;
905 Mymap::key_equal cmpfn = c1.key_eq();
906 CPPUNIT_ASSERT( cmpfn('a','a') == true);
910 void UnorderedTest::unordered_map_cov4()
914 typedef unordered_map<char, int> Mymap;
917 c1.insert(Mymap::value_type('a', 1));
918 c1.insert(Mymap::value_type('b', 2));
919 c1.insert(Mymap::value_type('c', 3));
921 c2.insert(Mymap::value_type('d', 4));
922 c2.insert(Mymap::value_type('e', 5));
923 c2.insert(Mymap::value_type('f', 6));
927 Mymap::iterator it1 = c1.begin();
928 CPPUNIT_ASSERT( it1->first == 'd');
929 CPPUNIT_ASSERT( it1->second == 4);
931 CPPUNIT_ASSERT( it1->first == 'e');
932 CPPUNIT_ASSERT( it1->second == 5);
934 CPPUNIT_ASSERT( it1->first == 'f');
935 CPPUNIT_ASSERT( it1->second == 6);
938 typedef unordered_map<char, int> Mymap;
941 c1.insert(Mymap::value_type('a', 1));
942 c1.insert(Mymap::value_type('b', 2));
943 c1.insert(Mymap::value_type('c', 3));
945 c2.insert(Mymap::value_type('d', 4));
946 c2.insert(Mymap::value_type('e', 5));
947 c2.insert(Mymap::value_type('f', 6));
951 Mymap::iterator it1 = c1.begin();
952 CPPUNIT_ASSERT( it1->first == 'd');
953 CPPUNIT_ASSERT( it1->second == 4);
955 CPPUNIT_ASSERT( it1->first == 'e');
956 CPPUNIT_ASSERT( it1->second == 5);
958 CPPUNIT_ASSERT( it1->first == 'f');
959 CPPUNIT_ASSERT( it1->second == 6);
963 void UnorderedTest::unordered_map_cov5()
967 typedef unordered_map<char, int> Mymap;
970 c1.insert(Mymap::value_type('a', 1));
971 c1.insert(Mymap::value_type('b', 2));
972 c1.insert(Mymap::value_type('c', 3));
973 a = (float)( (float)c1.size()/(float)c1.bucket_count() ) ;
974 b = (float)c1.load_factor();
975 CPPUNIT_ASSERT( a == b );
977 c1.max_load_factor(0.10f);
978 CPPUNIT_ASSERT( c1.max_load_factor() == 0.1f );
980 CPPUNIT_ASSERT( c1.max_bucket_count() >= c1.bucket_count() );
984 typedef unordered_map<char, int> Mymap;
989 typedef unordered_map<char, int> Mymap;
992 c1.insert(Mymap::value_type('a', 1));
993 c1.insert(Mymap::value_type('b', 2));
994 c1.insert(Mymap::value_type('c', 3));
996 Mymap c3(c1.begin(),c1.end(),8,hash<char>(),equal_to<char>(),allocator<std::pair<const char, int> >());
997 Mymap::iterator it1 = c3.begin();
998 CPPUNIT_ASSERT( it1->first == 'a');
999 CPPUNIT_ASSERT( it1->second == 1);
1001 CPPUNIT_ASSERT( it1->first == 'b');
1002 CPPUNIT_ASSERT( it1->second == 2);
1004 CPPUNIT_ASSERT( it1->first == 'c');
1005 CPPUNIT_ASSERT( it1->second == 3);
1011 void UnorderedTest::unordered_multimap_cov1()
1015 typedef unordered_multimap<char, int> Mymap;
1018 c1.insert(Mymap::value_type('a', 1));
1019 c1.insert(Mymap::value_type('b', 2));
1020 c1.insert(Mymap::value_type('c', 3));
1022 Mymap::const_iterator it = c1.begin();
1023 Mymap::iterator it2 = c1.begin();
1025 CPPUNIT_ASSERT( it->first == it2->first);
1026 CPPUNIT_ASSERT( it->second == it2->second);
1028 Mymap::const_local_iterator lit = c1.begin(c1.bucket('a'));
1029 CPPUNIT_ASSERT( lit->first == 'a');
1030 CPPUNIT_ASSERT( lit->second == 1);
1033 typedef unordered_multimap<char, int> Mymap;
1036 c1.insert(Mymap::value_type('a', 1));
1037 c1.insert(Mymap::value_type('b', 2));
1038 c1.insert(Mymap::value_type('c', 3));
1041 Mymap::size_type bs = c1.bucket('a');
1042 CPPUNIT_ASSERT( c1.bucket_size(bs) == 1);
1044 CPPUNIT_ASSERT( c1.count('A') == 0);
1045 CPPUNIT_ASSERT( c1.count('b') == 1);
1046 CPPUNIT_ASSERT( c1.count('C') == 0);
1049 typedef unordered_multimap<char, int> Mymap;
1052 c1.insert(Mymap::value_type('a', 1));
1053 c1.insert(Mymap::value_type('b', 2));
1054 c1.insert(Mymap::value_type('c', 3));
1057 CPPUNIT_ASSERT( c1.size() == 0);
1058 CPPUNIT_ASSERT( c1.empty() == true);
1062 void UnorderedTest::unordered_multimap_cov2()
1066 typedef unordered_multimap<char, int> Mymap;
1069 c1.insert(Mymap::value_type('a', 1));
1071 Mymap::const_local_iterator lit = c1.end(c1.bucket('a'));
1074 typedef unordered_multimap<char, int> Mymap;
1077 c1.insert(Mymap::value_type('a', 1));
1078 c1.insert(Mymap::value_type('b', 2));
1079 c1.insert(Mymap::value_type('c', 3));
1081 std::pair<Mymap::iterator, Mymap::iterator> pair1 = c1.equal_range('b');
1082 CPPUNIT_ASSERT( pair1.first->first == 'b');
1083 CPPUNIT_ASSERT( pair1.first->second == 2);
1085 std::pair<Mymap::iterator, Mymap::const_iterator> pair2 = c1.equal_range('c');
1086 CPPUNIT_ASSERT( pair2.first->first == 'c');
1087 CPPUNIT_ASSERT( pair2.first->second == 3);
1090 typedef unordered_multimap<char, int> Mymap;
1093 c1.insert(Mymap::value_type('a', 1));
1094 c1.insert(Mymap::value_type('b', 2));
1095 c1.insert(Mymap::value_type('c', 3));
1097 c1.erase(c1.begin());
1098 Mymap::iterator it2 = c1.begin();
1099 CPPUNIT_ASSERT( it2->first == 'b');
1100 CPPUNIT_ASSERT( it2->second == 2);
1102 CPPUNIT_ASSERT( c1.erase('b') == 1);
1104 c1.erase(c1.begin(), c1.end());
1105 CPPUNIT_ASSERT( c1.empty() );
1109 void UnorderedTest::unordered_multimap_cov3()
1113 typedef unordered_map<char, int> Mymap;
1116 c1.insert(Mymap::value_type('a', 1));
1117 c1.insert(Mymap::value_type('b', 2));
1118 c1.insert(Mymap::value_type('c', 3));
1120 Mymap::iterator it = c1.find('b');
1121 CPPUNIT_ASSERT( it->first == 'b');
1122 CPPUNIT_ASSERT( it->second == 2);
1124 Mymap::const_iterator it1 = c1.find('c');
1125 CPPUNIT_ASSERT( it1->first == 'c');
1126 CPPUNIT_ASSERT( it1->second == 3);
1129 typedef unordered_multimap<char, int> Mymap;
1130 typedef allocator<std::pair<const char, int> > Myalloc;
1133 Mymap::allocator_type al = c1.get_allocator();
1134 CPPUNIT_ASSERT ((al == Myalloc()) == true);
1136 Mymap::hasher hfn = c1.hash_function();
1137 hfn('a');// returns the hash function object
1140 typedef unordered_multimap<char, int> Mymap;
1143 c1.insert(Mymap::value_type('a', 1));
1144 c1.insert(Mymap::value_type('b', 2));
1145 c1.insert(Mymap::value_type('c', 3));
1147 Mymap::iterator it2;
1149 c2.insert(c1.begin(), c1.end());
1151 CPPUNIT_ASSERT( it2->first == 'a');
1152 CPPUNIT_ASSERT( it2->second == 1);
1156 void UnorderedTest::unordered_multimap_cov4()
1160 typedef unordered_multimap<char, int> Mymap;
1162 Mymap::key_equal cmpfn = c1.key_eq();
1163 CPPUNIT_ASSERT( cmpfn('a','a') == true);
1166 typedef unordered_multimap<char, int> Mymap;
1169 c1.insert(Mymap::value_type('a', 1));
1170 c1.insert(Mymap::value_type('b', 2));
1171 c1.insert(Mymap::value_type('c', 3));
1173 c2.insert(Mymap::value_type('d', 4));
1174 c2.insert(Mymap::value_type('e', 5));
1175 c2.insert(Mymap::value_type('f', 6));
1179 Mymap::iterator it1 = c1.begin();
1180 CPPUNIT_ASSERT( it1->first == 'd');
1181 CPPUNIT_ASSERT( it1->second == 4);
1183 CPPUNIT_ASSERT( it1->first == 'e');
1184 CPPUNIT_ASSERT( it1->second == 5);
1186 CPPUNIT_ASSERT( it1->first == 'f');
1187 CPPUNIT_ASSERT( it1->second == 6);
1190 typedef unordered_multimap<char, int> Mymap;
1193 c1.insert(Mymap::value_type('a', 1));
1194 c1.insert(Mymap::value_type('b', 2));
1195 c1.insert(Mymap::value_type('c', 3));
1197 c2.insert(Mymap::value_type('d', 4));
1198 c2.insert(Mymap::value_type('e', 5));
1199 c2.insert(Mymap::value_type('f', 6));
1203 Mymap::iterator it1 = c1.begin();
1204 CPPUNIT_ASSERT( it1->first == 'd');
1205 CPPUNIT_ASSERT( it1->second == 4);
1207 CPPUNIT_ASSERT( it1->first == 'e');
1208 CPPUNIT_ASSERT( it1->second == 5);
1210 CPPUNIT_ASSERT( it1->first == 'f');
1211 CPPUNIT_ASSERT( it1->second == 6);
1214 typedef unordered_multimap<char, int> Mymap;
1218 c1.insert(Mymap::value_type('a', 1));
1219 c1.insert(Mymap::value_type('b', 2));
1220 c1.insert(Mymap::value_type('c', 3));
1222 a = (float)( (float)c1.size()/(float)c1.bucket_count() ) ;
1223 b = (float)c1.load_factor();
1224 CPPUNIT_ASSERT( a == b );
1226 c1.max_load_factor(0.10f);
1227 CPPUNIT_ASSERT( c1.max_load_factor() == 0.1f );
1229 CPPUNIT_ASSERT( c1.max_bucket_count() >= c1.bucket_count() );
1234 void UnorderedTest::unordered_multimap_cov5()
1238 typedef unordered_multimap<char, int> Mymap;
1243 typedef unordered_multimap<char, int> Mymap;
1246 c1.insert(Mymap::value_type('a', 1));
1247 c1.insert(Mymap::value_type('b', 2));
1248 c1.insert(Mymap::value_type('c', 3));
1251 Mymap::iterator it2;
1254 CPPUNIT_ASSERT( it2->first == 'a');
1255 CPPUNIT_ASSERT( it2->second == 1);
1258 typedef unordered_multimap<char, int> Mymap;
1261 c1.insert(Mymap::value_type('a', 1));
1262 c1.insert(Mymap::value_type('b', 2));
1263 c1.insert(Mymap::value_type('c', 3));
1265 Mymap c3(c1.begin(),c1.end(),8,hash<char>(),equal_to<char>(),allocator<std::pair<const char, int> >());
1266 Mymap::iterator it1 = c3.begin();
1267 CPPUNIT_ASSERT( it1->first == 'a');
1268 CPPUNIT_ASSERT( it1->second == 1);
1270 CPPUNIT_ASSERT( it1->first == 'b');
1271 CPPUNIT_ASSERT( it1->second == 2);
1273 CPPUNIT_ASSERT( it1->first == 'c');
1274 CPPUNIT_ASSERT( it1->second == 3);
1279 void UnorderedTest::unordered_multiset_cov1()
1283 #if defined (STLPORT)
1284 unordered_multiset<int> int_uset;
1286 CPPUNIT_ASSERT( int_uset.bucket_count() < int_uset.max_bucket_count() );
1289 size_t nbBuckets = int_uset.bucket_count();
1290 size_t nbInserts = int_uset.bucket_count() - 1;
1291 for (i = 0; (size_t)i < nbInserts; ++i) {
1294 CPPUNIT_ASSERT( nbBuckets == int_uset.bucket_count() );
1296 size_t bucketSizes = 0;
1297 for (i = 0; (size_t)i < nbBuckets; ++i) {
1298 bucketSizes += int_uset.bucket_size(i);
1300 CPPUNIT_ASSERT( bucketSizes == int_uset.size() );
1304 #if defined (STLPORT)
1305 unordered_multiset<int> int_uset;
1307 CPPUNIT_ASSERT( int_uset.max_load_factor() == 1.0f );
1308 CPPUNIT_ASSERT( int_uset.load_factor() == 0.0f );
1310 size_t nbInserts = int_uset.bucket_count() - 1;
1311 for (int i = 0; (size_t)i < nbInserts; ++i) {
1314 CPPUNIT_ASSERT( int_uset.size() == nbInserts );
1316 int_uset.max_load_factor(0.5f);
1318 CPPUNIT_ASSERT( int_uset.load_factor() < int_uset.max_load_factor() );
1320 size_t bucketsHint = int_uset.bucket_count() + 1;
1321 int_uset.rehash(bucketsHint);
1322 CPPUNIT_ASSERT( int_uset.bucket_count() >= bucketsHint );
1324 CPPUNIT_ASSERT( int_uset.key_eq()(10, 10) );
1325 CPPUNIT_ASSERT( int_uset.hash_function()(10) == 10 );
1329 typedef unordered_multiset<char> Myset;
1335 Myset::const_iterator it = c1.begin();
1336 CPPUNIT_ASSERT(*it == 'a');
1338 Myset::const_iterator it1 = c1.end();
1344 void UnorderedTest::unordered_multiset_cov2()
1348 typedef unordered_multiset<char> Myset;
1356 CPPUNIT_ASSERT(c1.size() == 0 );
1359 typedef unordered_multiset<char> Myset;
1365 CPPUNIT_ASSERT( c1.erase('b') == 1);
1368 typedef unordered_multimap<char, int> Mymap;
1369 typedef allocator<std::pair<const char, int> > Myalloc;
1372 Mymap::allocator_type al = c1.get_allocator();
1373 CPPUNIT_ASSERT ((al == Myalloc()) == true);
1376 typedef unordered_multiset<char> Myset;
1382 Myset::iterator it2 = c1.insert(c1.begin(), 'd');
1383 CPPUNIT_ASSERT(c1.size() == 4 );
1385 c2.insert(c1.begin(),c1.end());
1386 CPPUNIT_ASSERT(c2.size() == 4 );
1390 void UnorderedTest::unordered_multiset_cov3()
1394 typedef unordered_multiset<char> Myset;
1407 Myset::const_iterator it1 = c1.begin();
1408 Myset::const_iterator it2 = c2.begin();
1409 CPPUNIT_ASSERT( *it1 == 'd' );
1410 CPPUNIT_ASSERT( *it2 == 'a' );
1412 CPPUNIT_ASSERT( *it1 == 'e' );
1413 CPPUNIT_ASSERT( *it2 == 'b' );
1415 CPPUNIT_ASSERT( *it1 == 'f' );
1416 CPPUNIT_ASSERT( *it2 == 'c' );
1419 typedef unordered_multiset<char> Myset;
1432 Myset::const_iterator it1 = c1.begin();
1433 Myset::const_iterator it2 = c2.begin();
1434 CPPUNIT_ASSERT( *it1 == 'd' );
1435 CPPUNIT_ASSERT( *it2 == 'a' );
1437 CPPUNIT_ASSERT( *it1 == 'e' );
1438 CPPUNIT_ASSERT( *it2 == 'b' );
1440 CPPUNIT_ASSERT( *it1 == 'f' );
1441 CPPUNIT_ASSERT( *it2 == 'c' );
1445 void UnorderedTest::unordered_multiset_cov4()
1449 typedef unordered_multiset<char> Myset;
1456 CPPUNIT_ASSERT(c2.size() == 3 );
1459 typedef unordered_multiset<char> Myset;
1465 Myset c3(c1.begin(),c1.end(),8,hash<char>(),equal_to<char>(),allocator<pair<const char, int> >());
1467 Myset::iterator it1 = c3.begin();
1468 CPPUNIT_ASSERT( *it1 == 'a');
1470 CPPUNIT_ASSERT( *it1 == 'b');
1472 CPPUNIT_ASSERT( *it1 == 'c');