void TestResizeAndCopy() { typedef tbb::concurrent_vector<Foo> vector_t; for( int old_size=0; old_size<=128; NextSize( old_size ) ) { for( int new_size=old_size; new_size<=128; NextSize( new_size ) ) { long count = FooCount; vector_t v; ASSERT( count==FooCount, NULL ); v.grow_by(old_size); ASSERT( count+old_size==FooCount, NULL ); for( int j=0; j<old_size; ++j ) v[j].bar() = j*j; v.grow_to_at_least(new_size); ASSERT( count+new_size==FooCount, NULL ); for( int j=0; j<new_size; ++j ) { int expected = j<old_size ? j*j : Foo::initial_value_of_bar; if( v[j].bar()!=expected ) std::printf("ERROR on line %d for old_size=%ld new_size=%ld v[%ld].bar()=%d != %d\n",__LINE__,long(old_size),long(new_size),long(j),v[j].bar(), expected); } ASSERT( v.size()==size_t(new_size), NULL ); for( int j=0; j<new_size; ++j ) { v[j].bar() = ~j; } const vector_t& cv = v; // Try copy constructor vector_t copy_of_v(cv); CheckVector(cv,new_size,old_size); v.clear(); ASSERT( v.empty(), NULL ); CheckVector(copy_of_v,new_size,old_size); } } }
//! Test the assignment operator void TestAssign() { typedef tbb::concurrent_vector<FooWithAssign> vector_t; for( int dst_size=1; dst_size<=128; NextSize( dst_size ) ) { for( int src_size=2; src_size<=128; NextSize( src_size ) ) { vector_t u; u.grow_to_at_least(src_size); for( int i=0; i<src_size; ++i ) u[i].bar() = i*i; vector_t v; v.grow_to_at_least(dst_size); for( int i=0; i<dst_size; ++i ) v[i].bar() = -i; v = u; u.clear(); ASSERT( u.size()==0, NULL ); ASSERT( v.size()==size_t(src_size), NULL ); for( int i=0; i<src_size; ++i ) ASSERT( v[i].bar()==(i*i), NULL ); } } }
//! Test of assign, grow, copying with various sizes void TestResizeAndCopy() { typedef static_counting_allocator<debug_allocator<Foo,std::allocator>, std::size_t> allocator_t; typedef tbb::concurrent_vector<Foo, allocator_t> vector_t; allocator_t::init_counters(); for( int old_size=0; old_size<=128; NextSize( old_size ) ) { for( int new_size=0; new_size<=1280; NextSize( new_size ) ) { long count = FooCount; vector_t v; ASSERT( count==FooCount, NULL ); v.assign(old_size/2, Foo() ); ASSERT( count+old_size/2==FooCount, NULL ); for( int j=0; j<old_size/2; ++j ) ASSERT( v[j].state == Foo::CopyInitialized, NULL); v.assign(FooIterator(0), FooIterator(old_size)); v.resize(new_size, Foo(33) ); ASSERT( count+new_size==FooCount, NULL ); for( int j=0; j<new_size; ++j ) { int expected = j<old_size ? j : 33; if( v[j].bar()!=expected ) REPORT("ERROR on line %d for old_size=%ld new_size=%ld v[%ld].bar()=%d != %d\n",__LINE__,long(old_size),long(new_size),long(j),v[j].bar(), expected); } ASSERT( v.size()==size_t(new_size), NULL ); for( int j=0; j<new_size; ++j ) { v[j].bar() = ~j; } const vector_t& cv = v; // Try copy constructor vector_t copy_of_v(cv); CheckVector(cv,new_size,old_size); ASSERT( !(v != copy_of_v), NULL ); v.clear(); ASSERT( v.empty(), NULL ); swap(v, copy_of_v); ASSERT( copy_of_v.empty(), NULL ); CheckVector(v,new_size,old_size); } } ASSERT( allocator_t::items_allocated == allocator_t::items_freed, NULL); ASSERT( allocator_t::allocations == allocator_t::frees, NULL); }
//! Test the assignment operator and swap void TestAssign() { typedef tbb::concurrent_vector<FooWithAssign, local_counting_allocator<std::allocator<FooWithAssign>, size_t > > vector_t; local_counting_allocator<std::allocator<FooWithAssign>, size_t > init_alloc; init_alloc.allocations = 100; for( int dst_size=1; dst_size<=128; NextSize( dst_size ) ) { for( int src_size=2; src_size<=128; NextSize( src_size ) ) { vector_t u(FooIterator(0), FooIterator(src_size), init_alloc); for( int i=0; i<src_size; ++i ) ASSERT( u[i].bar()==i, NULL ); vector_t v(dst_size, FooWithAssign(), init_alloc); for( int i=0; i<dst_size; ++i ) { ASSERT( v[i].state==Foo::CopyInitialized, NULL ); v[i].bar() = ~i; } ASSERT( v != u, NULL); v.swap(u); CheckVector(u, dst_size, src_size); u.swap(v); // using assignment v = u; ASSERT( v == u, NULL); u.clear(); ASSERT( u.size()==0, NULL ); ASSERT( v.size()==size_t(src_size), NULL ); for( int i=0; i<src_size; ++i ) ASSERT( v[i].bar()==i, NULL ); ASSERT( 0 == u.get_allocator().frees, NULL); u.shrink_to_fit(); // deallocate unused memory size_t items_allocated = u.get_allocator().items_allocated, items_freed = u.get_allocator().items_freed; size_t allocations = u.get_allocator().allocations, frees = u.get_allocator().frees + 100; ASSERT( items_allocated == items_freed, NULL); ASSERT( allocations == frees, NULL); } } }