-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sorted_listvector.cpp
More file actions
211 lines (189 loc) · 5.01 KB
/
Copy pathtest_sorted_listvector.cpp
File metadata and controls
211 lines (189 loc) · 5.01 KB
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
#include "stdafx.h"
#include "sorted_listvector.h"
#include <iostream>
using namespace std;
static bool is_listvector_sorted(const sorted_listvector<int>& listvec)
{
bool sorted = true;
int prev = INT_MIN;
for(list<sorted_vector<int> >::const_iterator itList = listvec.begin(), itListEnd = listvec.end(); itList != itListEnd; ++itList)
{
for(vector<int>::const_iterator itVec = itList->begin(), itVecEnd = itList->end(); itVec != itVecEnd; ++itVec)
{
int cur = *itVec;
if(prev > cur)
{
sorted = false;
}
prev = cur;
}
}
return sorted;
}
static void print_listvector(const sorted_listvector<int>& listvec)
{
cout << "[" << listvec.size() << "]=" << endl;
int prev = INT_MIN;
int cur = 0;
for(list<sorted_vector<int> >::const_iterator itList = listvec.begin(), itListEnd = listvec.end(); itList != itListEnd; ++itList)
{
cout << " " << cur++ << ":[" << itList->size() << "]={";
for(vector<int>::const_iterator itVec = itList->begin(), itVecEnd = itList->end(); itVec != itVecEnd; ++itVec)
{
cout << " ";
int cur = *itVec;
if(prev > cur)
{
cout << "ERROR->";
}
prev = cur;
cout << cur;
}
cout << " }" << endl;
}
}
static void insert_listvector(sorted_listvector<int>& listvec, int val)
{
cout << "insert " << val << "\t-> ";
listvec.insert(val);
print_listvector(listvec);
}
static bool search_listvector(sorted_listvector<int>& listvec, int val)
{
cout << "search " << val << "\t-> ";
bool found = listvec.has(val);
cout << (found ? "found" : "not found") << endl;
return found;
}
void Test4()
{
cout << "*** INSERT TEST #1 ***" << endl;
{
sorted_listvector<int> listvec(4);
int input[] = {10, 1, 2, 11, 6, 5, 5, 12, -10, 5};
for(int i = 0; i < sizeof(input)/sizeof(int); ++i)
{
insert_listvector(listvec, input[i]);
if(!is_listvector_sorted(listvec))
{
cout << "*** ERROR *** TEST FAILED ***" << endl;
return;
}
}
}
cout << endl << "*** INSERT TEST #2 ***" << endl;
{
sorted_listvector<int> listvec(4);
int input[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for(int i = 0; i < sizeof(input)/sizeof(int); ++i)
{
insert_listvector(listvec,input[i]);
if(!is_listvector_sorted(listvec))
{
cout << "*** ERROR *** TEST FAILED ***" << endl;
return;
}
}
}
cout << endl << "*** INSERT TEST #3 ***" << endl;
{
sorted_listvector<int> listvec(4);
int input[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
for(int i = 0; i < sizeof(input)/sizeof(int); ++i)
{
insert_listvector(listvec,input[i]);
if(!is_listvector_sorted(listvec))
{
cout << "*** ERROR *** TEST FAILED ***" << endl;
return;
}
}
}
cout << "TEST PASSED" << endl;
}
void Test5()
{
for(int i = 0; i<10; ++i)
{
cout << "*** SORT TEST #" << i << " ***" << endl;
sorted_listvector<int> listvec(4);
for(int j = 0; j<24; ++j)
{
insert_listvector(listvec,rand()%10);
if(!is_listvector_sorted(listvec))
{
cout << "*** ERROR *** TEST FAILED ***" << endl;
return;
}
}
}
cout << "TEST PASSED" << endl;
}
void Test6()
{
cout << "*** FILL TEST ***" << endl;
const int count = 1000;
vector<int> input;
for(int j = 0; j<count; ++j)
{
size_t size = input.size();
size_t pos = size != 0 ? rand() % (size+1) : 0;
input.insert(input.end() - pos, j);
}
sorted_listvector<int> listvec(128);
for(int j = 0; j<count; ++j)
{
listvec.insert(input[j]);
}
print_listvector(listvec);
if(!is_listvector_sorted(listvec))
{
cout << "*** ERROR *** TEST FAILED ***" << endl;
return;
}
size_t total = listvec.size_total();
size_t capacity = listvec.capacity_total();
cout << "Fill ratio: " << total << "/" << capacity << " = " << (100.f*(float)total/(float)capacity) << "%%" << endl;
cout << "TEST PASSED" << endl;
}
void Test7()
{
cout << "*** SEARCH TEST ***" << endl;
{
sorted_listvector<int> listvec(4);
int input[] = {10, 1, 2, 11, 6, 5, 5, 12, -10, 5};
for(int i = 0; i < sizeof(input)/sizeof(int); ++i)
{
listvec.insert(input[i]);
if(!is_listvector_sorted(listvec))
{
cout << "*** ERROR *** TEST FAILED ***" << endl;
return;
}
}
print_listvector(listvec);
cout << "\tSearching existed values:" << endl;
int search[] = {-10, 1, 2, 5, 6, 10, 11, 12};
for(int i = 0; i < sizeof(search)/sizeof(int); ++i)
{
bool found = search_listvector(listvec, search[i]);
if(!found)
{
cout << "*** ERROR *** TEST FAILED ***" << endl;
return;
}
}
cout << "\tSearching missed values" << endl;
int missed[] = {-20, 3, 4, 7, 8, 9, 20};
for(int i = 0; i < sizeof(missed)/sizeof(int); ++i)
{
bool found = search_listvector(listvec, missed[i]);
if(found)
{
cout << "*** ERROR *** TEST FAILED ***" << endl;
return;
}
}
cout << "TEST PASSED" << endl;
}
}