peter klausler a39e9cf6be [flang] Basic tests of external I/O runtime (part 9/9)
Add new unit tests for external Fortran I/O that drive the
Fortran I/O runtime API from C++ and exercise basic writing
and read-back in the various combinations of access modes,
record length variability, and formatting.  Sequential modes
are tested with positioning.  More thorough tests written in
Fortran will follow when they can be compiled and run.

The Fortran runtime's error termination callback registration
was extended with source file and line number positions for
better failure messages in unit testing.

Reviewed By: sscalpone

Differential Revision: https://reviews.llvm.org/D83164
2020-07-06 10:37:13 -07:00

47 lines
1.1 KiB
C++

#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);
}
}