-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.cpp
86 lines (79 loc) · 1.87 KB
/
tests.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
//
// Created by mguzek on 6/7/20.
//
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include "HashSet.h"
//Very basic unit tests
void threadJob(HashSet* set, int id)
{
for(int i = 0; i < 10; i++) {
set->add(id + 1);
std::this_thread::sleep_for (std::chrono::milliseconds(100));
if(set->contains(id + 1))
set->remove(id + 1);
}
}
bool testConcurrent(HashSet* set) {
std::vector<std::thread> v(8);
int i = 0;
for(auto& t: v) {
t = std::thread([set, i]{
threadJob(set, i);
});
i++;
}
for(auto& t: v) {
t.join();
}
for(int i = 8; i > 0; i--)
if(set->contains(i))
return false;
return true;
}
bool testSequential(HashSet* set) {
for(int i = 100; i > 0; i--)
{
if(!set->add(i))
return false;
}
if(set->size() != 100)
return false;
for(int i = 100; i > 0; i--)
{
if(set->add(i))
return false;
}
for(int i = 100; i > 0; i--)
if(!set->contains(i))
return false;
for(int i = 95; i > 0; i--)
if(!set->remove(i))
return false;
if(set->size() != 5)
return false;
for(int i = 95; i > 0; i--)
if(set->remove(i))
return false;
if(set->remove(101))
return false;
if(set->size() != 5)
return false;
if(set->remove(-1))
return false;
if(set->size() != 5)
return false;
if(!set->remove(99))
return false;
if(set->size() != 4)
return false;
return true;
}
void test(HashSet* set) {
std::cout << "Test sequential... ";
std::cout << (testSequential(set) ? "OK" : "FAIL") << std::endl << std::flush;
std::cout << "Test concurrent... ";
std::cout << (testConcurrent(set) ? "OK" : "FAIL") << std::endl << std::flush;
}