map02.cpp

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C++ 代码 · 共 494 行 · 第 1/2 页

CPP
494
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*  Copyright (c) 2004-2006 The Open Watcom Contributors. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Tests for std::multimap
*
****************************************************************************/

#include <iostream>
#include <string>
#include "sanity.cpp"
#include "allocxtr.hpp"
#include <map>

/* ------------------------------------------------------------------
 * construct_test( )
 * Construct different maps in different ways.
 */
bool construct_test( )
{
    typedef std::multimap< int, int > mmii_t;
    mmii_t m1;
    int i;
    for( i = 0;  i < 10;  i++ ){
        m1.insert( mmii_t::value_type(1, i) );
        m1.insert( mmii_t::value_type(2, 2*i) );
    }
    mmii_t::iterator it = m1.begin();
    for( i = 0; i < 20; i++, ++it){
        //std::cout<< (*it).first << " " << (*it).second << "\n";
    }
    //if(  ) FAIL
    return( true );
}
/* ------------------------------------------------------------------
 * access_test( )
 * insert, find, erase, count
 */
bool access_test( )
{
    typedef std::multimap< int, int > mmii_t;
    typedef mmii_t::iterator mmiter_t;
    mmii_t m1;
    
    int num[] = {11,11,11,10,4,8,8,2,1,12,13,19,7,18,4,2,2,17,11};
    int notnum[] = {3,5,6,9,14,15,16};
    int const num_size = sizeof(num) / sizeof(int);
    int const notnum_size = sizeof(notnum) / sizeof(int);
        
    //insert
    for( int i = 0; i < num_size; i++ ){
        mmiter_t ans;
        ans = m1.insert( mmii_t::value_type( num[i], num[i]*num[i] ) );
        if( INSANE( m1 ) || m1.size() != (i+1) || m1.empty() ) FAIL
    }
    //find inserted
    for( int i = 0; i < num_size; i++ ){
        mmiter_t it = m1.find( num[i] );
        if( INSANE( m1 ) || m1.size() != num_size || m1.empty() ||
            it == m1.end() || (*it).second != num[i]*num[i] ) FAIL
    }
    //find not inserted
    for( int i = 0; i < notnum_size; i++ ){
        mmiter_t it = m1.find( notnum[i] );
        if( INSANE( m1 ) || m1.size() != num_size || m1.empty() ||
            it != m1.end() ) FAIL
    }
    //insert again
    for( int i = 0; i < num_size; i++ ){
        mmiter_t ans;
        ans = m1.insert( mmii_t::value_type( num[i], -1 ) );
        if( INSANE( m1 ) || m1.size() != (num_size+i+1) ||
            m1.empty() ) FAIL
    }
    //couple of spot checks on contents
    if( m1.count( 11 ) != 8 ) FAIL      //count the number of 11's
    mmiter_t it = m1.find( 11 );
    for( int i = 0; i < 4; i++ ){       //first 11's should map to 11^2
        if( INSANE( m1 ) || m1.size() != num_size*2 ||
            m1.empty() || (*it).second != 11*11 ) FAIL
        ++it;
    }
    for( int i = 0; i < 4; i++ ){       //next 11's should map to -1
        if( INSANE( m1 ) || m1.size() != num_size*2 ||
            m1.empty() || (*it).second != -1 ) FAIL
        ++it;
    }
    //erase tests
    //erase first of multiple keys
    m1.erase( m1.find( 11 ) );
    if( INSANE( m1 ) || m1.size() != num_size*2-1 || m1.count( 11 ) != 7 ) FAIL
    //erase last of multiple keys
    m1.erase( --( m1.find( 12 ) ) );
    if( INSANE( m1 ) || m1.size() != num_size*2-2 || m1.count( 11 ) != 6 ) FAIL
    //erase middle of multiple keys
    m1.erase( --(--(--( m1.find( 12 ) ))) );
    if( INSANE( m1 ) || m1.size() != num_size*2-3 || m1.count( 11 ) != 5 ) FAIL
    //erase all keys
    m1.erase( m1.find(12) );
    m1.erase( m1.find(12) );
    if( INSANE( m1 ) || m1.size() != num_size*2-5 || m1.count( 12 ) != 0 ) FAIL
    //erase all keys other method
    if( m1.erase( 2 ) != 6 ) FAIL;
    if( INSANE( m1 ) || m1.size() != num_size*2-11 || m1.count( 2 ) != 0 ) FAIL
    
    return( true );
}
/* ------------------------------------------------------------------
 * iterator_test( )
 * Test the iterator functionality
 */
bool iterator_test( )
{
    typedef std::multimap< int, int > m_t;
    m_t m1;
    m_t::iterator it;
    m_t::const_iterator cit;
    //use nums[] so numbers are not inserted in simple order
    int nums[] = { 2,1,5,6,7,8,4,9,3,0 };   
    for( int i = 0; i < 10; i++ ){
        for( int j = 0; j < 5; j++ ){
            m1.insert( m_t::value_type( nums[i], nums[i]*nums[i] ) );
        }
    }
    //test increment and dereferencing ( will be sorted by std::less<int> )
    it = m1.begin( );
    int ans = (*it).first;
    for( int i = 0; i < 10 ; i++ ){
        for( int j = 0; j < 5; j++ ){
            if( INSANE( m1 ) || ans != i || it->second != i*i ) FAIL
            it->second = -i;
            if( i%2 ) ans = (*(it++)).first + ( (j == 4) ? 1 : 0 );
            else ans = (*(++it)).first;
        }
    }
    //and again with const iterator
    cit = m1.begin( );
    ans = (*cit).first;
    for( int i = 0; i < 10 ; i++ ){
        for( int j = 0; j < 5; j++ ){
            if( INSANE( m1 ) || ans != i || cit->second != -i ) FAIL
            if( i%2 ) ans = (*(cit++)).first + ( (j == 4) ? 1 : 0 );
            else ans = (*(++cit)).first;
        }
    }
    //test decrement ( will be sorted by std::less<int> )
    it = m1.end( );
    for( int i = 9; i > 0 ; i-- ){
        for( int j = 0; j < 5; j++ ){
            if( i%2 ) ans = (*(--it)).first;
            else ans = (*(it--)).first - ( (j == 0) ? 1 : 0 );
            if( INSANE( m1 ) || ans != i || it->second != -i ) FAIL
            (*it).second = i*i;
        }
    }
    //and again with const iterator
    cit = m1.end( );
    for( int i = 9; i > 0 ; i-- ){
        for( int j = 0; j < 5; j++ ){
            if( i%2 ) ans = (*(--cit)).first;
            else ans = (*(cit--)).first - ( (j == 0) ? 1 : 0 );
            if( INSANE( m1 ) || ans != i || cit->second != i*i ) FAIL
        }
    }
    return( true );
}
/* ------------------------------------------------------------------
 * copy_test( )
 * Test all things plagiarised
 */
bool copy_test( )
{
    typedef std::multimap< int, int > m_t;
    m_t m1;
    int i, j;
    for( i = 0; i < 10; i++ ){
        for( j = 0; j < 10; j++ ){
            m1.insert( m_t::value_type( i, j ) );
        }
    }
    m_t m1cpy( m1 );
    if( INSANE( m1cpy ) || m1cpy.size() != 100 ) FAIL
    //check it copied to new one ok
    for( i = 0; i < 10; i++ ){
        m_t::iterator it = m1cpy.find( i );
        for( j = 0; j < 10; j++, ++it ){
            if( it->first != i && it->second != j ) FAIL
            it->second = -j;
        }
    }
    m1cpy.erase( 1 );
     if( INSANE( m1cpy ) || m1cpy.size() != 90 ) FAIL
    //check it is a copy and old not effecting new one
    if( INSANE( m1 ) || m1.size() != 100 ) FAIL
    for( i = 0; i < 10; i++ ){
        m_t::iterator it = m1.find( i );
        for( j = 0; j < 10; j++, ++it ){
            if( it->first != i && it->second != j ) FAIL
        }
    }
    m_t m1cpy2 = m1;    //assignment style construct
    if( INSANE( m1cpy2 ) || m1cpy2.size() != 100 ) FAIL
    for( i = 0; i < 10; i++ ){
        m_t::iterator it = m1cpy2.find( i );
        for( j = 0; j < 10; j++, ++it ){
            if( it->first != i && it->second != j ) FAIL
        }
    }
    //check assignment
    m1.erase( 5 );
    m1cpy.clear();
    m1.insert( m_t::value_type( 11, -11 ));
    m1 = m1cpy = m1cpy2;
    if( INSANE( m1cpy ) || m1cpy.size() != 100 ) FAIL
    if( INSANE( m1 )    || m1.size()    != 100 ) FAIL
    for( i = 0; i < 10; i++ ){
        m_t::iterator it = m1.find( i );
        m_t::iterator it2 = m1cpy.find( i );
        for( j = 0; j < 10; j++, ++it ){
            if( it->first != i && it->second != j ) FAIL
            if( it->first != i && it->second != j ) FAIL
        }
    }
    
    return( true );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?