vector01.cpp
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C++ 代码 · 共 519 行 · 第 1/2 页
CPP
519 行
/****************************************************************************
*
* 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: This file contains the functional tests for std::vector.
*
****************************************************************************/
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "itcat.h"
#include "sanity.cpp"
bool construction_test( )
{
std::vector< int > v1;
std::vector< int > v2(10);
std::vector< int > v3(v1);
std::vector< int > v4(v2);
std::vector< int > v5(10, 1);
if( v1.size( ) != 0 || !v1.empty( ) || INSANE( v1 ) ) FAIL;
if( v2.size( ) != 10 || v2.empty( ) || INSANE( v2 ) ) FAIL;
if( v3.size( ) != 0 || !v3.empty( ) || INSANE( v3 ) ) FAIL;
if( v4.size( ) != 10 || v4.empty( ) || INSANE( v4 ) ) FAIL;
if( v5.size( ) != 10 || v5.empty( ) || INSANE( v5 ) ) FAIL;
// Use checked access so that we get an exception if something's wrong.
for( std::vector< int >::size_type i = 0; i < v5.size( ); ++i ) {
if( v5.at( i ) != 1 ) FAIL;
}
return( true );
}
bool access_test( )
{
using std::out_of_range;
std::vector< int > v(10);
for (int i = 0; i < 10; ++i ) {
v[i] = i;
}
for (int i = 0; i < 10; ++i ) {
if( v[i] != i ) FAIL;
}
for (int i = 0; i < 10; ++i ) {
v.at( i ) = i;
}
for (int i = 0; i < 10; ++i ) {
if( v.at( i ) != i ) FAIL;
}
try {
v.at( 10 ) = 0;
FAIL;
}
catch( out_of_range ) {
// Okay
}
return( true );
}
bool assign_test( )
{
std::vector< int > v1(10, 1);
std::vector< int > v2( 5, 2);
std::vector< int > v3(15, 3);
// Make destination smaller.
v1 = v2;
if( v1.size( ) != 5 || INSANE( v1 ) ) FAIL;
for( std::vector< int >::size_type i = 0; i < v1.size( ); ++i ) {
if( v1.at( i ) != 2 ) FAIL;
}
// Make destination bigger.
v1 = v3;
if( v1.size( ) != 15 || INSANE( v1 ) ) FAIL;
for( std::vector< int >::size_type i = 0; i < v1.size( ); ++i ) {
if( v1.at( i ) != 3 ) FAIL;
}
// Make destination smaller.
v1.assign( 10, 4 );
if( v1.size( ) != 10 || INSANE( v1 ) ) FAIL;
for( std::vector< int >::size_type i = 0; i < v1.size( ); ++i ) {
if( v1.at( i ) != 4 ) FAIL;
}
// Make destination bigger.
v1.assign( 20, 5 );
if( v1.size( ) != 20 || INSANE( v1 ) ) FAIL;
for( std::vector< int >::size_type i = 0; i < v1.size( ); ++i ) {
if( v1.at( i ) != 5 ) FAIL;
}
// Avoid using the template assign member.
std::vector< std::string > s1(10, "Hello");
// Make destination smaller.
s1.assign( 10, "There" );
if( s1.size( ) != 10 || INSANE( s1 ) ) FAIL;
for( std::vector< int >::size_type i = 0; i < s1.size( ); ++i ) {
if( s1.at( i ) != "There" ) FAIL;
}
// Make destination bigger.
s1.assign( 20, "World" );
if( s1.size( ) != 20 || INSANE( s1 ) ) FAIL;
for( std::vector< int >::size_type i = 0; i < s1.size( ); ++i ) {
if( s1.at( i ) != "World" ) FAIL;
}
// Try the template assign member in the "natural" way.
int array[4] = { 0, 1, 2, 3 };
v1.assign(InpIt<int>(array), InpIt<int>(array + 4));
if( v1.size( ) != 4 || INSANE( v1 ) ) FAIL;
for( std::vector< int >::size_type i = 0; i < v1.size( ); ++i ) {
if( v1.at( i ) != i ) FAIL;
}
// Make sure this does the right thing.
std::vector< double > d1(10, 3.14);
d1.assign( 5, 2.718 );
if( d1.size( ) != 5 || INSANE( s1 ) ) FAIL;
for( std::vector< int >::size_type i = 0; i < d1.size( ); ++i ) {
if( d1.at( i ) != 2.718 ) FAIL;
}
return( true );
}
template< class Type >
bool pushback_test(Type *check, std::size_t check_size )
{
using std::vector; // Make sure this works.
vector< Type > vec;
for( typename vector< Type >::size_type i = 0; i < check_size; ++i ) {
vec.push_back( check[i] );
if( vec.back( ) != check[i] ) FAIL;
if( vec.front( ) != check[0] ) FAIL;
}
if( vec.size( ) != check_size || INSANE( vec ) ) FAIL;
std::reverse( check, check + check_size );
for( typename vector< Type >::size_type i = 0; i < check_size; ++i ) {
if( vec.back( ) != check[i] ) FAIL;
if( vec.front( ) != check[check_size - 1] ) FAIL;
vec.pop_back( );
}
if( vec.size( ) != 0 || INSANE( vec ) ) FAIL;
return( true );
}
bool iterator_test( )
{
std::vector< int > vec(10);
int counter;
for(std::vector< int >::size_type i = 0; i < 10; ++i ) {
vec[i] = i;
}
std::vector< int >::iterator p;
counter = 0;
for( p = vec.begin( ); p != vec.end( ); ++p ) {
if( *p != counter ) FAIL;
++counter;
}
// Use p++ (probably a more exhaustive test should be created).
counter = 0;
for( p = vec.begin( ); p != vec.end( ); p++ ) {
if( *p != counter ) FAIL;
++counter;
}
// Also run iterators backwards, do pointer arithmetic, compare, etc.
return( true );
}
bool insert_single_test( )
{
typedef std::vector< int >::size_type size_type;
typedef std::vector< int >::iterator iterator;
// Try inserting repeatedly at the beginning.
std::vector< int > v1;
size_type cap = v1.capacity( );
int lim = 4 * cap;
iterator it = v1.end( );
for( int i = 0; i < lim; ++i ) {
it = v1.insert( it, i );
if( INSANE( v1 ) ) FAIL;
}
int expected_value = lim - 1;
for( size_type i = 0; i < lim; ++i ) {
if( v1[i] != expected_value ) FAIL;
--expected_value;
}
// Try inserting repeatedly at the end.
std::vector< int > v2;
cap = v2.capacity( );
lim = 4 * cap;
it = v2.end( );
for( int i = 0; i < lim; ++i ) {
it = v2.insert( it, i );
++it;
if( INSANE( v2 ) ) FAIL;
}
expected_value = 0;
for( size_type i = 0; i < lim; ++i ) {
if( v2[i] != expected_value ) FAIL;
++expected_value;
}
// Add test case for repeated insertions in the middle.
return( true );
}
bool insert_multiple_test( )
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?