Home | History | Annotate | Download | only in eh
      1 /***********************************************************************************
      2   test_vector.cpp
      3 
      4  * Copyright (c) 1997
      5  * Mark of the Unicorn, Inc.
      6  *
      7  * Permission to use, copy, modify, distribute and sell this software
      8  * and its documentation for any purpose is hereby granted without fee,
      9  * provided that the above copyright notice appear in all copies and
     10  * that both that copyright notice and this permission notice appear
     11  * in supporting documentation.  Mark of the Unicorn makes no
     12  * representations about the suitability of this software for any
     13  * purpose.  It is provided "as is" without express or implied warranty.
     14 
     15 ***********************************************************************************/
     16 #include "Tests.h"
     17 #include "TestClass.h"
     18 #include "LeakCheck.h"
     19 # if defined (EH_NEW_HEADERS)
     20 #include <vector>
     21 #else
     22 #include <vector.h>
     23 #endif
     24 #include "test_construct.h"
     25 #include "test_assign_op.h"
     26 #include "test_push_back.h"
     27 #include "test_insert.h"
     28 #include "test_push_front.h"
     29 
     30 # if defined (__GNUC__) && defined (__APPLE__)
     31 typedef EH_STD::vector<TestClass, eh_allocator(TestClass) > TestVector;
     32 # else
     33 typedef EH_STD::__vector__<TestClass, eh_allocator(TestClass) > TestVector;
     34 # endif
     35 
     36 inline sequence_container_tag
     37 container_category(const TestVector&)
     38 {
     39   return sequence_container_tag();
     40 }
     41 
     42 void prepare_insert_n( TestVector& c, size_t insCnt );
     43 
     44 void prepare_insert_n( TestVector& c, size_t insCnt )
     45 {
     46     if ( random_number(2) )
     47         c.reserve( c.size() + insCnt );
     48 }
     49 
     50 struct test_reserve
     51 {
     52     test_reserve( size_t n ) : fAmount(n) {
     53             gTestController.SetCurrentTestName("vector::reserve()");
     54     }
     55 
     56     void operator()( TestVector& v ) const
     57     {
     58         v.reserve( fAmount );
     59     }
     60 private:
     61     size_t fAmount;
     62 };
     63 
     64 inline void prepare_insert_range( TestVector& vec, size_t, TestClass* first, TestClass* last )
     65 {
     66     if ( random_number(2) )
     67     {
     68         ptrdiff_t d = 0;
     69         EH_DISTANCE( first, last, d );
     70         vec.reserve( vec.size() + d );
     71     }
     72 }
     73 
     74 void test_vector()
     75 {
     76 
     77     ConstCheck( 0, test_construct_n<TestVector>( random_number(random_base) ) );
     78 
     79     TestVector emptyVector;
     80     TestVector testVector, testVector2;
     81     size_t vectorSize = random_number(random_base);
     82 
     83     testVector.reserve(vectorSize*4);
     84     while ( testVector.size() < vectorSize )
     85     {
     86         TestClass x;
     87         testVector.push_back( x );
     88         testVector2.push_back( TestClass() );
     89     }
     90 
     91     size_t insCnt = random_number(random_base);
     92     TestClass *insFirst = new TestVector::value_type[1+ insCnt];
     93 
     94     ConstCheck( 0, test_construct_pointer_range<TestVector>(insFirst, insFirst+insCnt) );
     95 
     96     WeakCheck( testVector, insert_range_tester(testVector, insFirst, insFirst+insCnt) );
     97     WeakCheck( testVector, insert_range_at_begin_tester(testVector, insFirst, insFirst+insCnt) );
     98     WeakCheck( testVector, insert_range_at_end_tester(testVector, insFirst, insFirst+insCnt) );
     99     delete[] insFirst;
    100 
    101     WeakCheck( testVector, test_insert_one<TestVector>(testVector) );
    102     WeakCheck( testVector, test_insert_one<TestVector>(testVector, 0) );
    103     WeakCheck( testVector, test_insert_one<TestVector>(testVector, (int)testVector.size()) );
    104 
    105     WeakCheck( testVector, test_insert_n<TestVector>(testVector, random_number(random_base) ) );
    106     WeakCheck( testVector, test_insert_n<TestVector>(testVector, random_number(random_base), 0 ) );
    107     WeakCheck( testVector, test_insert_n<TestVector>(testVector, random_number(random_base), (int)testVector.size() ) );
    108 
    109     WeakCheck( testVector, insert_range_tester(testVector, testVector2.begin(), testVector2.end() ) );
    110 
    111 
    112     StrongCheck( testVector, test_reserve( testVector.capacity() + random_number(random_base) ) );
    113     StrongCheck( testVector, test_push_back<TestVector>(testVector) );
    114     StrongCheck( emptyVector, test_push_back<TestVector>(emptyVector) );
    115 
    116     ConstCheck( 0, test_default_construct<TestVector>() );
    117     ConstCheck( 0, test_construct_n_instance<TestVector>( random_number(random_base) ) );
    118     ConstCheck( 0, test_construct_iter_range<TestVector>( testVector2 ) );
    119     ConstCheck( testVector, test_copy_construct<TestVector>() );
    120 
    121     testVector2.resize( testVector.size() * 3 / 2 );
    122     WeakCheck( testVector, test_assign_op<TestVector>( testVector2 ) );
    123     testVector2.clear();
    124     testVector2.resize( testVector.size() * 2 / 3 );
    125     WeakCheck( testVector, test_assign_op<TestVector>( testVector2 ) );
    126 }
    127