forked from ProgrammingCCC/blackjack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
67 lines (58 loc) · 1.68 KB
/
utils.h
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
#pragma once
#include <functional>
#include <iostream>
#include <map>
#include <string>
static bool failed = false;
template <class T>
constexpr void assert_equal(const T &expected, const T &testVal) {
if (expected != testVal) {
std::cout << "Result\tError\tExpected: " << expected << " actual "
<< testVal << std::endl;
failed = true;
} else {
std::cout << "Result\tOK" << std::endl;
}
}
class AbstractTestHarness {
private:
std::map<std::string, std::function<void()>> testFuncs;
protected:
void register_test_func(const std::string &&name,
std::function<void()> &&func) {
this->testFuncs.insert(
std::pair<std::string, std::function<void()>>(name, func));
}
public:
AbstractTestHarness() {
this->testFuncs = std::map<std::string, std::function<void()>>();
}
void execute() {
std::cout << "Found " << testFuncs.size() << " Tests" << std::endl;
for (auto f : testFuncs) {
std::cout << "Executing Test " << f.first << std::endl;
f.second();
}
}
};
class TestManager {
private:
std::map<std::string, AbstractTestHarness> tests;
protected:
void add_test(const std::string &&name, AbstractTestHarness &&test) {
this->tests.insert(std::pair<std::string, AbstractTestHarness>(name, test));
}
public:
TestManager() { this->tests = std::map<std::string, AbstractTestHarness>(); }
void execute() {
std::cout << "Found " << tests.size() << " Test Harnesses" << std::endl;
for (auto t : tests) {
std::cout << "Executing Test Harness " << t.first << std::endl;
t.second.execute();
}
if (failed) {
std::cout << "tests failed" << std::endl;
exit(1);
}
}
};