set02.cpp

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

CPP
485
字号
/****************************************************************************
*
*                            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 <cstdlib>
#include <iostream>
#include <string>
#include "sanity.cpp"
#include "allocxtr.hpp"
//#include <_rbtree.h>
#include <set>

/* ------------------------------------------------------------------
 * construct_test( )
 * Construct different maps in different ways.
 */
bool construct_test( )
{
    typedef std::multiset< int > msii_t;
    msii_t s1;
    int i;
    for( i = 0;  i < 100;  i++ ){
        s1.insert( 1 );
        s1.insert( 2 );
    }
    return( true );
}
/* ------------------------------------------------------------------
 * access_test( )
 * insert, find, erase, count
 */
bool access_test( )
{
    typedef std::multiset< int > msii_t;
    typedef msii_t::iterator msiter_t;
    msii_t s1;
    
    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++ ){
        msiter_t it;
        it = s1.insert( num[i] );
        if( INSANE( s1 ) || s1.size() != (i+1) || s1.empty() ||
            it == s1.end() || *it != num[i] ) FAIL
    }
    //find inserted
    for( int i = 0; i < num_size; i++ ){
        msiter_t it = s1.find( num[i] );
        if( INSANE( s1 ) || s1.size() != num_size || s1.empty() ||
            it == s1.end() || *it != num[i] ) FAIL
    }
    //find not inserted
    for( int i = 0; i < notnum_size; i++ ){
        msiter_t it = s1.find( notnum[i] );
        if( INSANE( s1 ) || s1.size() != num_size || s1.empty() ||
            it != s1.end() ) FAIL
    }
    //insert again
    for( int i = 0; i < num_size; i++ ){
        msiter_t it;
        it = s1.insert( num[i] );
        if( INSANE( s1 ) || s1.size() != (num_size+i+1) ||
            s1.empty() || it == s1.end() || *it != num[i] ) FAIL
    }
    //spot checks on contents
    if( s1.count( 11 ) != 8 ) FAIL      //count the number of 11's
    msiter_t it = s1.find( 11 );
    for( int i = 0; i < 4; i++ ){
        if( INSANE( s1 ) || s1.size() != num_size*2 ||
            s1.empty() || it == s1.end() || *it != 11 ) FAIL
        ++it;
    }
    //erase tests
    //erase first of multiple keys
    s1.erase( s1.find( 11 ) );
    if( INSANE( s1 ) || s1.size() != num_size*2-1 || s1.count( 11 ) != 7 ) FAIL
    //erase last of multiple keys
    s1.erase( --( s1.find( 12 ) ) );
    if( INSANE( s1 ) || s1.size() != num_size*2-2 || s1.count( 11 ) != 6 ) FAIL
    //erase middle of multiple keys
    s1.erase( --(--(--( s1.find( 12 ) ))) );
    if( INSANE( s1 ) || s1.size() != num_size*2-3 || s1.count( 11 ) != 5 ) FAIL
    //erase all keys
    s1.erase( s1.find(12) );
    s1.erase( s1.find(12) );
    if( INSANE( s1 ) || s1.size() != num_size*2-5 || s1.count( 12 ) != 0 ) FAIL
    //erase all keys other method
    if( s1.erase( 2 ) != 6 ) FAIL;
    if( INSANE( s1 ) || s1.size() != num_size*2-11 || s1.count( 2 ) != 0 ) FAIL
    
    return( true );
}
/* ------------------------------------------------------------------
 * iterator_test( )
 * Test the iterator functionality
 */
bool iterator_test( )
{
    typedef std::multiset< int > s_t;
    s_t s1;
    s_t::iterator it;
    s_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++ ){
            s1.insert( nums[i] );
        }
    }
    //test increment and dereferencing ( will be sorted by std::less<int> )
    it = s1.begin( );
    int ans = *it;
    for( int i = 0; i < 10 ; i++ ){
        for( int j = 0; j < 5; j++ ){
            if( INSANE( s1 ) || ans != i || *it != i ) FAIL
            if( i%2 ) ans = *(it++) + ( (j == 4) ? 1 : 0 );
            else ans = *(++it);
        }
    }
    //and again with const iterator
    cit = s1.begin( );
    ans = *cit;
    for( int i = 0; i < 10 ; i++ ){
        for( int j = 0; j < 5; j++ ){
            if( INSANE( s1 ) || ans != i || *cit != i ) FAIL
            if( i%2 ) ans = *(cit++) + ( (j == 4) ? 1 : 0 );
            else ans = *(++cit);
        }
    }
    //test decrement ( will be sorted by std::less<int> )
    it = s1.end( );
    for( int i = 9; i > 0 ; i-- ){
        for( int j = 0; j < 5; j++ ){
            if( i%2 ) ans = *(--it);
            else ans = *(it--) - ( (j == 0) ? 1 : 0 );
            if( INSANE( s1 ) || ans != i || *it != i ) FAIL
        }
    }
    //and again with const iterator
    cit = s1.end( );
    for( int i = 9; i > 0 ; i-- ){
        for( int j = 0; j < 5; j++ ){
            if( i%2 ) ans = *(--cit);
            else ans = *(cit--) - ( (j == 0) ? 1 : 0 );
            if( INSANE( s1 ) || ans != i || *cit != i ) FAIL
        }
    }
    return( true );
}
/* ------------------------------------------------------------------
 * copy_test( )
 * Test all things plagiarised
 */
bool copy_test( )
{
    typedef std::multiset< int > s_t;
    s_t s1;
    int i, j;
    for( i = 0; i < 10; i++ ){
        for( j = 0; j < 10; j++ ){
            s1.insert( i );
        }
    }
    s_t s1cpy( s1 );
    if( INSANE( s1cpy ) || s1cpy.size() != 100 ) FAIL
    //check it copied to new one ok
    for( i = 0; i < 10; i++ ){
        s_t::iterator it = s1cpy.find( i );
        for( j = 0; j < 10; j++, ++it ){
            if( *it != i ) FAIL
        }
    }
    s1cpy.erase( 1 );
    if( INSANE( s1cpy ) || s1cpy.size() != 90 ) FAIL
    //check it is a copy and old not effecting new one
    if( INSANE( s1 ) || s1.size() != 100 ) FAIL
    for( i = 0; i < 10; i++ ){
        s_t::iterator it = s1.find( i );
        for( j = 0; j < 10; j++, ++it ){
            if( *it != i ) FAIL
        }
    }
    //assignment style construct
    s_t s1cpy2 = s1;
    if( INSANE( s1cpy2 ) || s1cpy2.size() != 100 ) FAIL
    for( i = 0; i < 10; i++ ){
        s_t::iterator it = s1cpy2.find( i );
        for( j = 0; j < 10; j++, ++it ){
            if( *it != i ) FAIL
        }
    }
    //check assignment
    s1cpy.clear();
    s1.erase( 5 );
    s1.insert( 11 );
    s1 = s1cpy = s1cpy2;
    if( INSANE( s1cpy ) || s1cpy.size() != 100 ) FAIL
    if( INSANE( s1 )    || s1.size()    != 100 ) FAIL
    for( i = 0; i < 10; i++ ){
        s_t::iterator it = s1.find( i );
        s_t::iterator it2 = s1cpy.find( i );
        for( j = 0; j < 10; j++, ++it ){
            if( *it != i ) FAIL
            if( *it != i ) FAIL
        }
    }
    
    return( true );
}
/* ------------------------------------------------------------------
 * allocator_test
 * test stateful allocators and exception handling
 */

⌨️ 快捷键说明

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