1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/unordered_test.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,1475 @@
1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include <e32std.h>
1.20 +#include <vector>
1.21 +#include <algorithm>
1.22 +#include <string>
1.23 +#if defined (STLPORT)
1.24 +# include <unordered_map>
1.25 +# include <unordered_set>
1.26 +#endif
1.27 +
1.28 +//#include <iostream>
1.29 +
1.30 +#include "cppunit/cppunit_proxy.h"
1.31 +
1.32 +#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
1.33 +using namespace std;
1.34 +#endif
1.35 +
1.36 +//
1.37 +// TestCase class
1.38 +//
1.39 +class UnorderedTest : public CPPUNIT_NS::TestCase
1.40 +{
1.41 + CPPUNIT_TEST_SUITE(UnorderedTest);
1.42 +#if !defined (STLPORT)
1.43 + CPPUNIT_IGNORE;
1.44 +#endif
1.45 + CPPUNIT_TEST(uset);
1.46 + CPPUNIT_TEST(umultiset);
1.47 +#if defined (__DMC__)
1.48 + CPPUNIT_IGNORE;
1.49 +#endif
1.50 + CPPUNIT_TEST(umap);
1.51 + CPPUNIT_STOP_IGNORE;
1.52 + CPPUNIT_TEST(umultimap);
1.53 +#if defined (__DMC__)
1.54 + CPPUNIT_IGNORE;
1.55 +#endif
1.56 + CPPUNIT_TEST(user_case);
1.57 + CPPUNIT_STOP_IGNORE;
1.58 + CPPUNIT_TEST(hash_policy);
1.59 + CPPUNIT_TEST(buckets);
1.60 +#if defined (__DMC__)
1.61 + CPPUNIT_IGNORE;
1.62 +#endif
1.63 + CPPUNIT_TEST(equal_range);
1.64 +#if !defined (_STLP_USE_CONTAINERS_EXTENSION)
1.65 + CPPUNIT_IGNORE;
1.66 +#endif
1.67 + CPPUNIT_TEST(template_methods);
1.68 + CPPUNIT_TEST(unordered_set_cov1);
1.69 + CPPUNIT_TEST(unordered_set_cov2);
1.70 + CPPUNIT_TEST(unordered_set_cov3);
1.71 + CPPUNIT_TEST(unordered_map_cov1);
1.72 + CPPUNIT_TEST(unordered_map_cov2);
1.73 + CPPUNIT_TEST(unordered_map_cov3);
1.74 + CPPUNIT_TEST(unordered_map_cov4);
1.75 + CPPUNIT_TEST(unordered_map_cov5);
1.76 + CPPUNIT_TEST(unordered_multimap_cov1);
1.77 + CPPUNIT_TEST(unordered_multimap_cov2);
1.78 + CPPUNIT_TEST(unordered_multimap_cov3);
1.79 + CPPUNIT_TEST(unordered_multimap_cov4);
1.80 + CPPUNIT_TEST(unordered_multimap_cov5);
1.81 + CPPUNIT_TEST(unordered_multiset_cov1);
1.82 + CPPUNIT_TEST(unordered_multiset_cov2);
1.83 + CPPUNIT_TEST(unordered_multiset_cov3);
1.84 + CPPUNIT_TEST(unordered_multiset_cov4);
1.85 + CPPUNIT_TEST_SUITE_END();
1.86 +
1.87 +protected:
1.88 + void uset();
1.89 + void umultiset();
1.90 + void umap();
1.91 + void umultimap();
1.92 + void user_case();
1.93 + void hash_policy();
1.94 + void buckets();
1.95 + void equal_range();
1.96 + void template_methods();
1.97 + void unordered_set_cov1();
1.98 + void unordered_set_cov2();
1.99 + void unordered_set_cov3();
1.100 + void unordered_map_cov1();
1.101 + void unordered_map_cov2();
1.102 + void unordered_map_cov3();
1.103 + void unordered_map_cov4();
1.104 + void unordered_map_cov5();
1.105 + void unordered_multimap_cov1();
1.106 + void unordered_multimap_cov2();
1.107 + void unordered_multimap_cov3();
1.108 + void unordered_multimap_cov4();
1.109 + void unordered_multimap_cov5();
1.110 + void unordered_multiset_cov1();
1.111 + void unordered_multiset_cov2();
1.112 + void unordered_multiset_cov3();
1.113 + void unordered_multiset_cov4();
1.114 +};
1.115 +
1.116 +CPPUNIT_TEST_SUITE_REGISTRATION(UnorderedTest);
1.117 +
1.118 +const int NB_ELEMS = 2000;
1.119 +
1.120 +//
1.121 +// tests implementation
1.122 +//
1.123 +void UnorderedTest::uset()
1.124 +{
1.125 +#if defined (STLPORT)
1.126 + typedef unordered_set<int, hash<int>, equal_to<int> > usettype;
1.127 + usettype us;
1.128 +
1.129 + //Small compilation check of the copy constructor:
1.130 + usettype us2(us);
1.131 + //And assignment operator
1.132 + us = us2;
1.133 +
1.134 + int i;
1.135 + pair<usettype::iterator, bool> ret;
1.136 + for (i = 0; i < NB_ELEMS; ++i) {
1.137 + ret = us.insert(i);
1.138 + CPPUNIT_ASSERT( ret.second );
1.139 + CPPUNIT_ASSERT( *ret.first == i );
1.140 +
1.141 + ret = us.insert(i);
1.142 + CPPUNIT_ASSERT( !ret.second );
1.143 + CPPUNIT_ASSERT( *ret.first == i );
1.144 + }
1.145 +
1.146 + vector<int> us_val;
1.147 +
1.148 + usettype::local_iterator lit, litEnd;
1.149 + for (i = 0; i < NB_ELEMS; ++i) {
1.150 + lit = us.begin(us.bucket(i));
1.151 + litEnd = us.end(us.bucket(i));
1.152 +
1.153 + usettype::size_type bucket_pos = us.bucket(*lit);
1.154 + for (; lit != litEnd; ++lit) {
1.155 + CPPUNIT_ASSERT( us.bucket(*lit) == bucket_pos );
1.156 + us_val.push_back(*lit);
1.157 + }
1.158 + }
1.159 +
1.160 + //A compilation time check to uncomment from time to time
1.161 + {
1.162 + //usettype::iterator it;
1.163 + //CPPUNIT_ASSERT( it != lit );
1.164 + }
1.165 +
1.166 + sort(us_val.begin(), us_val.end());
1.167 + for (i = 0; i < NB_ELEMS; ++i) {
1.168 + CPPUNIT_ASSERT( us_val[i] == i );
1.169 + }
1.170 +#endif
1.171 +}
1.172 +
1.173 +void UnorderedTest::umultiset()
1.174 +{
1.175 +#if defined (STLPORT)
1.176 + typedef unordered_multiset<int, hash<int>, equal_to<int> > usettype;
1.177 + usettype us;
1.178 +
1.179 + int i;
1.180 + usettype::iterator ret;
1.181 + for (i = 0; i < NB_ELEMS; ++i) {
1.182 + ret = us.insert(i);
1.183 + CPPUNIT_ASSERT( *ret == i );
1.184 +
1.185 + ret = us.insert(i);
1.186 + CPPUNIT_ASSERT( *ret == i );
1.187 + }
1.188 +
1.189 + CPPUNIT_ASSERT( us.size() == 2 * NB_ELEMS );
1.190 + vector<int> us_val;
1.191 +
1.192 + usettype::local_iterator lit, litEnd;
1.193 + for (i = 0; i < NB_ELEMS; ++i) {
1.194 + lit = us.begin(us.bucket(i));
1.195 + litEnd = us.end(us.bucket(i));
1.196 +
1.197 + usettype::size_type bucket_pos = us.bucket(*lit);
1.198 + for (; lit != litEnd; ++lit) {
1.199 + CPPUNIT_ASSERT( us.bucket(*lit) == bucket_pos );
1.200 + us_val.push_back(*lit);
1.201 + }
1.202 + }
1.203 +
1.204 + sort(us_val.begin(), us_val.end());
1.205 + for (i = 0; i < NB_ELEMS; ++i) {
1.206 + CPPUNIT_ASSERT( us_val[2 * i] == i );
1.207 + CPPUNIT_ASSERT( us_val[2 * i + 1] == i );
1.208 + }
1.209 +#endif
1.210 +}
1.211 +
1.212 +void UnorderedTest::umap()
1.213 +{
1.214 +#if defined (STLPORT) && !defined (__DMC__)
1.215 + typedef unordered_map<int, int, hash<int>, equal_to<int> > umaptype;
1.216 + umaptype us;
1.217 +
1.218 + //Compilation check of the [] operator:
1.219 + umaptype us2;
1.220 + us[0] = us2[0];
1.221 + us.clear();
1.222 +
1.223 + {
1.224 + //An other compilation check
1.225 + typedef unordered_map<int, umaptype> uumaptype;
1.226 + uumaptype uus;
1.227 + umaptype const& uref = uus[0];
1.228 + umaptype ucopy = uus[0];
1.229 + ucopy = uref;
1.230 + //Avoids warning:
1.231 + //(void*)&uref;
1.232 + }
1.233 +
1.234 + int i;
1.235 + pair<umaptype::iterator, bool> ret;
1.236 + for (i = 0; i < NB_ELEMS; ++i) {
1.237 + umaptype::value_type p1(i, i);
1.238 + ret = us.insert(p1);
1.239 + CPPUNIT_ASSERT( ret.second );
1.240 + CPPUNIT_ASSERT( *ret.first == p1 );
1.241 +
1.242 + umaptype::value_type p2(i, i + 1);
1.243 + ret = us.insert(p2);
1.244 + CPPUNIT_ASSERT( !ret.second );
1.245 + CPPUNIT_ASSERT( *ret.first == p1 );
1.246 + }
1.247 +
1.248 + {
1.249 + //Lets look for some values to see if everything is normal:
1.250 + umaptype::iterator umit;
1.251 + for (int j = 0; j < NB_ELEMS; j += NB_ELEMS / 100) {
1.252 + umit = us.find(j);
1.253 +
1.254 + CPPUNIT_ASSERT( umit != us.end() );
1.255 + CPPUNIT_ASSERT( (*umit).second == j );
1.256 + }
1.257 + }
1.258 +
1.259 + CPPUNIT_ASSERT( us.size() == (size_t)NB_ELEMS );
1.260 + vector<pair<int, int> > us_val;
1.261 +
1.262 + umaptype::local_iterator lit, litEnd;
1.263 + for (i = 0; i < NB_ELEMS; ++i) {
1.264 + lit = us.begin(us.bucket(i));
1.265 + litEnd = us.end(us.bucket(i));
1.266 +
1.267 + umaptype::size_type bucket_pos = us.bucket((*lit).first);
1.268 + for (; lit != litEnd; ++lit) {
1.269 + CPPUNIT_ASSERT( us.bucket((*lit).first) == bucket_pos );
1.270 + us_val.push_back(make_pair((*lit).first, (*lit).second));
1.271 + }
1.272 + }
1.273 +
1.274 + sort(us_val.begin(), us_val.end());
1.275 + for (i = 0; i < NB_ELEMS; ++i) {
1.276 + CPPUNIT_ASSERT( us_val[i] == make_pair(i, i) );
1.277 + }
1.278 +#endif
1.279 +}
1.280 +
1.281 +void UnorderedTest::umultimap()
1.282 +{
1.283 +#if defined (STLPORT)
1.284 + typedef unordered_multimap<int, int, hash<int>, equal_to<int> > umaptype;
1.285 + umaptype us;
1.286 +
1.287 + int i;
1.288 + umaptype::iterator ret;
1.289 + for (i = 0; i < NB_ELEMS; ++i) {
1.290 + umaptype::value_type p(i, i);
1.291 + ret = us.insert(p);
1.292 + CPPUNIT_ASSERT( *ret == p );
1.293 +
1.294 + ret = us.insert(p);
1.295 + CPPUNIT_ASSERT( *ret == p );
1.296 + }
1.297 +
1.298 + CPPUNIT_ASSERT( us.size() == 2 * NB_ELEMS );
1.299 + typedef pair<int, int> ptype;
1.300 + vector<ptype> us_val;
1.301 +
1.302 + umaptype::local_iterator lit, litEnd;
1.303 + for (i = 0; i < NB_ELEMS; ++i) {
1.304 + lit = us.begin(us.bucket(i));
1.305 + litEnd = us.end(us.bucket(i));
1.306 +
1.307 + umaptype::size_type bucket_pos = us.bucket((*lit).first);
1.308 + for (; lit != litEnd; ++lit) {
1.309 + CPPUNIT_ASSERT( us.bucket((*lit).first) == bucket_pos );
1.310 + us_val.push_back(ptype((*lit).first, (*lit).second));
1.311 + }
1.312 + }
1.313 +
1.314 + sort(us_val.begin(), us_val.end());
1.315 + for (i = 0; i < NB_ELEMS; ++i) {
1.316 + ptype p(i, i);
1.317 + CPPUNIT_ASSERT( us_val[i * 2] == p );
1.318 + CPPUNIT_ASSERT( us_val[i * 2 + 1] == p );
1.319 + }
1.320 +#endif
1.321 +}
1.322 +
1.323 +void UnorderedTest::user_case()
1.324 +{
1.325 +#if defined (STLPORT) && !defined (__DMC__)
1.326 + typedef unordered_map<int, string> UnorderedMap1;
1.327 + typedef unordered_map<int, UnorderedMap1> UnorderedMap2;
1.328 +
1.329 + UnorderedMap1 foo;
1.330 + UnorderedMap2 bar;
1.331 +
1.332 + foo.insert(UnorderedMap1::value_type(1, string("test1")));
1.333 + foo.insert(UnorderedMap1::value_type(2, string("test2")));
1.334 + foo.insert(UnorderedMap1::value_type(3, string("test3")));
1.335 + foo.insert(UnorderedMap1::value_type(4, string("test4")));
1.336 + foo.insert(UnorderedMap1::value_type(5, string("test5")));
1.337 +
1.338 + bar.insert(UnorderedMap2::value_type(0, foo));
1.339 + UnorderedMap2::iterator it = bar.find(0);
1.340 + CPPUNIT_ASSERT( it != bar.end() );
1.341 +
1.342 + UnorderedMap1 &body = it->second;
1.343 + UnorderedMap1::iterator cur = body.find(3);
1.344 + CPPUNIT_ASSERT( cur != body.end() );
1.345 +
1.346 + body.erase(body.begin(), body.end());
1.347 + CPPUNIT_ASSERT( body.empty() );
1.348 +#endif
1.349 +}
1.350 +
1.351 +void UnorderedTest::hash_policy()
1.352 +{
1.353 +#if defined (STLPORT)
1.354 + unordered_set<int> int_uset;
1.355 +
1.356 + CPPUNIT_ASSERT( int_uset.max_load_factor() == 1.0f );
1.357 + CPPUNIT_ASSERT( int_uset.load_factor() == 0.0f );
1.358 +
1.359 + size_t nbInserts = int_uset.bucket_count() - 1;
1.360 + for (int i = 0; (size_t)i < nbInserts; ++i) {
1.361 + int_uset.insert(i);
1.362 + }
1.363 + CPPUNIT_ASSERT( int_uset.size() == nbInserts );
1.364 +
1.365 + int_uset.max_load_factor(0.5f);
1.366 + int_uset.rehash(0);
1.367 + CPPUNIT_ASSERT( int_uset.load_factor() < int_uset.max_load_factor() );
1.368 +
1.369 + size_t bucketsHint = int_uset.bucket_count() + 1;
1.370 + int_uset.rehash(bucketsHint);
1.371 + CPPUNIT_ASSERT( int_uset.bucket_count() >= bucketsHint );
1.372 +
1.373 + CPPUNIT_ASSERT( int_uset.key_eq()(10, 10) );
1.374 + CPPUNIT_ASSERT( int_uset.hash_function()(10) == 10 );
1.375 +#endif
1.376 +}
1.377 +
1.378 +void UnorderedTest::buckets()
1.379 +{
1.380 +#if defined (STLPORT)
1.381 + unordered_set<int> int_uset;
1.382 +
1.383 + CPPUNIT_ASSERT( int_uset.bucket_count() < int_uset.max_bucket_count() );
1.384 +
1.385 + int i;
1.386 + size_t nbBuckets = int_uset.bucket_count();
1.387 + size_t nbInserts = int_uset.bucket_count() - 1;
1.388 + for (i = 0; (size_t)i < nbInserts; ++i) {
1.389 + int_uset.insert(i);
1.390 + }
1.391 + CPPUNIT_ASSERT( nbBuckets == int_uset.bucket_count() );
1.392 +
1.393 + size_t bucketSizes = 0;
1.394 + for (i = 0; (size_t)i < nbBuckets; ++i) {
1.395 + bucketSizes += int_uset.bucket_size(i);
1.396 + }
1.397 + CPPUNIT_ASSERT( bucketSizes == int_uset.size() );
1.398 +#endif
1.399 +}
1.400 +
1.401 +void UnorderedTest::equal_range()
1.402 +{
1.403 +#if defined (STLPORT) && !defined (__DMC__)
1.404 + typedef unordered_multiset<size_t> umset;
1.405 + {
1.406 + //General test
1.407 + umset iumset;
1.408 + iumset.max_load_factor(10.0f);
1.409 +
1.410 + size_t nbBuckets = iumset.bucket_count();
1.411 +
1.412 + for (size_t i = 0; i < nbBuckets; ++i) {
1.413 + iumset.insert(i);
1.414 + iumset.insert(i + nbBuckets);
1.415 + iumset.insert(i + 2 * nbBuckets);
1.416 + iumset.insert(i + 3 * nbBuckets);
1.417 + iumset.insert(i + 4 * nbBuckets);
1.418 + }
1.419 +
1.420 + CPPUNIT_ASSERT( nbBuckets == iumset.bucket_count() );
1.421 + CPPUNIT_ASSERT( iumset.size() == 5 * nbBuckets );
1.422 +
1.423 + pair<umset::iterator, umset::iterator> p = iumset.equal_range(1);
1.424 + CPPUNIT_ASSERT( p.first != p.second );
1.425 +
1.426 + size_t nbElems = iumset.size();
1.427 + nbElems -= distance(p.first, p.second);
1.428 + for (umset::iterator j = p.first; j != p.second;) {
1.429 + iumset.erase(j++);
1.430 + }
1.431 +
1.432 + CPPUNIT_ASSERT( nbElems == iumset.size() );
1.433 +
1.434 + p = iumset.equal_range(2);
1.435 + CPPUNIT_ASSERT( p.first != p.second );
1.436 + nbElems -= distance(p.first, p.second);
1.437 + iumset.erase(p.first, p.second);
1.438 + CPPUNIT_ASSERT( nbElems == iumset.size() );
1.439 + }
1.440 +
1.441 + {
1.442 + //More specific test that tries to put many values in the same bucket
1.443 + umset iumset;
1.444 +
1.445 + size_t i;
1.446 + const size_t nbBuckets = iumset.bucket_count();
1.447 + const size_t targetedBucket = nbBuckets / 2;
1.448 +
1.449 + //Lets put 10 values in the targeted bucket:
1.450 + for (i = 0; i < 10; ++i) {
1.451 + iumset.insert(targetedBucket + (i * nbBuckets));
1.452 + }
1.453 +
1.454 + //We put again 10 values in the targeted bucket and in reverse order:
1.455 + for (i = 9; i <= 10; --i) {
1.456 + iumset.insert(targetedBucket + (i * nbBuckets));
1.457 + }
1.458 +
1.459 + //Now we put some more elements until hash container is resized:
1.460 + i = 0;
1.461 + while (iumset.bucket_count() == nbBuckets) {
1.462 + iumset.insert(i++);
1.463 + }
1.464 +
1.465 + //CPPUNIT_ASSERT( iumset.bucket_size(targetedBucket) == 21 );
1.466 +
1.467 + pair<umset::iterator, umset::iterator> p = iumset.equal_range(targetedBucket);
1.468 + CPPUNIT_ASSERT( p.first != p.second );
1.469 + CPPUNIT_ASSERT( distance(p.first, p.second) == 3 );
1.470 + }
1.471 +
1.472 + {
1.473 + srand(0);
1.474 + for (int runs = 0; runs < 2; ++runs) {
1.475 + size_t magic = rand();
1.476 + umset hum;
1.477 + size_t c = 0;
1.478 + for (int i = 0; i < 10000; ++i) {
1.479 + if ((rand() % 500) == 0) {
1.480 + hum.insert(magic);
1.481 + ++c;
1.482 + }
1.483 + else {
1.484 + size_t r;
1.485 + while ((r = rand()) == magic);
1.486 + hum.insert(r);
1.487 + }
1.488 +
1.489 + /*
1.490 + if ((float)(hum.size() + 1) / (float)hum.bucket_count() > hum.max_load_factor()) {
1.491 + cout << "Hash container dump: Nb elems: " << hum.size() << ", Nb buckets: " << hum.bucket_count() << "\n";
1.492 + for (size_t b = 0; b < hum.bucket_count(); ++b) {
1.493 + if (hum.bucket_size(b) != 0) {
1.494 + umset::local_iterator litBegin(hum.begin(b)), litEnd(hum.end(b));
1.495 + cout << "B" << b << ": ";
1.496 + for (umset::local_iterator lit = litBegin; lit != litEnd; ++lit) {
1.497 + if (lit != litBegin) {
1.498 + cout << " - ";
1.499 + }
1.500 + cout << *lit;
1.501 + }
1.502 + cout << "\n";
1.503 + }
1.504 + }
1.505 + cout << endl;
1.506 + }
1.507 + */
1.508 + }
1.509 + CPPUNIT_ASSERT( hum.count(magic) == c );
1.510 + }
1.511 + }
1.512 +#endif
1.513 +}
1.514 +
1.515 +struct Key
1.516 +{
1.517 + Key() : m_data(0) {}
1.518 + explicit Key(int data) : m_data(data) {}
1.519 +
1.520 + int m_data;
1.521 +};
1.522 +
1.523 +struct KeyHash
1.524 +{
1.525 + size_t operator () (Key key) const
1.526 + { return (size_t)key.m_data; }
1.527 +
1.528 + size_t operator () (int data) const
1.529 + { return (size_t)data; }
1.530 +};
1.531 +
1.532 +struct KeyEqual
1.533 +{
1.534 + bool operator () (Key lhs, Key rhs) const
1.535 + { return lhs.m_data == rhs.m_data; }
1.536 +
1.537 + bool operator () (Key lhs, int rhs) const
1.538 + { return lhs.m_data == rhs; }
1.539 +
1.540 + bool operator () (int lhs, Key rhs) const
1.541 + { return lhs == rhs.m_data; }
1.542 +};
1.543 +
1.544 +struct KeyHashPtr
1.545 +{
1.546 + size_t operator () (Key const volatile *key) const
1.547 + { return (size_t)key->m_data; }
1.548 +
1.549 + size_t operator () (int data) const
1.550 + { return (size_t)data; }
1.551 +};
1.552 +
1.553 +struct KeyEqualPtr
1.554 +{
1.555 + bool operator () (Key const volatile *lhs, Key const volatile *rhs) const
1.556 + { return lhs->m_data == rhs->m_data; }
1.557 +
1.558 + bool operator () (Key const volatile *lhs, int rhs) const
1.559 + { return lhs->m_data == rhs; }
1.560 +
1.561 + bool operator () (int lhs, Key const volatile *rhs) const
1.562 + { return lhs == rhs->m_data; }
1.563 +};
1.564 +
1.565 +void UnorderedTest::template_methods()
1.566 +{
1.567 +#if defined (STLPORT) && defined (_STLP_USE_CONTAINERS_EXTENSION)
1.568 + {
1.569 + typedef unordered_set<Key, KeyHash, KeyEqual> Container;
1.570 + Container cont;
1.571 + cont.insert(Key(1));
1.572 + cont.insert(Key(2));
1.573 + cont.insert(Key(3));
1.574 + cont.insert(Key(4));
1.575 +
1.576 + CPPUNIT_ASSERT( cont.count(Key(1)) == 1 );
1.577 + CPPUNIT_ASSERT( cont.count(1) == 1 );
1.578 + CPPUNIT_ASSERT( cont.count(5) == 0 );
1.579 +
1.580 + CPPUNIT_ASSERT( cont.find(2) != cont.end() );
1.581 + CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) );
1.582 +
1.583 + Container const& ccont = cont;
1.584 + CPPUNIT_ASSERT( ccont.find(2) != ccont.end() );
1.585 + CPPUNIT_ASSERT( ccont.bucket(2) == ccont.bucket(2) );
1.586 + CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.begin(), ccont.end()) );
1.587 + }
1.588 +
1.589 + {
1.590 + typedef unordered_set<Key*, KeyHashPtr, KeyEqualPtr> Container;
1.591 + Container cont;
1.592 + Key key1(1), key2(2), key3(3), key4(4);
1.593 + cont.insert(&key1);
1.594 + cont.insert(&key2);
1.595 + cont.insert(&key3);
1.596 + cont.insert(&key4);
1.597 +
1.598 + CPPUNIT_ASSERT( cont.count(1) == 1 );
1.599 + CPPUNIT_ASSERT( cont.count(5) == 0 );
1.600 +
1.601 + CPPUNIT_ASSERT( cont.find(2) != cont.end() );
1.602 + CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) );
1.603 +
1.604 + Container const& ccont = cont;
1.605 + CPPUNIT_ASSERT( ccont.find(2) != ccont.end() );
1.606 + CPPUNIT_ASSERT( ccont.bucket(2) == ccont.bucket(2) );
1.607 + CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.begin(), ccont.end()) );
1.608 + }
1.609 + {
1.610 + typedef unordered_multiset<Key, KeyHash, KeyEqual> Container;
1.611 + Container cont;
1.612 + cont.insert(Key(1));
1.613 + cont.insert(Key(2));
1.614 + cont.insert(Key(1));
1.615 + cont.insert(Key(2));
1.616 +
1.617 + CPPUNIT_ASSERT( cont.count(Key(1)) == 2 );
1.618 + CPPUNIT_ASSERT( cont.count(1) == 2 );
1.619 + CPPUNIT_ASSERT( cont.count(5) == 0 );
1.620 +
1.621 + CPPUNIT_ASSERT( cont.find(2) != cont.end() );
1.622 + CPPUNIT_ASSERT( cont.equal_range(1) != make_pair(cont.end(), cont.end()) );
1.623 +
1.624 + Container const& ccont = cont;
1.625 + CPPUNIT_ASSERT( ccont.find(2) != ccont.end() );
1.626 + CPPUNIT_ASSERT( ccont.bucket(2) == ccont.bucket(2) );
1.627 + CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.end(), ccont.end()) );
1.628 + }
1.629 +
1.630 + {
1.631 + typedef unordered_multiset<Key const volatile*, KeyHashPtr, KeyEqualPtr> Container;
1.632 + Container cont;
1.633 + Key key1(1), key2(2), key3(3), key4(4);
1.634 + cont.insert(&key1);
1.635 + cont.insert(&key2);
1.636 + cont.insert(&key3);
1.637 + cont.insert(&key4);
1.638 +
1.639 + CPPUNIT_ASSERT( cont.count(1) == 1 );
1.640 + CPPUNIT_ASSERT( cont.count(5) == 0 );
1.641 +
1.642 + CPPUNIT_ASSERT( cont.find(2) != cont.end() );
1.643 + CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) );
1.644 +
1.645 + Container const& ccont = cont;
1.646 + CPPUNIT_ASSERT( ccont.find(2) != ccont.end() );
1.647 + CPPUNIT_ASSERT( ccont.bucket(2) == ccont.bucket(2) );
1.648 + CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.begin(), ccont.end()) );
1.649 + }
1.650 +#endif
1.651 +}
1.652 +void UnorderedTest::unordered_set_cov1()
1.653 + {
1.654 + __UHEAP_MARK;
1.655 + {
1.656 + typedef unordered_set<char> Myset;
1.657 + Myset c1,c2;
1.658 +
1.659 + c1.insert('a');
1.660 + c1.insert('b');
1.661 + c1.insert('c');
1.662 +
1.663 + c2.insert('d');
1.664 + c2.insert('e');
1.665 + c2.insert('f');
1.666 +
1.667 + c1.swap(c2);
1.668 + Myset::const_iterator it1 = c1.begin();
1.669 + Myset::const_iterator it2 = c2.begin();
1.670 + CPPUNIT_ASSERT( *it1 == 'd' );
1.671 + CPPUNIT_ASSERT( *it2 == 'a' );
1.672 + it1++;it2++;
1.673 + CPPUNIT_ASSERT( *it1 == 'e' );
1.674 + CPPUNIT_ASSERT( *it2 == 'b' );
1.675 + it1++;it2++;
1.676 + CPPUNIT_ASSERT( *it1 == 'f' );
1.677 + CPPUNIT_ASSERT( *it2 == 'c' );
1.678 + }
1.679 + {
1.680 + typedef unordered_set<char> Myset;
1.681 + Myset c1,c2;
1.682 +
1.683 + c1.insert('a');
1.684 + c1.insert('b');
1.685 + c1.insert('c');
1.686 +
1.687 + c2.insert('d');
1.688 + c2.insert('e');
1.689 + c2.insert('f');
1.690 +
1.691 + swap(c1,c2);
1.692 + Myset::const_iterator it1 = c1.begin();
1.693 + Myset::const_iterator it2 = c2.begin();
1.694 + CPPUNIT_ASSERT( *it1 == 'd' );
1.695 + CPPUNIT_ASSERT( *it2 == 'a' );
1.696 + it1++;it2++;
1.697 + CPPUNIT_ASSERT( *it1 == 'e' );
1.698 + CPPUNIT_ASSERT( *it2 == 'b' );
1.699 + it1++;it2++;
1.700 + CPPUNIT_ASSERT( *it1 == 'f' );
1.701 + CPPUNIT_ASSERT( *it2 == 'c' );
1.702 + }
1.703 + {
1.704 + typedef unordered_set<char> Myset;
1.705 + Myset c1;
1.706 +
1.707 + c1.max_size();
1.708 + }
1.709 + __UHEAP_MARKEND;
1.710 + }
1.711 +void UnorderedTest::unordered_set_cov2()
1.712 + {
1.713 + __UHEAP_MARK;
1.714 + {
1.715 + typedef unordered_set<char> Myset;
1.716 + Myset c1;
1.717 +
1.718 + c1.insert('a');
1.719 + c1.insert('b');
1.720 + c1.insert('c');
1.721 +
1.722 + c1.clear();
1.723 + CPPUNIT_ASSERT(c1.size() == 0 );
1.724 + CPPUNIT_ASSERT(c1.empty() );
1.725 + }
1.726 + {
1.727 + typedef unordered_set<char> Myset;
1.728 + Myset c1;
1.729 +
1.730 + c1.insert('a');
1.731 + c1.insert('b');
1.732 + c1.insert('c');
1.733 +
1.734 + Myset::const_iterator it = c1.end();
1.735 + //it--;
1.736 + //CPPUNIT_ASSERT(*it == 'a' );
1.737 +
1.738 + c1.max_size();
1.739 + }
1.740 + {
1.741 + typedef unordered_set<char> Myset;
1.742 + Myset c1;
1.743 +
1.744 + c1.insert('a');
1.745 + c1.insert('b');
1.746 + c1.insert('c');
1.747 +
1.748 + c1.erase(c1.begin());
1.749 + Myset::iterator it2 = c1.begin();
1.750 + CPPUNIT_ASSERT(*it2 == 'b' );
1.751 +
1.752 + c1.erase(c1.begin(), c1.end());
1.753 + CPPUNIT_ASSERT(c1.size() == 0 );
1.754 + }
1.755 + {
1.756 + typedef unordered_set<char> Myset;
1.757 + Myset c1;
1.758 +
1.759 + c1.insert('a');
1.760 + c1.insert('b');
1.761 + c1.insert('c');
1.762 + CPPUNIT_ASSERT( c1.erase('b') == 1);
1.763 + }
1.764 + __UHEAP_MARKEND;
1.765 + }
1.766 +void UnorderedTest::unordered_set_cov3()
1.767 + {
1.768 + __UHEAP_MARK;
1.769 + {
1.770 + typedef unordered_set<char> Mymap;
1.771 + typedef allocator<std::pair<const char, int> > Myalloc;
1.772 + Mymap c1;
1.773 +
1.774 + Mymap::allocator_type al = c1.get_allocator();
1.775 + CPPUNIT_ASSERT ((al == Myalloc()) == true);
1.776 +
1.777 + }
1.778 + __UHEAP_MARKEND;
1.779 + }
1.780 +
1.781 +void UnorderedTest::unordered_map_cov1()
1.782 + {
1.783 + __UHEAP_MARK;
1.784 + {
1.785 + typedef unordered_map<char, int> Mymap;
1.786 + Mymap c1;
1.787 +
1.788 + c1.insert(Mymap::value_type('a', 1));
1.789 + c1.insert(Mymap::value_type('b', 2));
1.790 + c1.insert(Mymap::value_type('c', 3));
1.791 +
1.792 + Mymap::const_iterator it = c1.begin();
1.793 + CPPUNIT_ASSERT( it->first == 'a');
1.794 + CPPUNIT_ASSERT( it->second == 1);
1.795 +
1.796 + Mymap::const_iterator it1 = c1.end();
1.797 +
1.798 + Mymap::const_local_iterator lit = c1.begin(c1.bucket('a'));
1.799 + CPPUNIT_ASSERT( lit->first == 'a');
1.800 + CPPUNIT_ASSERT( lit->second == 1);
1.801 +
1.802 + Mymap::const_local_iterator lit1 = c1.end(c1.bucket('a'));
1.803 + }
1.804 + {
1.805 + typedef unordered_map<char, int> Mymap;
1.806 + Mymap c1;
1.807 +
1.808 + c1.insert(Mymap::value_type('a', 1));
1.809 + c1.insert(Mymap::value_type('b', 2));
1.810 + c1.insert(Mymap::value_type('c', 3));
1.811 + c1.bucket_count();
1.812 +
1.813 + Mymap::size_type bs = c1.bucket('a');
1.814 + CPPUNIT_ASSERT( c1.bucket_size(bs) == 1);
1.815 +
1.816 + CPPUNIT_ASSERT( c1.count('A') == 0);
1.817 + CPPUNIT_ASSERT( c1.count('b') == 1);
1.818 + CPPUNIT_ASSERT( c1.count('C') == 0);
1.819 + }
1.820 + __UHEAP_MARKEND;
1.821 + }
1.822 +void UnorderedTest::unordered_map_cov2()
1.823 + {
1.824 + __UHEAP_MARK;
1.825 + {
1.826 + typedef unordered_map<char, int> Mymap;
1.827 + Mymap c1;
1.828 +
1.829 + c1.insert(Mymap::value_type('a', 1));
1.830 + c1.insert(Mymap::value_type('b', 2));
1.831 + c1.insert(Mymap::value_type('c', 3));
1.832 +
1.833 + std::pair<Mymap::iterator, Mymap::iterator> pair1 = c1.equal_range('b');
1.834 + CPPUNIT_ASSERT( pair1.first->first == 'b');
1.835 + CPPUNIT_ASSERT( pair1.first->second == 2);
1.836 +
1.837 + std::pair<Mymap::iterator, Mymap::const_iterator> pair2 = c1.equal_range('c');
1.838 + CPPUNIT_ASSERT( pair2.first->first == 'c');
1.839 + CPPUNIT_ASSERT( pair2.first->second == 3);
1.840 + }
1.841 + {
1.842 + typedef unordered_map<char, int> Mymap;
1.843 + Mymap c1;
1.844 +
1.845 + c1.insert(Mymap::value_type('a', 1));
1.846 + c1.insert(Mymap::value_type('b', 2));
1.847 + c1.insert(Mymap::value_type('c', 3));
1.848 +
1.849 + c1.erase(c1.begin());
1.850 + Mymap::iterator it2 = c1.begin();
1.851 + CPPUNIT_ASSERT( it2->first == 'b');
1.852 + CPPUNIT_ASSERT( it2->second == 2);
1.853 +
1.854 + CPPUNIT_ASSERT( c1.erase('b') == 1);
1.855 + }
1.856 + {
1.857 + typedef unordered_map<char, int> Mymap;
1.858 + Mymap c1;
1.859 +
1.860 + c1.insert(Mymap::value_type('a', 1));
1.861 + c1.insert(Mymap::value_type('b', 2));
1.862 + c1.insert(Mymap::value_type('c', 3));
1.863 +
1.864 + Mymap::iterator it = c1.find('b');
1.865 + CPPUNIT_ASSERT( it->first == 'b');
1.866 + CPPUNIT_ASSERT( it->second == 2);
1.867 +
1.868 + // negative test case where the element to find is not present in the
1.869 + // unordered_map.
1.870 + Mymap::iterator it1 = c1.find('d');
1.871 + Mymap::iterator it2 = c1.end();
1.872 + CPPUNIT_ASSERT( it1 == it2);
1.873 + }
1.874 + __UHEAP_MARKEND;
1.875 + }
1.876 +void UnorderedTest::unordered_map_cov3()
1.877 + {
1.878 + __UHEAP_MARK;
1.879 + {
1.880 + typedef unordered_map<char, int> Mymap;
1.881 + typedef allocator<std::pair<const char, int> > Myalloc;
1.882 + Mymap c1;
1.883 +
1.884 + Mymap::allocator_type al = c1.get_allocator();
1.885 + CPPUNIT_ASSERT ((al == Myalloc()) == true);
1.886 +
1.887 + Mymap::hasher hfn = c1.hash_function();
1.888 + hfn('a');// returns the hash function object
1.889 + }
1.890 + {
1.891 + typedef unordered_map<char, int> Mymap;
1.892 + Mymap c1,c2;
1.893 +
1.894 + c1.insert(Mymap::value_type('a', 1));
1.895 + c1.insert(Mymap::value_type('b', 2));
1.896 + c1.insert(Mymap::value_type('c', 3));
1.897 +
1.898 + Mymap::iterator it2;
1.899 +
1.900 + c2.insert(c1.begin(), c1.end());
1.901 + it2 = c2.begin();
1.902 + CPPUNIT_ASSERT( it2->first == 'a');
1.903 + CPPUNIT_ASSERT( it2->second == 1);
1.904 + }
1.905 + {
1.906 + typedef unordered_map<char, int> Mymap;
1.907 + Mymap c1;
1.908 + Mymap::key_equal cmpfn = c1.key_eq();
1.909 + CPPUNIT_ASSERT( cmpfn('a','a') == true);
1.910 + }
1.911 + __UHEAP_MARKEND;
1.912 + }
1.913 +void UnorderedTest::unordered_map_cov4()
1.914 + {
1.915 + __UHEAP_MARK;
1.916 + {
1.917 + typedef unordered_map<char, int> Mymap;
1.918 + Mymap c1,c2;
1.919 +
1.920 + c1.insert(Mymap::value_type('a', 1));
1.921 + c1.insert(Mymap::value_type('b', 2));
1.922 + c1.insert(Mymap::value_type('c', 3));
1.923 +
1.924 + c2.insert(Mymap::value_type('d', 4));
1.925 + c2.insert(Mymap::value_type('e', 5));
1.926 + c2.insert(Mymap::value_type('f', 6));
1.927 +
1.928 + c1.swap(c2);
1.929 +
1.930 + Mymap::iterator it1 = c1.begin();
1.931 + CPPUNIT_ASSERT( it1->first == 'd');
1.932 + CPPUNIT_ASSERT( it1->second == 4);
1.933 + it1++;
1.934 + CPPUNIT_ASSERT( it1->first == 'e');
1.935 + CPPUNIT_ASSERT( it1->second == 5);
1.936 + it1++;
1.937 + CPPUNIT_ASSERT( it1->first == 'f');
1.938 + CPPUNIT_ASSERT( it1->second == 6);
1.939 + }
1.940 + {
1.941 + typedef unordered_map<char, int> Mymap;
1.942 + Mymap c1,c2;
1.943 +
1.944 + c1.insert(Mymap::value_type('a', 1));
1.945 + c1.insert(Mymap::value_type('b', 2));
1.946 + c1.insert(Mymap::value_type('c', 3));
1.947 +
1.948 + c2.insert(Mymap::value_type('d', 4));
1.949 + c2.insert(Mymap::value_type('e', 5));
1.950 + c2.insert(Mymap::value_type('f', 6));
1.951 +
1.952 + swap(c1,c2);
1.953 +
1.954 + Mymap::iterator it1 = c1.begin();
1.955 + CPPUNIT_ASSERT( it1->first == 'd');
1.956 + CPPUNIT_ASSERT( it1->second == 4);
1.957 + it1++;
1.958 + CPPUNIT_ASSERT( it1->first == 'e');
1.959 + CPPUNIT_ASSERT( it1->second == 5);
1.960 + it1++;
1.961 + CPPUNIT_ASSERT( it1->first == 'f');
1.962 + CPPUNIT_ASSERT( it1->second == 6);
1.963 + }
1.964 + __UHEAP_MARKEND;
1.965 + }
1.966 +void UnorderedTest::unordered_map_cov5()
1.967 + {
1.968 + __UHEAP_MARK;
1.969 + {
1.970 + typedef unordered_map<char, int> Mymap;
1.971 + Mymap c1;
1.972 + float a,b;
1.973 + c1.insert(Mymap::value_type('a', 1));
1.974 + c1.insert(Mymap::value_type('b', 2));
1.975 + c1.insert(Mymap::value_type('c', 3));
1.976 + a = (float)( (float)c1.size()/(float)c1.bucket_count() ) ;
1.977 + b = (float)c1.load_factor();
1.978 + CPPUNIT_ASSERT( a == b );
1.979 +
1.980 + c1.max_load_factor(0.10f);
1.981 + CPPUNIT_ASSERT( c1.max_load_factor() == 0.1f );
1.982 +
1.983 + CPPUNIT_ASSERT( c1.max_bucket_count() >= c1.bucket_count() );
1.984 + c1.rehash(100);
1.985 + }
1.986 + {
1.987 + typedef unordered_map<char, int> Mymap;
1.988 + Mymap c1;
1.989 + c1.max_size();
1.990 + }
1.991 + {
1.992 + typedef unordered_map<char, int> Mymap;
1.993 + Mymap c1;
1.994 +
1.995 + c1.insert(Mymap::value_type('a', 1));
1.996 + c1.insert(Mymap::value_type('b', 2));
1.997 + c1.insert(Mymap::value_type('c', 3));
1.998 +
1.999 + Mymap c3(c1.begin(),c1.end(),8,hash<char>(),equal_to<char>(),allocator<std::pair<const char, int> >());
1.1000 + Mymap::iterator it1 = c3.begin();
1.1001 + CPPUNIT_ASSERT( it1->first == 'a');
1.1002 + CPPUNIT_ASSERT( it1->second == 1);
1.1003 + it1++;
1.1004 + CPPUNIT_ASSERT( it1->first == 'b');
1.1005 + CPPUNIT_ASSERT( it1->second == 2);
1.1006 + it1++;
1.1007 + CPPUNIT_ASSERT( it1->first == 'c');
1.1008 + CPPUNIT_ASSERT( it1->second == 3);
1.1009 +
1.1010 + }
1.1011 + __UHEAP_MARKEND;
1.1012 + }
1.1013 +
1.1014 +void UnorderedTest::unordered_multimap_cov1()
1.1015 + {
1.1016 + __UHEAP_MARK;
1.1017 + {
1.1018 + typedef unordered_multimap<char, int> Mymap;
1.1019 + Mymap c1;
1.1020 +
1.1021 + c1.insert(Mymap::value_type('a', 1));
1.1022 + c1.insert(Mymap::value_type('b', 2));
1.1023 + c1.insert(Mymap::value_type('c', 3));
1.1024 +
1.1025 + Mymap::const_iterator it = c1.begin();
1.1026 + Mymap::iterator it2 = c1.begin();
1.1027 +
1.1028 + CPPUNIT_ASSERT( it->first == it2->first);
1.1029 + CPPUNIT_ASSERT( it->second == it2->second);
1.1030 +
1.1031 + Mymap::const_local_iterator lit = c1.begin(c1.bucket('a'));
1.1032 + CPPUNIT_ASSERT( lit->first == 'a');
1.1033 + CPPUNIT_ASSERT( lit->second == 1);
1.1034 + }
1.1035 + {
1.1036 + typedef unordered_multimap<char, int> Mymap;
1.1037 + Mymap c1;
1.1038 +
1.1039 + c1.insert(Mymap::value_type('a', 1));
1.1040 + c1.insert(Mymap::value_type('b', 2));
1.1041 + c1.insert(Mymap::value_type('c', 3));
1.1042 + c1.bucket_count();
1.1043 +
1.1044 + Mymap::size_type bs = c1.bucket('a');
1.1045 + CPPUNIT_ASSERT( c1.bucket_size(bs) == 1);
1.1046 +
1.1047 + CPPUNIT_ASSERT( c1.count('A') == 0);
1.1048 + CPPUNIT_ASSERT( c1.count('b') == 1);
1.1049 + CPPUNIT_ASSERT( c1.count('C') == 0);
1.1050 + }
1.1051 + {
1.1052 + typedef unordered_multimap<char, int> Mymap;
1.1053 + Mymap c1;
1.1054 +
1.1055 + c1.insert(Mymap::value_type('a', 1));
1.1056 + c1.insert(Mymap::value_type('b', 2));
1.1057 + c1.insert(Mymap::value_type('c', 3));
1.1058 +
1.1059 + c1.clear();
1.1060 + CPPUNIT_ASSERT( c1.size() == 0);
1.1061 + CPPUNIT_ASSERT( c1.empty() == true);
1.1062 + }
1.1063 + __UHEAP_MARKEND;
1.1064 + }
1.1065 +void UnorderedTest::unordered_multimap_cov2()
1.1066 + {
1.1067 + __UHEAP_MARK;
1.1068 + {
1.1069 + typedef unordered_multimap<char, int> Mymap;
1.1070 + Mymap c1;
1.1071 +
1.1072 + c1.insert(Mymap::value_type('a', 1));
1.1073 +
1.1074 + Mymap::const_local_iterator lit = c1.end(c1.bucket('a'));
1.1075 + }
1.1076 + {
1.1077 + typedef unordered_multimap<char, int> Mymap;
1.1078 + Mymap c1;
1.1079 +
1.1080 + c1.insert(Mymap::value_type('a', 1));
1.1081 + c1.insert(Mymap::value_type('b', 2));
1.1082 + c1.insert(Mymap::value_type('c', 3));
1.1083 +
1.1084 + std::pair<Mymap::iterator, Mymap::iterator> pair1 = c1.equal_range('b');
1.1085 + CPPUNIT_ASSERT( pair1.first->first == 'b');
1.1086 + CPPUNIT_ASSERT( pair1.first->second == 2);
1.1087 +
1.1088 + std::pair<Mymap::iterator, Mymap::const_iterator> pair2 = c1.equal_range('c');
1.1089 + CPPUNIT_ASSERT( pair2.first->first == 'c');
1.1090 + CPPUNIT_ASSERT( pair2.first->second == 3);
1.1091 + }
1.1092 + {
1.1093 + typedef unordered_multimap<char, int> Mymap;
1.1094 + Mymap c1;
1.1095 +
1.1096 + c1.insert(Mymap::value_type('a', 1));
1.1097 + c1.insert(Mymap::value_type('b', 2));
1.1098 + c1.insert(Mymap::value_type('c', 3));
1.1099 +
1.1100 + c1.erase(c1.begin());
1.1101 + Mymap::iterator it2 = c1.begin();
1.1102 + CPPUNIT_ASSERT( it2->first == 'b');
1.1103 + CPPUNIT_ASSERT( it2->second == 2);
1.1104 +
1.1105 + CPPUNIT_ASSERT( c1.erase('b') == 1);
1.1106 +
1.1107 + c1.erase(c1.begin(), c1.end());
1.1108 + CPPUNIT_ASSERT( c1.empty() );
1.1109 + }
1.1110 + __UHEAP_MARKEND;
1.1111 + }
1.1112 +void UnorderedTest::unordered_multimap_cov3()
1.1113 + {
1.1114 + __UHEAP_MARK;
1.1115 + {
1.1116 + typedef unordered_map<char, int> Mymap;
1.1117 + Mymap c1;
1.1118 +
1.1119 + c1.insert(Mymap::value_type('a', 1));
1.1120 + c1.insert(Mymap::value_type('b', 2));
1.1121 + c1.insert(Mymap::value_type('c', 3));
1.1122 +
1.1123 + Mymap::iterator it = c1.find('b');
1.1124 + CPPUNIT_ASSERT( it->first == 'b');
1.1125 + CPPUNIT_ASSERT( it->second == 2);
1.1126 +
1.1127 + Mymap::const_iterator it1 = c1.find('c');
1.1128 + CPPUNIT_ASSERT( it1->first == 'c');
1.1129 + CPPUNIT_ASSERT( it1->second == 3);
1.1130 + }
1.1131 + {
1.1132 + typedef unordered_multimap<char, int> Mymap;
1.1133 + typedef allocator<std::pair<const char, int> > Myalloc;
1.1134 + Mymap c1;
1.1135 +
1.1136 + Mymap::allocator_type al = c1.get_allocator();
1.1137 + CPPUNIT_ASSERT ((al == Myalloc()) == true);
1.1138 +
1.1139 + Mymap::hasher hfn = c1.hash_function();
1.1140 + hfn('a');// returns the hash function object
1.1141 + }
1.1142 + {
1.1143 + typedef unordered_multimap<char, int> Mymap;
1.1144 + Mymap c1,c2;
1.1145 +
1.1146 + c1.insert(Mymap::value_type('a', 1));
1.1147 + c1.insert(Mymap::value_type('b', 2));
1.1148 + c1.insert(Mymap::value_type('c', 3));
1.1149 +
1.1150 + Mymap::iterator it2;
1.1151 +
1.1152 + c2.insert(c1.begin(), c1.end());
1.1153 + it2 = c2.begin();
1.1154 + CPPUNIT_ASSERT( it2->first == 'a');
1.1155 + CPPUNIT_ASSERT( it2->second == 1);
1.1156 + }
1.1157 + __UHEAP_MARKEND;
1.1158 + }
1.1159 +void UnorderedTest::unordered_multimap_cov4()
1.1160 + {
1.1161 + __UHEAP_MARK;
1.1162 + {
1.1163 + typedef unordered_multimap<char, int> Mymap;
1.1164 + Mymap c1;
1.1165 + Mymap::key_equal cmpfn = c1.key_eq();
1.1166 + CPPUNIT_ASSERT( cmpfn('a','a') == true);
1.1167 + }
1.1168 + {
1.1169 + typedef unordered_multimap<char, int> Mymap;
1.1170 + Mymap c1,c2;
1.1171 +
1.1172 + c1.insert(Mymap::value_type('a', 1));
1.1173 + c1.insert(Mymap::value_type('b', 2));
1.1174 + c1.insert(Mymap::value_type('c', 3));
1.1175 +
1.1176 + c2.insert(Mymap::value_type('d', 4));
1.1177 + c2.insert(Mymap::value_type('e', 5));
1.1178 + c2.insert(Mymap::value_type('f', 6));
1.1179 +
1.1180 + c1.swap(c2);
1.1181 +
1.1182 + Mymap::iterator it1 = c1.begin();
1.1183 + CPPUNIT_ASSERT( it1->first == 'd');
1.1184 + CPPUNIT_ASSERT( it1->second == 4);
1.1185 + it1++;
1.1186 + CPPUNIT_ASSERT( it1->first == 'e');
1.1187 + CPPUNIT_ASSERT( it1->second == 5);
1.1188 + it1++;
1.1189 + CPPUNIT_ASSERT( it1->first == 'f');
1.1190 + CPPUNIT_ASSERT( it1->second == 6);
1.1191 + }
1.1192 + {
1.1193 + typedef unordered_multimap<char, int> Mymap;
1.1194 + Mymap c1,c2;
1.1195 +
1.1196 + c1.insert(Mymap::value_type('a', 1));
1.1197 + c1.insert(Mymap::value_type('b', 2));
1.1198 + c1.insert(Mymap::value_type('c', 3));
1.1199 +
1.1200 + c2.insert(Mymap::value_type('d', 4));
1.1201 + c2.insert(Mymap::value_type('e', 5));
1.1202 + c2.insert(Mymap::value_type('f', 6));
1.1203 +
1.1204 + swap(c1,c2);
1.1205 +
1.1206 + Mymap::iterator it1 = c1.begin();
1.1207 + CPPUNIT_ASSERT( it1->first == 'd');
1.1208 + CPPUNIT_ASSERT( it1->second == 4);
1.1209 + it1++;
1.1210 + CPPUNIT_ASSERT( it1->first == 'e');
1.1211 + CPPUNIT_ASSERT( it1->second == 5);
1.1212 + it1++;
1.1213 + CPPUNIT_ASSERT( it1->first == 'f');
1.1214 + CPPUNIT_ASSERT( it1->second == 6);
1.1215 + }
1.1216 + {
1.1217 + typedef unordered_multimap<char, int> Mymap;
1.1218 + Mymap c1;
1.1219 + float a,b;
1.1220 +
1.1221 + c1.insert(Mymap::value_type('a', 1));
1.1222 + c1.insert(Mymap::value_type('b', 2));
1.1223 + c1.insert(Mymap::value_type('c', 3));
1.1224 +
1.1225 + a = (float)( (float)c1.size()/(float)c1.bucket_count() ) ;
1.1226 + b = (float)c1.load_factor();
1.1227 + CPPUNIT_ASSERT( a == b );
1.1228 +
1.1229 + c1.max_load_factor(0.10f);
1.1230 + CPPUNIT_ASSERT( c1.max_load_factor() == 0.1f );
1.1231 +
1.1232 + CPPUNIT_ASSERT( c1.max_bucket_count() >= c1.bucket_count() );
1.1233 + c1.rehash(100);
1.1234 + }
1.1235 + __UHEAP_MARKEND;
1.1236 + }
1.1237 +void UnorderedTest::unordered_multimap_cov5()
1.1238 + {
1.1239 + __UHEAP_MARK;
1.1240 + {
1.1241 + typedef unordered_multimap<char, int> Mymap;
1.1242 + Mymap c1;
1.1243 + c1.max_size();
1.1244 + }
1.1245 + {
1.1246 + typedef unordered_multimap<char, int> Mymap;
1.1247 + Mymap c1,c2;
1.1248 +
1.1249 + c1.insert(Mymap::value_type('a', 1));
1.1250 + c1.insert(Mymap::value_type('b', 2));
1.1251 + c1.insert(Mymap::value_type('c', 3));
1.1252 +
1.1253 + c2 = c1;
1.1254 + Mymap::iterator it2;
1.1255 +
1.1256 + it2 = c2.begin();
1.1257 + CPPUNIT_ASSERT( it2->first == 'a');
1.1258 + CPPUNIT_ASSERT( it2->second == 1);
1.1259 + }
1.1260 + {
1.1261 + typedef unordered_multimap<char, int> Mymap;
1.1262 + Mymap c1;
1.1263 +
1.1264 + c1.insert(Mymap::value_type('a', 1));
1.1265 + c1.insert(Mymap::value_type('b', 2));
1.1266 + c1.insert(Mymap::value_type('c', 3));
1.1267 +
1.1268 + Mymap c3(c1.begin(),c1.end(),8,hash<char>(),equal_to<char>(),allocator<std::pair<const char, int> >());
1.1269 + Mymap::iterator it1 = c3.begin();
1.1270 + CPPUNIT_ASSERT( it1->first == 'a');
1.1271 + CPPUNIT_ASSERT( it1->second == 1);
1.1272 + it1++;
1.1273 + CPPUNIT_ASSERT( it1->first == 'b');
1.1274 + CPPUNIT_ASSERT( it1->second == 2);
1.1275 + it1++;
1.1276 + CPPUNIT_ASSERT( it1->first == 'c');
1.1277 + CPPUNIT_ASSERT( it1->second == 3);
1.1278 +
1.1279 + }
1.1280 + __UHEAP_MARKEND;
1.1281 +}
1.1282 +void UnorderedTest::unordered_multiset_cov1()
1.1283 + {
1.1284 + __UHEAP_MARK;
1.1285 + {
1.1286 + #if defined (STLPORT)
1.1287 + unordered_multiset<int> int_uset;
1.1288 +
1.1289 + CPPUNIT_ASSERT( int_uset.bucket_count() < int_uset.max_bucket_count() );
1.1290 +
1.1291 + int i;
1.1292 + size_t nbBuckets = int_uset.bucket_count();
1.1293 + size_t nbInserts = int_uset.bucket_count() - 1;
1.1294 + for (i = 0; (size_t)i < nbInserts; ++i) {
1.1295 + int_uset.insert(i);
1.1296 + }
1.1297 + CPPUNIT_ASSERT( nbBuckets == int_uset.bucket_count() );
1.1298 +
1.1299 + size_t bucketSizes = 0;
1.1300 + for (i = 0; (size_t)i < nbBuckets; ++i) {
1.1301 + bucketSizes += int_uset.bucket_size(i);
1.1302 + }
1.1303 + CPPUNIT_ASSERT( bucketSizes == int_uset.size() );
1.1304 + #endif
1.1305 + }
1.1306 + {
1.1307 + #if defined (STLPORT)
1.1308 + unordered_multiset<int> int_uset;
1.1309 +
1.1310 + CPPUNIT_ASSERT( int_uset.max_load_factor() == 1.0f );
1.1311 + CPPUNIT_ASSERT( int_uset.load_factor() == 0.0f );
1.1312 +
1.1313 + size_t nbInserts = int_uset.bucket_count() - 1;
1.1314 + for (int i = 0; (size_t)i < nbInserts; ++i) {
1.1315 + int_uset.insert(i);
1.1316 + }
1.1317 + CPPUNIT_ASSERT( int_uset.size() == nbInserts );
1.1318 +
1.1319 + int_uset.max_load_factor(0.5f);
1.1320 + int_uset.rehash(0);
1.1321 + CPPUNIT_ASSERT( int_uset.load_factor() < int_uset.max_load_factor() );
1.1322 +
1.1323 + size_t bucketsHint = int_uset.bucket_count() + 1;
1.1324 + int_uset.rehash(bucketsHint);
1.1325 + CPPUNIT_ASSERT( int_uset.bucket_count() >= bucketsHint );
1.1326 +
1.1327 + CPPUNIT_ASSERT( int_uset.key_eq()(10, 10) );
1.1328 + CPPUNIT_ASSERT( int_uset.hash_function()(10) == 10 );
1.1329 + #endif
1.1330 + }
1.1331 + {
1.1332 + typedef unordered_multiset<char> Myset;
1.1333 + Myset c1;
1.1334 +
1.1335 + c1.insert('a');
1.1336 + c1.insert('b');
1.1337 + c1.insert('c');
1.1338 + Myset::const_iterator it = c1.begin();
1.1339 + CPPUNIT_ASSERT(*it == 'a');
1.1340 +
1.1341 + Myset::const_iterator it1 = c1.end();
1.1342 +
1.1343 + c1.max_size();
1.1344 + }
1.1345 + __UHEAP_MARKEND;
1.1346 + }
1.1347 +void UnorderedTest::unordered_multiset_cov2()
1.1348 + {
1.1349 + __UHEAP_MARK;
1.1350 + {
1.1351 + typedef unordered_multiset<char> Myset;
1.1352 + Myset c1;
1.1353 +
1.1354 + c1.insert('a');
1.1355 + c1.insert('b');
1.1356 + c1.insert('c');
1.1357 +
1.1358 + c1.clear();
1.1359 + CPPUNIT_ASSERT(c1.size() == 0 );
1.1360 + }
1.1361 + {
1.1362 + typedef unordered_multiset<char> Myset;
1.1363 + Myset c1;
1.1364 +
1.1365 + c1.insert('a');
1.1366 + c1.insert('b');
1.1367 + c1.insert('c');
1.1368 + CPPUNIT_ASSERT( c1.erase('b') == 1);
1.1369 + }
1.1370 + {
1.1371 + typedef unordered_multimap<char, int> Mymap;
1.1372 + typedef allocator<std::pair<const char, int> > Myalloc;
1.1373 + Mymap c1;
1.1374 +
1.1375 + Mymap::allocator_type al = c1.get_allocator();
1.1376 + CPPUNIT_ASSERT ((al == Myalloc()) == true);
1.1377 + }
1.1378 + {
1.1379 + typedef unordered_multiset<char> Myset;
1.1380 + Myset c1,c2;
1.1381 +
1.1382 + c1.insert('a');
1.1383 + c1.insert('b');
1.1384 + c1.insert('c');
1.1385 + Myset::iterator it2 = c1.insert(c1.begin(), 'd');
1.1386 + CPPUNIT_ASSERT(c1.size() == 4 );
1.1387 +
1.1388 + c2.insert(c1.begin(),c1.end());
1.1389 + CPPUNIT_ASSERT(c2.size() == 4 );
1.1390 + }
1.1391 + __UHEAP_MARKEND;
1.1392 + }
1.1393 +void UnorderedTest::unordered_multiset_cov3()
1.1394 + {
1.1395 + __UHEAP_MARK;
1.1396 + {
1.1397 + typedef unordered_multiset<char> Myset;
1.1398 + Myset c1,c2;
1.1399 +
1.1400 +
1.1401 + c1.insert('a');
1.1402 + c1.insert('b');
1.1403 + c1.insert('c');
1.1404 +
1.1405 + c2.insert('d');
1.1406 + c2.insert('e');
1.1407 + c2.insert('f');
1.1408 +
1.1409 + c1.swap(c2);
1.1410 + Myset::const_iterator it1 = c1.begin();
1.1411 + Myset::const_iterator it2 = c2.begin();
1.1412 + CPPUNIT_ASSERT( *it1 == 'd' );
1.1413 + CPPUNIT_ASSERT( *it2 == 'a' );
1.1414 + it1++;it2++;
1.1415 + CPPUNIT_ASSERT( *it1 == 'e' );
1.1416 + CPPUNIT_ASSERT( *it2 == 'b' );
1.1417 + it1++;it2++;
1.1418 + CPPUNIT_ASSERT( *it1 == 'f' );
1.1419 + CPPUNIT_ASSERT( *it2 == 'c' );
1.1420 + }
1.1421 + {
1.1422 + typedef unordered_multiset<char> Myset;
1.1423 + Myset c1,c2;
1.1424 +
1.1425 +
1.1426 + c1.insert('a');
1.1427 + c1.insert('b');
1.1428 + c1.insert('c');
1.1429 +
1.1430 + c2.insert('d');
1.1431 + c2.insert('e');
1.1432 + c2.insert('f');
1.1433 +
1.1434 + swap(c1,c2);
1.1435 + Myset::const_iterator it1 = c1.begin();
1.1436 + Myset::const_iterator it2 = c2.begin();
1.1437 + CPPUNIT_ASSERT( *it1 == 'd' );
1.1438 + CPPUNIT_ASSERT( *it2 == 'a' );
1.1439 + it1++;it2++;
1.1440 + CPPUNIT_ASSERT( *it1 == 'e' );
1.1441 + CPPUNIT_ASSERT( *it2 == 'b' );
1.1442 + it1++;it2++;
1.1443 + CPPUNIT_ASSERT( *it1 == 'f' );
1.1444 + CPPUNIT_ASSERT( *it2 == 'c' );
1.1445 + }
1.1446 + __UHEAP_MARKEND;
1.1447 + }
1.1448 +void UnorderedTest::unordered_multiset_cov4()
1.1449 + {
1.1450 + __UHEAP_MARK;
1.1451 + {
1.1452 + typedef unordered_multiset<char> Myset;
1.1453 + Myset c1,c2;
1.1454 + c1.insert('a');
1.1455 + c1.insert('b');
1.1456 + c1.insert('c');
1.1457 +
1.1458 + c2 = c1;
1.1459 + CPPUNIT_ASSERT(c2.size() == 3 );
1.1460 + }
1.1461 + {
1.1462 + typedef unordered_multiset<char> Myset;
1.1463 + Myset c1;
1.1464 +
1.1465 + c1.insert('a');
1.1466 + c1.insert('b');
1.1467 + c1.insert('c');
1.1468 + Myset c3(c1.begin(),c1.end(),8,hash<char>(),equal_to<char>(),allocator<pair<const char, int> >());
1.1469 +
1.1470 + Myset::iterator it1 = c3.begin();
1.1471 + CPPUNIT_ASSERT( *it1 == 'a');
1.1472 + it1++;
1.1473 + CPPUNIT_ASSERT( *it1 == 'b');
1.1474 + it1++;
1.1475 + CPPUNIT_ASSERT( *it1 == 'c');
1.1476 + }
1.1477 + __UHEAP_MARKEND;
1.1478 +}