File: test_algo.cpp

package info (click to toggle)
stlport4.6 4.6.2-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 7,056 kB
  • ctags: 16,390
  • sloc: ansic: 46,190; cpp: 18,805; sh: 266; asm: 93; perl: 58; makefile: 10
file content (255 lines) | stat: -rw-r--r-- 7,718 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/***********************************************************************************
	test_algo.cpp
		
 * Copyright (c) 1997
 * Mark of the Unicorn, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Mark of the Unicorn makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.

***********************************************************************************/
#include "Tests.h"
#include "LeakCheck.h"
#include "SortClass.h"

#if defined (EH_NEW_HEADERS)
# include <algorithm>
# include <cassert>
#else
# include <algo.h>
# include <assert.h>
#endif

#if defined (EH_NEW_IOSTREAMS)
# include <iostream>
#else
# include <iostream.h>
#endif


//
// SortBuffer -- a buffer of SortClass objects that can be used to test sorting.
//
struct SortBuffer
{
    enum { kBufferSize = 100 };
	
    SortClass* begin() { return items; }
    const SortClass* begin() const { return items; }
    SortClass* end() { return items + kBufferSize; }
    const SortClass* end() const { return items + kBufferSize; }
	
	// Sort each half of the buffer and reset the address of each element
	// so that merge() can be tested for stability.
    void PrepareMerge()
    {
        EH_STD::sort( begin(), begin() + ( end() - begin() )/2 );
        EH_STD::sort( begin() + ( end() - begin() )/2, end() );
        for ( SortClass* p = begin(); p != end(); p++ )
            p->ResetAddress();
    }
    
  SortBuffer()
  {
    PrepareMerge();
  }
  
  SortBuffer( const SortBuffer& rhs )
  {
    SortClass* q = begin();
    for ( const SortClass* p = rhs.begin() ; p != rhs.end(); p++,q++ )
      *q = *p;
  }

private:	
    SortClass items[kBufferSize];
};

//
// less_by_reference -- a functor for comparing objects against a
// 	constant value.
//
template <class T>
struct less_by_reference
{
    less_by_reference( const T& arg ) : fArg(arg) {}
    bool operator()( const T& x ) const { return x < fArg; }
private:
    const T& fArg;
};

struct test_stable_partition
{
    test_stable_partition( const SortBuffer& buf )
        : orig( buf ), partitionPoint(SortClass::kRange / 2) {
        gTestController.SetCurrentTestName("stable_partition()");
        }

    void operator()( SortBuffer& buf ) const
    {
        less_by_reference<SortClass> throw_cmp( partitionPoint );
		
        SortClass* d = EH_STD::stable_partition( buf.begin(), buf.end(), throw_cmp );
		
		// If we get here no exception occurred during the operation.
		// Stop any potential failures that might occur during verification.
        gTestController.CancelFailureCountdown();
        
        // Prepare an array of counts of the occurrence of each value in
        // the legal range.
        unsigned counts[SortClass::kRange];
        EH_STD::fill_n( counts, (int)SortClass::kRange, 0 );
        for ( const SortClass *q = orig.begin(); q != orig.end(); q++ )
            counts[ q->value() ]++;
			
        less_by_reference<TestClass> cmp( partitionPoint );
        for ( const SortClass* p = buf.begin(); p != buf.end(); p++ )
        {
        	// Check that adjacent items with the same value haven't been
        	// reordered. This could be a more thorough test.
            if ( p != buf.begin() && p->value() == p[-1].value() )
                EH_ASSERT( p->GetAddress() > p[-1].GetAddress() );
            
            // Check that the partitioning worked.
            EH_ASSERT( (p < d) == cmp( *p ) );
            
            // Decrement the appropriate count for each value.
            counts[ p->value() ]--;
        }
        
        // Check that the values were only rearranged, and none were lost.
        for ( unsigned j = 0; j < SortClass::kRange; j++ )
            EH_ASSERT( counts[j] == 0 );
    }
	
private:
    const SortBuffer& orig;
    SortClass partitionPoint;
};

void assert_sorted_version( const SortBuffer& orig, const SortBuffer& buf );

/*===================================================================================
	assert_sorted_version

	EFFECTS: Asserts that buf is a stable-sorted version of orig.
====================================================================================*/
void assert_sorted_version( const SortBuffer& orig, const SortBuffer& buf )
{
	// Stop any potential failures that might occur during verification.
    gTestController.CancelFailureCountdown();
    
    // Prepare an array of counts of the occurrence of each value in
    // the legal range.
    unsigned counts[SortClass::kRange];
    EH_STD::fill_n( counts, (int)SortClass::kRange, 0 );
    for ( const SortClass *q = orig.begin(); q != orig.end(); q++ )
        counts[ q->value() ]++;
	
	// Check that each element is greater than the previous one, or if they are
	// equal, that their order has been preserved.
    for ( const SortClass* p = buf.begin(); p != buf.end(); p++ )
    {
        if ( p != buf.begin() )
            EH_ASSERT( p->value() > p[-1].value()
                    || p->value() == p[-1].value() && p->GetAddress() > p[-1].GetAddress() );
		// Decrement the appropriate count for each value.
        counts[ p->value() ]--;
    }
    
	// Check that the values were only rearranged, and none were lost.
    for ( unsigned j = 0; j < SortClass::kRange; j++ )
        EH_ASSERT( counts[j] == 0 );
}

//
// The test operators
//
struct test_stable_sort_1
{
    test_stable_sort_1( const SortBuffer& buf )
        : orig( buf ) {
        gTestController.SetCurrentTestName("stable_sort() #1");
        }

    void operator()( SortBuffer& buf ) const
    {
        EH_STD::stable_sort( buf.begin(), buf.end() );
        assert_sorted_version( orig, buf );
    }
	
private:
    const SortBuffer& orig;
};

struct test_stable_sort_2
{
    test_stable_sort_2( const SortBuffer& buf )
        : orig( buf ) {        
            gTestController.SetCurrentTestName("stable_sort() #2");
        }

    void operator()( SortBuffer& buf ) const
    {
      EH_STD::stable_sort( buf.begin(), buf.end(), EH_STD::less<SortClass>() );
      assert_sorted_version( orig, buf );
    }
	
private:
    const SortBuffer& orig;
};

struct test_inplace_merge_1
{
    test_inplace_merge_1( SortBuffer& buf )
        : orig( buf ) {
        gTestController.SetCurrentTestName("inplace_merge #1()");
        }

    void operator()( SortBuffer& buf ) const
    {
        EH_STD::inplace_merge( buf.begin(), buf.begin() + ( buf.end() - buf.begin() )/2, buf.end() );
        assert_sorted_version( orig, buf );
    }
	
private:
    const SortBuffer& orig;
};

struct test_inplace_merge_2
{
    test_inplace_merge_2( SortBuffer& buf )
        : orig( buf ) {
        gTestController.SetCurrentTestName("inplace_merge() #2");
        }

    void operator()( SortBuffer& buf ) const
    {
        EH_STD::inplace_merge( buf.begin(), buf.begin() + ( buf.end() - buf.begin() )/2, buf.end(), 
                       EH_STD::less<SortClass>() );
        assert_sorted_version( orig, buf );
    }
	
private:
    const SortBuffer& orig;
};

void test_algo()
{
    SortBuffer mergeBuf;
    mergeBuf.PrepareMerge();
    
    EH_STD::cerr<<"EH test : testing algo.h"<<EH_STD::endl;
    WeakCheck( mergeBuf, test_inplace_merge_1( mergeBuf ) );
    WeakCheck( mergeBuf, test_inplace_merge_2( mergeBuf ) );

    SortBuffer buf;
    WeakCheck( buf, test_stable_sort_1( buf ) );
    WeakCheck( buf, test_stable_sort_2( buf ) );
    WeakCheck( buf, test_stable_partition( buf ) );
}