testing.cpp
2.96 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
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
#include "testing.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
namespace testing {
namespace {
int passes{0};
int failures{0};
} // namespace
static void BitBucket(const char *, ...) {}
static void PrintFailureDetails(const char *format, ...) {
va_list ap;
va_start(ap, format);
fputs("\t", stderr);
vfprintf(stderr, format, ap);
va_end(ap);
fputc('\n', stderr);
}
FailureDetailPrinter Test(
const char *file, int line, const char *predicate, bool pass) {
if (pass) {
++passes;
return BitBucket;
} else {
++failures;
fprintf(stderr, "%s:%d: FAIL: %s\n", file, line, predicate);
return PrintFailureDetails;
}
}
FailureDetailPrinter Match(const char *file, int line, std::uint64_t want,
const char *gots, std::uint64_t got) {
if (want == got) {
++passes;
return BitBucket;
} else {
++failures;
fprintf(stderr, "%s:%d: FAIL: %s == 0x%jx, not 0x%jx\n", file, line, gots,
static_cast<std::uintmax_t>(got), static_cast<std::uintmax_t>(want));
return PrintFailureDetails;
}
}
FailureDetailPrinter Match(const char *file, int line, const char *want,
const char *gots, const std::string &got) {
if (want == got) {
++passes;
return BitBucket;
} else {
++failures;
fprintf(stderr, "%s:%d: FAIL: %s == \"%s\", not \"%s\"\n", file, line, gots,
got.data(), want);
return PrintFailureDetails;
}
}
FailureDetailPrinter Match(const char *file, int line, const std::string &want,
const char *gots, const std::string &got) {
return Match(file, line, want.data(), gots, got);
}
FailureDetailPrinter Compare(const char *file, int line, const char *xs,
const char *rel, const char *ys, std::uint64_t x, std::uint64_t y) {
while (*rel == ' ') {
++rel;
}
bool pass{false};
if (*rel == '<') {
if (rel[1] == '=') {
pass = x <= y;
} else {
pass = x < y;
}
} else if (*rel == '>') {
if (rel[1] == '=') {
pass = x >= y;
} else {
pass = x > y;
}
} else if (*rel == '=') {
pass = x == y;
} else if (*rel == '!') {
pass = x != y;
}
if (pass) {
++passes;
return BitBucket;
} else {
++failures;
fprintf(stderr, "%s:%d: FAIL: %s[0x%jx] %s %s[0x%jx]\n", file, line, xs,
static_cast<std::uintmax_t>(x), rel, ys,
static_cast<std::uintmax_t>(y));
return PrintFailureDetails;
}
}
int Complete() {
if (failures == 0) {
if (passes == 1) {
llvm::outs() << "single test PASSES\n";
} else {
llvm::outs() << "all " << passes << " tests PASS\n";
}
passes = 0;
return EXIT_SUCCESS;
} else {
if (passes == 1) {
llvm::errs() << "1 test passes, ";
} else {
llvm::errs() << passes << " tests pass, ";
}
if (failures == 1) {
llvm::errs() << "1 test FAILS\n";
} else {
llvm::errs() << failures << " tests FAIL\n";
}
passes = failures = 0;
return EXIT_FAILURE;
}
}
} // namespace testing