-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpcustomtest.cc
108 lines (94 loc) · 2.49 KB
/
tpcustomtest.cc
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
/**
* File: tpcustomtest.cc
* ---------------------
* Unit tests *you* write to exercise the ThreadPool in a variety
* of ways.
*/
#include <iostream>
#include <sstream>
#include <map>
#include <string>
#include <functional>
#include <cstring>
#include <sys/types.h> // used to count the number of threads
#include <unistd.h> // used to count the number of threads
#include <dirent.h> // for opendir, readdir, closedir
#include "thread-pool.h"
#include "thread-utils.h"
#include "ostreamlock.h"
using namespace std;
static void singleThreadNoWaitTest() {
ThreadPool pool(4);
pool.schedule([] {
cout << "This is a test." << endl;
});
sleep_for(1000); // emulate wait without actually calling wait (that's a different test)
}
static void singleThreadSingleWaitTest() {
ThreadPool pool(4);
pool.schedule([] {
cout << "This is a test." << endl;
sleep_for(1000);
});
}
static void noThreadsDoubleWaitTest() {
ThreadPool pool(4);
pool.wait();
pool.wait();
}
static void reuseThreadPoolTest() {
ThreadPool pool(4);
for (size_t i = 0; i < 16; i++) {
pool.schedule([] {
cout << "This is a test." << endl;
sleep_for(50);
});
}
pool.wait();
pool.schedule([] {
cout << "This is a code." << endl;
sleep_for(1000);
});
pool.wait();
}
struct testEntry {
string flag;
function<void(void)> testfn;
};
static void buildMap(map<string, function<void(void)>>& testFunctionMap) {
testEntry entries[] = {
{"--single-thread-no-wait", singleThreadNoWaitTest},
{"--single-thread-single-wait", singleThreadSingleWaitTest},
{"--no-threads-double-wait", noThreadsDoubleWaitTest},
{"--reuse-thread-pool", reuseThreadPoolTest},
};
for (const testEntry& entry: entries) {
testFunctionMap[entry.flag] = entry.testfn;
}
}
static void executeAll(const map<string, function<void(void)>>& testFunctionMap) {
for (const auto& entry: testFunctionMap) {
cout << entry.first << ":" << endl;
entry.second();
}
}
int main(int argc, char **argv) {
if (argc != 2) {
cout << "Ouch! I need exactly two arguments." << endl;
return 0;
}
map<string, function<void(void)>> testFunctionMap;
buildMap(testFunctionMap);
string flag = argv[1];
if (flag == "--all") {
executeAll(testFunctionMap);
return 0;
}
auto found = testFunctionMap.find(argv[1]);
if (found == testFunctionMap.end()) {
cout << "Oops... we don't recognize the flag \"" << argv[1] << "\"." << endl;
return 0;
}
found->second();
return 0;
}