map02.cpp
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C++ 代码 · 共 494 行 · 第 1/2 页
CPP
494 行
}
/* ------------------------------------------------------------------
* allocator_test
* test stateful allocators and exception handling
*/
bool allocator_test( )
{
typedef std::multimap< int, int, std::less<int>, LowMemAllocator<int> > map_t;
LowMemAllocator<int> mem(200);
mem.SetTripOnAlloc();
map_t m( map_t::key_compare(), mem );
bool thrown = false;
//LowMemAllocator is set to trip after 200 allocations or 100 inserts
//(each unique key gets 2 allocations in multimap/set)
try{
for( int i=0; i<101; i++ ){
m.insert( map_t::value_type(i,-i) );
}
}catch( std::bad_alloc const & ){
mem = m.get_allocator();
if( mem.GetNumAllocs() != 201 ) FAIL //should have failed on 201st
if( INSANE(m) || m.size() != 100 ) FAIL
thrown = true;
}
if( !thrown ) FAIL //exception should have been thrown
m.clear();
mem.Reset(200);
mem.SetTripOnConstruct();
thrown = false;
//LowMemAllocator is set to trip after 100 allocations
try{
for( int i=0; i<101; i++ ){
m.insert( map_t::value_type(i,-i) );
}
}catch( std::bad_alloc const & ){
mem = m.get_allocator();
if( mem.GetNumConstructs() != 201 ) FAIL
//should have cleaned up last extra alloc
if( mem.GetNumAllocs() != 201 || mem.GetNumDeallocs() != 1 ) FAIL
if( INSANE(m) || m.size() != 100 ) FAIL
thrown = true;
}
if( !thrown ) FAIL //exception should have been thrown
//if container didn't deal with the exception and clean up the allocated
//memory then the leak detector will also trip later
m.clear();
mem.Reset(140);
mem.SetTripOnAlloc();
thrown = false;
for( int i = 0; i < 70; i++ ){
m.insert( map_t::value_type(i,-i) );
}
//now reset the allocator so it trips at a lower threshold
//and test the copy mechanism works right
mem.Reset( 50 );
mem.SetTripOnAlloc();
try{
map_t m2( m );
}catch( std::bad_alloc ){
if( mem.GetNumConstructs() != 50 ) FAIL
if( mem.GetNumAllocs() != 51 ) FAIL
if( mem.GetNumDestroys() != 50 ) FAIL
if( mem.GetNumDeallocs() != 50 ) FAIL
if( INSANE( m ) || m.size() != 70 ) FAIL
thrown = true;
}
if( !thrown ) FAIL
return( true );
}
/* ------------------------------------------------------------------
* bounds_test( )
*/
bool bounds_test( )
{
typedef std::multimap<int, int> m_t;
m_t m;
int i, j;
for( i = 0; i < 10; i++ ){
for( j = 0; j < 10; j++ ){
m.insert( m_t::value_type(i, j) );
}
}
m.erase( 5 );
if( m.lower_bound( -1 ) != m.begin() ) FAIL
if( m.lower_bound( 0 ) != m.begin() ) FAIL
if( m.lower_bound( 2 )->first != 2 ||
m.lower_bound( 2 )->second != 0 ) FAIL
if( m.lower_bound( 4 )->first != 4 ||
m.lower_bound( 4 )->second != 0 ) FAIL
if( m.lower_bound( 5 )->first != 6 ||
m.lower_bound( 5 )->second != 0 ) FAIL
if( m.lower_bound( 6 )->first != 6 ||
m.lower_bound( 6 )->second != 0 ) FAIL
if( m.lower_bound( 9 )->first != 9 ||
m.lower_bound( 9 )->second != 0 ) FAIL
if( m.lower_bound( 10 ) != m.end() ) FAIL
if( m.upper_bound( -1 ) != m.begin() ) FAIL
if( m.upper_bound( 0 )->first != 1 ||
m.upper_bound( 0 )->second != 0 ) FAIL
if( m.upper_bound( 2 )->first != 3 ||
m.upper_bound( 2 )->second != 0 ) FAIL
if( m.upper_bound( 4 )->first != 6 ||
m.upper_bound( 4 )->second != 0 ) FAIL
if( m.upper_bound( 5 )->first != 6 ||
m.upper_bound( 5 )->second != 0 ) FAIL
if( m.upper_bound( 6 )->first != 7 ||
m.upper_bound( 6 )->second != 0 ) FAIL
if( m.upper_bound( 8 )->first != 9 ||
m.upper_bound( 8 )->second != 0 ) FAIL
if( m.upper_bound( 9 ) != m.end() ) FAIL
if( m.upper_bound( 10 ) != m.end() ) FAIL
m_t const mc( m );
if( mc.lower_bound( -1 ) != mc.begin() ) FAIL
if( mc.lower_bound( 0 ) != mc.begin() ) FAIL
if( mc.lower_bound( 2 )->first != 2 ||
mc.lower_bound( 2 )->second != 0 ) FAIL
if( mc.lower_bound( 4 )->first != 4 ||
mc.lower_bound( 4 )->second != 0 ) FAIL
if( mc.lower_bound( 5 )->first != 6 ||
mc.lower_bound( 5 )->second != 0 ) FAIL
if( mc.lower_bound( 6 )->first != 6 ||
mc.lower_bound( 6 )->second != 0 ) FAIL
if( mc.lower_bound( 9 )->first != 9 ||
mc.lower_bound( 9 )->second != 0 ) FAIL
if( mc.lower_bound( 10 ) != mc.end() ) FAIL
if( mc.upper_bound( -1 ) != mc.begin() ) FAIL
if( mc.upper_bound( 0 )->first != 1 ||
mc.upper_bound( 0 )->second != 0 ) FAIL
if( mc.upper_bound( 2 )->first != 3 ||
mc.upper_bound( 2 )->second != 0 ) FAIL
if( mc.upper_bound( 4 )->first != 6 ||
mc.upper_bound( 4 )->second != 0 ) FAIL
if( mc.upper_bound( 5 )->first != 6 ||
mc.upper_bound( 5 )->second != 0 ) FAIL
if( mc.upper_bound( 6 )->first != 7 ||
mc.upper_bound( 6 )->second != 0 ) FAIL
if( mc.upper_bound( 8 )->first != 9 ||
mc.upper_bound( 8 )->second != 0 ) FAIL
if( mc.upper_bound( 9 ) != mc.end() ) FAIL
if( mc.upper_bound( 10 ) != mc.end() ) FAIL
//m_t::iterator it = mc.upper_bound( 3 ); //illegal
return( true );
}
/* ------------------------------------------------------------------
* hint_ins_test( )
* insert, find, erase, count
*/
bool hint_ins_test( )
{
typedef std::multimap< int, int > mmii_t;
typedef mmii_t::iterator mmiter_t;
mmii_t m1;
mmiter_t it;
//hint insert tests
m1.clear();
m1.insert( m1.end(), mmii_t::value_type(4,4) );
m1.insert( m1.end(), mmii_t::value_type(7,7) );
m1.insert( --m1.end(), mmii_t::value_type(6,6) );
m1.insert( m1.end(), mmii_t::value_type(8,8) );
m1.insert( m1.begin(), mmii_t::value_type(2,2) );
m1.insert( ++m1.begin(), mmii_t::value_type(3,3) );
m1.insert( m1.find(6), mmii_t::value_type(5,5) );
m1.insert( m1.end(), mmii_t::value_type(0,0) ); //invalid hint
m1.insert( m1.find(0), mmii_t::value_type(1,1) ); //invalid hint
m1.insert( ++m1.begin(), mmii_t::value_type(9,9) ); //invalid hint
//mmiter_t itx =m1.begin();
for( int i = 0; i < 10; i++){
//std::cout<<i<<", "<<itx->first<<", "<<itx->second<<"\n"; ++itx;
mmiter_t it = m1.find( i );
if( INSANE( m1 ) || m1.size() != 10 || m1.empty() ||
it == m1.end() || it->first != i || it->second != i) FAIL
}
//std::cout<<"xxx\n";
m1.clear();
mmiter_t it2;
m1.insert( m1.end(), mmii_t::value_type(0,0) );
m1.insert( m1.end(), mmii_t::value_type(0,1) );
it = m1.insert( m1.end(), mmii_t::value_type(1,1) );
it2 = m1.insert( m1.find(1), mmii_t::value_type(1,0) );
m1.insert( m1.end(), mmii_t::value_type(1,2) );
m1.insert( m1.end(), mmii_t::value_type(2,2) );
m1.insert( it2, mmii_t::value_type(0,2) );
m1.insert( --m1.end(), mmii_t::value_type(2,1) );
m1.insert( ++(++it), mmii_t::value_type(2,0) );
it = m1.insert( m1.begin(), mmii_t::value_type(4,2) ); //invalid hint, insert closest
m1.insert( m1.find(1), mmii_t::value_type(4,1) ); //invalid hint, insert closest
m1.insert( it2, mmii_t::value_type(4,0) ); //invalid hint, insert closest
m1.insert( ++m1.find(4), mmii_t::value_type(3,0) ); //invalid hint, insert closest
m1.insert( it, mmii_t::value_type(3,1) ); //invalid hint, insert closest
m1.insert( ++m1.find(4), mmii_t::value_type(3,2) ); //invalid hint, insert closest
it = m1.begin();
for( int i = 0; i < 5; i++){
for( int j = 0; j < 3; j++){
//std::cout<<i<<", "<<it->first<<", "<<it->second<<","<<m1.size()<<"\n";
if( INSANE( m1 ) || m1.size() != 15 || m1.empty() ||
it == m1.end() || it->first != i || it->second != j) FAIL
++it;
}
}
return( true );
}
int main( )
{
int rc = 0;
//heap_dump();
int original_count = heap_count( );
try {
if( !construct_test( ) || !heap_ok( "t1" ) ) rc = 1;
if( !access_test( ) || !heap_ok( "t2" ) ) rc = 1;
//if( !string_test( ) || !heap_ok( "t3" ) ) rc = 1;
//if( !torture_test( ) || !heap_ok( "t4" ) ) rc = 1;
//if( !clear_test( ) || !heap_ok( "t5" ) ) rc = 1;
if( !iterator_test( ) || !heap_ok( "t6" ) ) rc = 1;
if( !copy_test( ) || !heap_ok( "t7" ) ) rc = 1;
if( !allocator_test() || !heap_ok( "t8" ) ) rc = 1;
if( !bounds_test() || !heap_ok( "t9" ) ) rc = 1;
if( !hint_ins_test() || !heap_ok( "tA" ) ) rc = 1;
}
catch( ... ) {
std::cout << "Unexpected exception of unexpected type.\n";
rc = 1;
}
int heap_diff = heap_count( ) - original_count;
if( heap_diff ) {
heap_dump();
std::cout << "Possible memory leak! " << heap_diff << " " << original_count << "\n";
rc = 1;
}
return( rc );
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?