static void CheckTable( const MyTable& x, int n ) { ASSERT( x.size()==size_t(n), "table is different size than expected" ); ASSERT( x.empty()==(n==0), NULL ); ASSERT( x.size()<=x.max_size(), NULL ); for( int i=1; i<=n; ++i ) { MyKey key( MyKey::make(-i) ); typename MyTable::const_accessor a; bool b = x.find(a,key); ASSERT( b, NULL ); ASSERT( a->second.value_of()==i*i, NULL ); } int count = 0; int key_sum = 0; for( typename MyTable::const_iterator i(x.begin()); i!=x.end(); ++i ) { ++count; key_sum += -i->first.value_of(); } ASSERT( count==n, NULL ); ASSERT( key_sum==n*(n+1)/2, NULL ); }
//! Test travering the tabel with a parallel range void ParallelTraverseTable( MyTable& table, size_t n, size_t expected_size ) { REMARK("testing parallel traversal\n"); ASSERT( table.size()==expected_size, NULL ); AtomicByte* array = new AtomicByte[n]; memset( array, 0, n*sizeof(AtomicByte) ); MyTable::range_type r = table.range(10); tbb::parallel_for( r, ParallelTraverseBody<MyTable::range_type>( array, n )); Check( array, n, expected_size ); const MyTable& const_table = table; memset( array, 0, n*sizeof(AtomicByte) ); MyTable::const_range_type cr = const_table.range(10); tbb::parallel_for( cr, ParallelTraverseBody<MyTable::const_range_type>( array, n )); Check( array, n, expected_size ); delete[] array; }
void TestRehash() { REMARK("testing rehashing\n"); MyTable w; w.insert( std::make_pair(MyKey::make(-5), MyData()) ); w.rehash(); // without this, assertion will fail MyTable::iterator it = w.begin(); int i = 0; // check for non-rehashed buckets for( ; it != w.end(); i++ ) w.count( (it++)->first ); ASSERT( i == 1, NULL ); for( i=0; i<1000; i=(i<29 ? i+1 : i*2) ) { for( int j=max(256+i, i*2); j<10000; j*=3 ) { MyTable v; FillTable( v, i ); ASSERT(int(v.size()) == i, NULL); ASSERT(int(v.bucket_count()) <= j, NULL); v.rehash( j ); ASSERT(int(v.bucket_count()) >= j, NULL); CheckTable( v, i ); } } }
//! Test traversing the table with an iterator. void TraverseTable( MyTable& table, size_t n, size_t expected_size ) { REMARK("testing traversal\n"); size_t actual_size = table.size(); ASSERT( actual_size==expected_size, NULL ); size_t count = 0; bool* array = new bool[n]; memset( array, 0, n*sizeof(bool) ); const MyTable& const_table = table; MyTable::const_iterator ci = const_table.begin(); for( MyTable::iterator i = table.begin(); i!=table.end(); ++i ) { // Check iterator int k = i->first.value_of(); ASSERT( UseKey(k), NULL ); ASSERT( (*i).first.value_of()==k, NULL ); ASSERT( 0<=k && size_t(k)<n, "out of bounds key" ); ASSERT( !array[k], "duplicate key" ); array[k] = true; ++count; // Check lower/upper bounds std::pair<MyTable::iterator, MyTable::iterator> er = table.equal_range(i->first); std::pair<MyTable::const_iterator, MyTable::const_iterator> cer = const_table.equal_range(i->first); ASSERT(cer.first == er.first && cer.second == er.second, NULL); ASSERT(cer.first == i, NULL); ASSERT(std::distance(cer.first, cer.second) == 1, NULL); // Check const_iterator MyTable::const_iterator cic = ci++; ASSERT( cic->first.value_of()==k, NULL ); ASSERT( (*cic).first.value_of()==k, NULL ); } ASSERT( ci==const_table.end(), NULL ); delete[] array; if( count!=expected_size ) { REPORT("Line %d: count=%ld but should be %ld\n",__LINE__,long(count),long(expected_size)); } }