• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***********************************************************************************
2   test_bit_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 
18 #if defined( EH_BIT_VECTOR_IMPLEMENTED )
19 
20 # if defined (EH_NEW_HEADERS)
21 # ifdef __SUNPRO_CC
22 # include <stdio.h>
23 # endif
24 #include <vector>
25 # else
26 #include <bvector.h>
27 # endif
28 
29 #include "LeakCheck.h"
30 #include "test_construct.h"
31 #include "test_assign_op.h"
32 #include "test_push_back.h"
33 #include "test_insert.h"
34 #include "test_push_front.h"
35 
36 typedef EH_BIT_VECTOR BitVector;
37 
38 inline sequence_container_tag
container_category(const BitVector &)39 container_category(const BitVector&)
40 {
41   return sequence_container_tag();
42 }
43 
44 USING_CSTD_NAME(size_t)
45 
46   //
47 // test_BitVector_reserve
48 //
49 struct test_BitVector_reserve
50 {
test_BitVector_reservetest_BitVector_reserve51     test_BitVector_reserve( size_t n ) : fAmount(n)
52     {
53         gTestController.SetCurrentTestName("BitVector::reserve()");
54     }
55 
operator ()test_BitVector_reserve56     void operator()( BitVector& v ) const
57     {
58         v.reserve( fAmount );
59     }
60 private:
61     size_t fAmount;
62 };
63 
64 /*===================================================================================
65   test_bit_vector
66 
67   EFFECTS:  Performs tests on bit vectors
68 ====================================================================================*/
test_bit_vector()69 void test_bit_vector()
70 {
71 #define __WORD_BIT (int(CHAR_BIT*sizeof(unsigned int)))
72 
73   // Make some bit vectors to work with.
74   BitVector emptyVector;
75   BitVector testVector, testVector2;
76 
77   EH_ASSERT( testVector.size() == 0 );
78 
79   size_t BitVectorSize = random_number( random_base );
80   // Half the time, choose a size that will guarantee immediate reallocation
81   if ( random_number(2) )
82     BitVectorSize = BitVectorSize / __WORD_BIT * __WORD_BIT;
83 
84   EH_ASSERT( testVector.size() == 0 );
85   testVector.reserve(BitVectorSize);
86   EH_ASSERT( testVector.size() == 0 );
87   while (testVector.size() < BitVectorSize) {
88     testVector.push_back(random_number(2) != 0);
89     testVector2.push_back(random_number(2) != 0);
90   }
91 
92   // Test insertions
93   StrongCheck(testVector, test_insert_one<BitVector>(testVector) );
94   StrongCheck(testVector, test_insert_one<BitVector>(testVector,0) );
95   StrongCheck(testVector, test_insert_one<BitVector>(testVector, (int)testVector.size()) );
96 
97   StrongCheck(testVector, test_insert_n<BitVector>(testVector, random_number(random_base) ) );
98   StrongCheck(testVector, test_insert_n<BitVector>(testVector, random_number(random_base),0 ) );
99   StrongCheck(testVector, test_insert_n<BitVector>(testVector, random_number(random_base), (int)testVector.size()) );
100 #if 0
101   // Allocate some random bools to insert
102   size_t insCnt = 1 + random_number(random_base);
103   bool *insFirst = new BitVector::value_type[insCnt];
104   for (size_t n = 0; n < insCnt; n++)
105     insFirst[n] = random_number(2);
106   StrongCheck(testVector, insert_range_tester(testVector, insFirst, insFirst+insCnt));
107   StrongCheck(testVector, insert_range_at_begin_tester(testVector, insFirst, insFirst+insCnt));
108   StrongCheck(testVector, insert_range_at_end_tester(testVector, insFirst, insFirst+insCnt));
109   ConstCheck(0, test_construct_pointer_range<BitVector>( insFirst, insFirst + insCnt));
110   delete[] insFirst;
111 #endif
112   StrongCheck(testVector, insert_range_tester(testVector, testVector2.begin(), testVector2.end()));
113   StrongCheck(testVector, insert_range_at_begin_tester(testVector, testVector2.begin(),
114                                                                    testVector2.end()));
115   StrongCheck(testVector, insert_range_at_end_tester(testVector, testVector2.begin(),
116                                                                  testVector2.end()));
117   StrongCheck(testVector, test_BitVector_reserve( testVector.capacity() + random_number(50)));
118   StrongCheck(testVector, test_push_back<BitVector>(testVector));
119   StrongCheck(emptyVector, test_push_back<BitVector>(emptyVector));
120 
121   ConstCheck(0, test_default_construct<BitVector>());
122   ConstCheck(0, test_construct_n<BitVector>(random_number(random_base)));
123   ConstCheck(0, test_construct_n_instance<BitVector>(random_number(random_base)));
124   ConstCheck(0, test_construct_iter_range<BitVector>(testVector2));
125   ConstCheck(testVector, test_copy_construct<BitVector>() );
126   WeakCheck(testVector, test_assign_op<BitVector>(testVector2) );
127 }
128 
129 # endif /* BIT_VECTOR_IMPLEMENTED */
130