-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassertion.h
54 lines (49 loc) · 1.84 KB
/
assertion.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
#pragma once
#include <vector>
#include <unordered_map>
#include <string>
#include <mutex>
#include <chrono>
#include <tuple>
namespace kaiu {
class Assertions {
public:
enum result { unknown, skipped, passed, failed };
Assertions() = delete;
Assertions(const Assertions&) = delete;
Assertions(const std::vector<std::pair<const char *, const char *>>& strings);
~Assertions();
void set(const std::string& code, const result state, const std::string& note = "");
void pass(const std::string& code, const std::string& note = "");
void fail(const std::string& code, const std::string& note = "");
void skip(const std::string& code, const std::string& note = "");
void try_pass(const std::string& code, const std::string& note = "");
template <typename T, typename U>
void expect(const T& t, const U& u, const std::string& assertion, const std::string& note = "");
int print(bool always);
int print(const int argc, char const * const argv[]);
void print_error();
private:
using ensure_locked = const std::lock_guard<std::mutex>&;
std::chrono::time_point<std::chrono::steady_clock> start_time{std::chrono::steady_clock::now()};
std::mutex mx;
bool printed;
const std::vector<std::pair<const char *, const char *>> strings;
std::unordered_map<std::string, std::pair<result, std::string>> list;
int _print(ensure_locked, bool always);
void _set(ensure_locked, const std::string& code, const result state, const std::string& note);
std::pair<result, std::string>& _get(ensure_locked, const std::string& code);
};
template <typename Actual, typename Expect>
void Assertions::expect(const Actual& actual, const Expect& expect, const std::string& assertion, const std::string& note)
{
if (actual == expect) {
pass(assertion, note);
} else {
fail(assertion, note);
// cerr
// << " Expect: " << expect << endl
// << " Actual: " << actual << endl;
}
}
}