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