-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.cpp
171 lines (140 loc) · 3.08 KB
/
Test.cpp
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
#include "Test.h"
#include "SortAlgorithm.h"
#include <time.h>
#include <algorithm>
Test::Test( )
{
}
Test::~Test( )
{
}
vector<int> Test::genTestVector( int size )
{
vector<int> v( size, 0 );
srand( ( unsigned int )time( NULL ) );
for ( int i = 0; i < size; ++i )
{
int val = rand( ) % 200 - 100;
v[i] = val;
}
return v;
}
int Test::genRndNumber( int a, int b )
{
srand( ( unsigned int )time( NULL ) );
int val = rand( ) % (b - a) + a;
return val;
}
void Test::showVector( const vector<int>& v )
{
for ( size_t i = 0; i < v.size( ); i++ )
{
cout << v.at( i ) << " , ";
}
cout << endl;
}
bool Test::rndTest( SORT_FUNC sort_func )
{
bool ret = true;
for ( int i = 0; i < 100; ++i )
{
int size = genRndNumber( 2, 100 );
vector<int> v = genTestVector( size );
vector<int> r = v;
// showVector( v );
// SORT
sort_func( v );
// showVector( v );
if ( !isRight( v, r ) )
{
ret = false;
break;
}
}
return ret;
}
bool Test::isRight( const vector<int>& v, vector<int>& r )
{
std::sort( r.begin( ), r.end( ) );
for ( size_t i = 0; i < r.size( ); ++i )
{
if ( v[i] != r[i] )
{
cout << "Sort Result is : " << endl;
showVector( v );
cout << "But Right Result is : " << endl;
showVector( r );
return false;
}
}
return true;
}
bool Test::preTest( SORT_FUNC sort_func )
{
vector<int> v = genTestVector( 20 );
vector<int> r = v;
sort_func( v );
return isRight( v, r );
}
bool Test::test( SORT_FUNC sort_func )
{
bool ret = false;
if ( preTest( sort_func ) )
{
if ( rndTest( sort_func ) )
{
ret = true;
}
else
{
ret = false;
}
}
else
{
ret = false;
}
return ret;
}
bool Test::test( )
{
bool ret = true;
for ( auto it = sort_funcs_.begin( ); it != sort_funcs_.end( ); ++it )
{
if( test( it->second ) )
{
cout << it->first << " : PASS " << endl;
ret = ret & true;
}
else
{
cout << it->first << " : FAIL " << endl;
ret = ret & false;
}
}
return ret;
}
bool Test::add( const string& name, SORT_FUNC sort_func )
{
sort_funcs_.insert( std::pair<string, SORT_FUNC>( name, sort_func ) );
return true;
}
const map<string, long>& Test::calcTime( int size )
{
vector<int> raw_vec = genTestVector( size );
vector<int> test_vec = raw_vec;
clock_t start_time = 0;
clock_t end_time = 0;
elapsed_times_.clear( );
for ( auto it = sort_funcs_.begin( ); it != sort_funcs_.end( ); ++it )
{
const string name = it->first;
auto func = it->second;
test_vec = raw_vec;
start_time = clock( );
(*func)(test_vec);
end_time = clock( );
elapsed_times_[name] = end_time - start_time;
}
return elapsed_times_;
}