testing.cpp
1.12 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
#include "testing.h"
#include "../../runtime/terminator.h"
#include <algorithm>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <string>
static int failures{0};
// Override the Fortran runtime's Crash() for testing purposes
[[noreturn]] static void CatchCrash(
const char *sourceFile, int sourceLine, const char *message, va_list &ap) {
char buffer[1000];
std::vsnprintf(buffer, sizeof buffer, message, ap);
va_end(ap);
llvm::errs() << (sourceFile ? sourceFile : "unknown source file") << '('
<< sourceLine << "): CRASH: " << buffer << '\n';
throw std::string{buffer};
}
void StartTests() {
Fortran::runtime::Terminator::RegisterCrashHandler(CatchCrash);
}
llvm::raw_ostream &Fail() {
++failures;
return llvm::errs();
}
int EndTests() {
if (failures == 0) {
llvm::outs() << "PASS\n";
} else {
llvm::outs() << "FAIL " << failures << " tests\n";
}
return failures != 0;
}
void SetCharacter(char *to, std::size_t n, const char *from) {
auto len{std::strlen(from)};
std::memcpy(to, from, std::min(len, n));
if (len < n) {
std::memset(to + len, ' ', n - len);
}
}