-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
474 lines (412 loc) · 12.2 KB
/
main.cpp
File metadata and controls
474 lines (412 loc) · 12.2 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <iostream>
#include <gtest/gtest.h>
#include "imergeable_heap.h"
#include "binomial_heap.h"
#include "simple_heap.h"
#include "skew_heap.h"
#include "leftist_heap.h"
#include <chrono>
#include <random>
#include <vector>
#include <algorithm>
const int SMALL_TEST = 10;
const int MEDIUM_TEST = 1e4;
const int LARGE_TEST = 1e5;
const int EXTRA_LARGE_TEST = 5e6;
const int MAX_TEST = 1e8;
class HeapsTest : public ::testing::Test
{
protected:
virtual void SetUp()
{
engine.seed(179);
}
virtual void TearDown()
{
for (auto &ptr : binomialHeaps) {
delete ptr;
}
for (auto &ptr : simpleHeaps) {
delete ptr;
}
for (auto &ptr : skewHeaps) {
delete ptr;
}
for (auto &ptr : leftistHeaps) {
delete ptr;
}
binomialHeaps.clear();
simpleHeaps.clear();
skewHeaps.clear();
leftistHeaps.clear();
}
void AddHeap(int key)
{
binomialHeaps.push_back(new BinomialHeap(key));
skewHeaps.push_back(new SkewHeap(key));
simpleHeaps.push_back(new SimpleHeap(key));
leftistHeaps.push_back(new LeftistHeap(key));
}
void Insert(int index, int key)
{
binomialHeaps[index]->insert(key);
simpleHeaps[index]->insert(key);
skewHeaps[index]->insert(key);
leftistHeaps[index]->insert(key);
}
int ExtractMin(int index)
{
int m0 = simpleHeaps[index]->extractMin();
int m1 = binomialHeaps[index]->extractMin();
int m2 = skewHeaps[index]->extractMin();
int m3 = leftistHeaps[index]->extractMin();
EXPECT_EQ(m0, m1) << "in binomial heap #" << index << "; bad minimum extracted";
EXPECT_EQ(m0, m2) << "in skew heap #" << index << "; bad minimum extracted";
EXPECT_EQ(m0, m3) << "in leftist heap #" << index << "; bad minimum extracted";
return m0;
}
bool Empty(int index) const
{
return simpleHeaps[index]->empty();
}
bool Empty() const
{
return simpleHeaps.empty();
}
size_t Size() const
{
return simpleHeaps.size();
}
void TestSize(int index) const
{
size_t s0 = simpleHeaps[index]->size();
size_t s1 = binomialHeaps[index]->size();
size_t s2 = skewHeaps[index]->size();
size_t s3 = leftistHeaps[index]->size();
EXPECT_EQ(s0, s1) << "in binomial heap #" << index << "; bad size";
EXPECT_EQ(s0, s2) << "in skew heap #" << index << "; bad size";
EXPECT_EQ(s0, s3) << "in leftist heap #" << index << "; bad size";
}
void TestEmpty(int index) const
{
bool e0 = simpleHeaps[index]->empty();
bool e1 = binomialHeaps[index]->empty();
bool e2 = skewHeaps[index]->empty();
bool e3 = leftistHeaps[index]->empty();
EXPECT_EQ(e0, e1) << "in binomial heap #" << index
<< "; error while getting empty;";
EXPECT_EQ(e0, e2) << "in skew heap #" << index
<< "; error while getting empty;";
EXPECT_EQ(e0, e3) << "in leftist heap #" << index
<< "; error while getting empty;";
}
void Meld(int index1, int index2)
{
binomialHeaps[index1]->meld(*binomialHeaps[index2]);
std::swap(binomialHeaps[index2], binomialHeaps.back());
simpleHeaps[index1]->meld(*simpleHeaps[index2]);
std::swap(simpleHeaps[index2], simpleHeaps.back());
leftistHeaps[index1]->meld(*leftistHeaps[index2]);
std::swap(leftistHeaps[index2], leftistHeaps.back());
skewHeaps[index1]->meld(*skewHeaps[index2]);
std::swap(skewHeaps[index2], skewHeaps.back());
/*skewHeaps[index1]->print();
std::cout << "______________\n";
leftistHeaps[index1]->print();
std::cout << "leftist______________\n";
std::cout << "______________\n";*/
}
void TestAdd()
{
int key = random(engine) % 100;
AddHeap(key);
}
void TestInsert()
{
int index = random(engine) % Size();
int key = random(engine) % 100;
Insert(index, key);
TestEmpty(index);
TestSize(index);
}
void TestExtractMin()
{
int index = random(engine) % Size();
if (!Empty(index))
ExtractMin(index);
TestEmpty(index);
TestSize(index);
}
void TestMeld()
{
int index1 = random(engine) % Size();
int index2 = random(engine) % (Size() - 1);
if (index2 == index1)
index2 = Size() - 1;
Meld(index1, index2);
TestEmpty(index1);
TestSize(index1);
}
void MakeAllEmpty()
{
for (size_t i = 0; i < Size(); ++i) {
while (!Empty(i)) {
ExtractMin(i);
}
TestSize(i);
TestEmpty(i);
}
}
void TestCorrect(int toTest)
{
std::cout << "starting testing heaps correctly with "
<< toTest << " operations" << std::endl;
for (int i = 0; i < toTest; ++i)
{
//std::cout << i << std::endl;
int way = random(engine) % 4;
//std::cout << way << std::endl;
if (way == 0) {
TestAdd();
} else if (!Empty() && way == 1) {
TestInsert();
} else if (!Empty() && way == 2) {
TestExtractMin();
} else if (Size() > 1 && way == 3) {
TestMeld();
} else {
--i;
}
}
MakeAllEmpty();
}
public:
std::vector<BinomialHeap *> binomialHeaps;
std::vector<SkewHeap *> skewHeaps;
std::vector<LeftistHeap *> leftistHeaps;
std::vector<SimpleHeap *> simpleHeaps;
std::uniform_int_distribution<int> random;
std::default_random_engine engine;
};
class HeapPerfomanceTest : public ::testing::Test
{
protected:
typedef IMergeableHeap<int> * HeapPtr;
virtual void SetUp()
{
engine.seed(179);
}
virtual void TearDown()
{
for (auto &ptr : heaps) {
delete ptr;
}
heaps.clear();
}
void Insert(int index, int key)
{
heaps[index]->insert(key);
}
int ExtractMin(int index)
{
return heaps[index]->extractMin();
}
bool Empty(int index) const
{
return heaps[index]->empty();
}
bool Empty() const
{
return heaps.empty();
}
size_t Size() const
{
return heaps.size();
}
void Meld(int index1, int index2)
{
heaps[index1]->meld(*heaps[index2]);
std::swap(heaps[index2], heaps.back());
/*skewHeaps[index1]->print();
std::cout << "______________\n";
leftistHeaps[index1]->print();
std::cout << "leftist______________\n";
std::cout << "______________\n";*/
}
void TestInsert()
{
int index = random(engine) % Size();
int key = random(engine) % 100;
Insert(index, key);
}
void TestExtractMin()
{
int index = random(engine) % Size();
if (!Empty(index))
ExtractMin(index);
}
void TestMeld()
{
int index1 = random(engine) % Size();
int index2 = random(engine) % (Size() - 1);
if (index2 == index1)
index2 = Size() - 1;
Meld(index1, index2);
}
void MakeAllEmpty()
{
for (size_t i = 0; i < Size(); ++i) {
while (!Empty(i)) {
ExtractMin(i);
}
}
}
virtual void TestTimeHeap(std::vector<HeapPtr> &heapsEmpty, int operationsNumber)
{
heaps.swap(heapsEmpty);
std::cout << "starting testing current heap woriking time with "
<< operationsNumber << " operations" << std::endl;
for (int i = 0; i < operationsNumber; ++i)
{
//std::cout << i << std::endl;
int way = random(engine) % 3;
//std::cout << way << std::endl;
if (!Empty() && way == 0) {
TestInsert();
} else if (!Empty() && way == 1) {
TestExtractMin();
} else if (Size() > 1 && way == 2) {
TestMeld();
} else {
--i;
}
}
MakeAllEmpty();
}
public:
std::vector<HeapPtr> heaps;
std::uniform_int_distribution<int> random;
std::default_random_engine engine;
};
class HeapCompareWithSortTest : public ::testing::Test
{
protected:
typedef IMergeableHeap<int> * HeapPtr;
virtual void SetUp()
{
engine.seed(179);
}
virtual void TearDown()
{
}
double TestStdSort(std::vector <int> &toSort)
{
auto start(std::chrono::steady_clock::now());
std::sort(toSort.begin(), toSort.end());
auto end(std::chrono::steady_clock::now());
return std::chrono::duration_cast<std::chrono::duration<double> > (end - start).count();
}
double TestHeapSort(HeapPtr heap, std::vector <int> toSort)
{
auto start(std::chrono::steady_clock::now());
for (int elem : toSort) {
heap->insert(elem);
}
for (auto &elem : toSort) {
elem = heap->extractMin();
}
auto end(std::chrono::steady_clock::now());
return std::chrono::duration_cast<std::chrono::duration<double> > (end - start).count();
}
void TestWith(HeapPtr heap, size_t size)
{
std::vector <int> array(size);
generate(array.begin(), array.end(), [this] () { return random(engine); } );
std::vector <int> toHeap(array);
std::cout << "start sorting" << std::endl;
double d1 = TestStdSort(array);
std::cout << "sorting time with std::sort is " << d1 << " seconds" << std::endl;
std::cout << "start sorting" << std::endl;
double d2 = TestHeapSort(heap, toHeap);
std::cout << "sorting time with heap is " << d2 << " seconds" << std::endl;
std::cout << "relative is " << d2 / d1 << std::endl;
}
public:
std::uniform_int_distribution<int> random;
std::default_random_engine engine;
};
TEST_F(HeapsTest, TestCorrectSmall)
{
TestCorrect(SMALL_TEST);
}
TEST_F(HeapsTest, TestCorrectMedium)
{
TestCorrect(MEDIUM_TEST);
}
TEST_F(HeapPerfomanceTest, TestSkewLarge)
{
int testSize = EXTRA_LARGE_TEST;
int n = (int)log((float)testSize);
std::vector <IMergeableHeap<int> *> heaps(n, nullptr);
generate(heaps.begin(), heaps.end(), [] () { return new SkewHeap(); });
TestTimeHeap(heaps, testSize);
}
TEST_F(HeapPerfomanceTest, TestBinomialLarge)
{
int testSize = EXTRA_LARGE_TEST;
int n = (int)log((float)testSize);
std::vector <IMergeableHeap<int> *> heaps(n, nullptr);
generate(heaps.begin(), heaps.end(), [] () { return new BinomialHeap(); });
TestTimeHeap(heaps, testSize);
}
TEST_F(HeapPerfomanceTest, TestLeftistHeap)
{
int testSize = EXTRA_LARGE_TEST;
int n = (int)log((float)testSize);
std::vector <IMergeableHeap<int> *> heaps(n, nullptr);
generate(heaps.begin(), heaps.end(), [] () { return new LeftistHeap(); });
TestTimeHeap(heaps, testSize);
}
TEST_F(HeapPerfomanceTest, TestSimpleHeap)
{
int testSize = 1e4;
int n = (int)log((float)testSize);
std::vector <IMergeableHeap<int> *> heaps(n, nullptr);
generate(heaps.begin(), heaps.end(), [] () { return new SimpleHeap(); });
TestTimeHeap(heaps, testSize);
}
TEST_F(HeapCompareWithSortTest, TestSimpleHeap)
{
int testSize = EXTRA_LARGE_TEST;
HeapPtr heap = new SimpleHeap();
TestWith(heap, testSize);
delete heap;
}
TEST_F(HeapCompareWithSortTest, TestBinomialHeap)
{
int testSize = EXTRA_LARGE_TEST;
HeapPtr heap = new BinomialHeap();
TestWith(heap, testSize);
delete heap;
}
TEST_F(HeapCompareWithSortTest, TestSkewHeap)
{
int testSize = EXTRA_LARGE_TEST;
HeapPtr heap = new SkewHeap();
TestWith(heap, testSize);
delete heap;
}
TEST_F(HeapCompareWithSortTest, TestLeftistHeap)
{
int testSize = EXTRA_LARGE_TEST;
HeapPtr heap = new LeftistHeap();
TestWith(heap, testSize);
delete heap;
}
int main(int argc, char **argv)
{
std::cerr.setf(std::cerr.fixed);
std::cerr.precision(9);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}