-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
97 lines (78 loc) · 2.28 KB
/
test.cpp
File metadata and controls
97 lines (78 loc) · 2.28 KB
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
#include <csetjmp>
jmp_buf jump_buf;
#include <iostream>
#include <optional>
#include <functional>
#ifndef __WIN64__
#include "assign2.hpp"
#else
#include "assign2.cpp"
#endif
std::function<void ()> run_next;
template <int x>
std::optional<std::string> test();
#include "test.hpp"
extern thread_local std::unordered_set<void *> records;
template <int x>
void run();
template <>
void run<test_number>() {
std::cout << "本次测试到此结束。" << std::endl
<< "====================" << std::endl << std::endl;
}
template <int v>
void run() {
std::cout << "正在进行第 " << v << " 组测试:" << std::endl;
if (!setjmp(jump_buf)) {
try {
auto r (test<v>());
if (!r && records.empty() && bst_record.empty())
std::cout << "测试通过!" << std::endl;
else if (r)
std::cout << "测试失败,错误原因:" << *r << std::endl;
else
std::cout << "测试失败,错误原因:内存泄漏。" << std::endl;
} catch (std::string const &cause) {
std::cout << "测试异常,原因:" << cause << std::endl;
} catch (std::exception const &e) {
std::cout << "测试内抛出异常,原因:" << e.what() << std::endl;
}
std::cout << "====================" << std::endl << std::endl;
}
for (auto p: records)
free(p);
records.clear();
for (auto p: bst_record)
free(p);
bst_record.clear();
run<v+1>();
}
#include <cstdlib>
#include <cstdio>
#include <csignal>
#include <cstring>
#ifndef __WIN64__
void segfault_sigaction(int signal, siginfo_t *si, void *arg) {
#else
void segfault_sigaction(int s) {
#endif
std::cout << "测试异常:Segmentation Fault! " << std::endl
<< "====================" << std::endl << std::endl;
longjmp(jump_buf, 0);
}
void register_seg_fault() {
#ifndef __WIN64__
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = segfault_sigaction;
sa.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
#else
signal(SIGSEGV, segfault_sigaction);
#endif
}
int main() {
register_seg_fault();
run<0>();
}