-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.assert.h
173 lines (154 loc) · 7.13 KB
/
test.assert.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
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
// test::assert.h
#ifndef TEST_ASSERT_H
#define TEST_ASSERT_H
#include <iostream>
#include <stdexcept> // For std::runtime_error
#include <algorithm> // For std::replace
#include "fmt\full.h"
namespace test {
static int test_idx = 1;
static int test_failed = 0;
void testsFn();
static std::string test_name = "";
static int test_name_count_old = 0;
static int test_name_count = 0;
std::string replace_new_line(const std::string& str) {
std::string result = str; // Copy the original string to a modifiable variable
size_t pos = 0;
while ((pos = result.find('\n', pos)) != std::string::npos) {
result.replace(pos, 1, "\\n");
pos += 2; // Skip over the newly inserted "\\n"
}
return result;
}
class Tin {
std::stringstream buffer;
std::streambuf* old_cin;
public:
Tin(): old_cin(nullptr) {}
// ~Tin() {
// if(old_cin) std::cin.rdbuf(old_cin);
// }
void init(){ // overwrite std::cin
if(!old_cin) {
old_cin = std::cin.rdbuf();
std::cin.rdbuf(buffer.rdbuf());
}
}
void destroy(){ // reset std::cin
if(old_cin) std::cin.rdbuf(old_cin);
old_cin = nullptr;
}
template<typename... Args>
void set(const Args&... args) {
((buffer << args << '\n'), ...);
// init();
}
void clear() {
buffer.str("");
buffer.clear();
// destroy();
}
};
class Tout {
std::stringstream buffer;
std::streambuf* old_cout;
public:
Tout(): old_cout(nullptr) {}
// ~Tout() {
// if(old_cout) std::cout.rdbuf(old_cout);
// }
void init(){ // overwrite std::cout
if(!old_cout) {
old_cout = std::cout.rdbuf();
std::cout.rdbuf(buffer.rdbuf());
}
}
void destroy(){ // reset std::cout
if(old_cout) std::cout.rdbuf(old_cout);
old_cout = nullptr;
}
std::string get() const {
return buffer.str();
}
void clear() {
buffer.str("");
buffer.clear();
// destroy();
}
void assert_eq(const std::string _x, const std::string x_name){
this->destroy();
std::string x = replace_new_line(_x);
std::string y = replace_new_line(this->get());
int i = test::test_idx++;
if (test_name_count == test_name_count_old) test_name = fmt::print.sprint("test case ", i);
else test_name_count_old++;
bool pass = x.length() == y.length();
for (int i = 0; i < x.size(); i++){
if(!pass) break;
pass = x == y;
}
if(pass) fmt::println(ansi::green, "test ", i, " passed...\t", ansi::white,"[", x_name, ":`", x, "` == ", "tout",":`", y,"`]",
ansi::bg_black, ansi::blue, "\t\tIt(", test::test_name, ")" , ansi::reset);
else {
test::test_failed++;
fmt::println(ansi::red, "test ", i, " failed!!!",
"\t[", x_name, ":`", x, "` != ", "tout",":`", y,"`]", ansi::blue,
ansi::bg_black, "\t\tIt(", test::test_name, ")" , ansi::reset);
}
this->init();
}
};
Tin _tin;
Tout _tout;
void print_tests(){
fmt::println(
ansi::underline, ansi::cyan, "Result", ansi::reset, " :\n\t",
ansi::green, test::test_idx-test::test_failed-1, " tests passed\n\t",
ansi::red, test::test_failed, " tests failed", ansi::reset
);
}
template <typename T, typename U>
void assert_eq(const T &x, const U &y, std::string x_name, std::string y_name){
test::_tout.destroy();
int i = test::test_idx++;
if (test_name_count == test_name_count_old) test_name = fmt::print.sprint("test case ", i);
else test_name_count_old++;
std::string _x = replace_new_line(fmt::print.sprint(x));
std::string _y = replace_new_line(fmt::print.sprint(y));
if(x == y) fmt::println(ansi::green, "test ", i, " passed...\t", ansi::white,"[", x_name, ":`", _x, "` == ", y_name,":`", _y,"`]",
ansi::bg_black, ansi::blue, "\t\tIt(", test::test_name, ")" , ansi::reset);
else {
test::test_failed++;
fmt::println(ansi::red, "test ", i, " failed!!!",
"\t[", x_name, ":`", _x, "` != ", y_name,":`", _y,"`]", ansi::blue,
ansi::bg_black, "\t\tIt(", test::test_name, ")" , ansi::reset);
}
test::_tout.init();
}
void assert_true(const bool &b, std::string b_name){
test::_tout.destroy();
int i = test::test_idx++;
if (test_name_count == test_name_count_old) test_name = fmt::print.sprint("test case ", i);
else test_name_count_old++;
if(b) fmt::println(ansi::green, "test ", i, " passed...\t", ansi::white,"[", b_name, ":`", b, "` == ", "True]",
ansi::bg_black, ansi::blue, "\t\tIt(", test::test_name, ")" , ansi::reset);
else {
test::test_failed++;
fmt::println(ansi::red, "test ", i, " failed!!!",
"\t[", b_name, ":`", b, "` != True]", ansi::blue,
ansi::bg_black, "\t\tIt(", test::test_name, ")" , ansi::reset);
}
test::_tout.init();
}
}
#define assert(b) test::assert_true((bool)b, #b)
#define asserteq(x, y) test::assert_eq(x, y, #x, #y)
#define asserttout(b) test::_tout.assert_eq(b, #b)
#define debug(x) std::cerr << "Debugging information: " << #x << " = " << fmt::print.sprint(x) << std::endl
#define MAKE_TESTS void test::testsFn()
#define RUN_TESTS test::_tin.init(); test::_tout.init(); test::testsFn(); test::_tin.clear(); test::_tout.clear(); test::_tin.destroy(); test::_tout.destroy(); test::print_tests();
#define It(...) test::test_name_count++; test::test_name = #__VA_ARGS__; test::_tout.clear(); if(true)
#define tout test::_tout
#define tin test::_tin
#endif