libostd/tests/test_runner.cc

108 lines
2.5 KiB
C++
Raw Normal View History

#include <cstddef>
#include <unordered_map>
#include <ostd/platform.hh>
2017-04-24 18:18:10 +02:00
#include <ostd/range.hh>
#include <ostd/format.hh>
#include <ostd/io.hh>
#include <ostd/string.hh>
2018-04-18 03:57:22 +02:00
#include <ostd/path.hh>
using namespace ostd;
#ifndef OSTD_PLATFORM_WIN32
2017-02-25 14:56:51 +01:00
constexpr auto COLOR_RED = "\033[91m";
constexpr auto COLOR_GREEN = "\033[92m";
constexpr auto COLOR_BLUE = "\033[94m";
constexpr auto COLOR_BOLD = "\033[1m";
constexpr auto COLOR_END = "\033[0m";
#else
2017-02-25 14:56:51 +01:00
constexpr auto COLOR_RED = "";
constexpr auto COLOR_GREEN = "";
constexpr auto COLOR_BLUE = "";
constexpr auto COLOR_BOLD = "";
constexpr auto COLOR_END = "";
2017-04-24 18:18:10 +02:00
#define popen _popen
#endif
2017-04-24 18:18:10 +02:00
static void write_padded(string_range s, std::size_t n) {
write(s, "...");
if ((s.size() + 3) >= n) {
return;
}
for (size_t i = 0; i < (n - (s.size() + 3)); ++i) {
write(' ');
}
}
int main(int argc, char **argv) {
/* configurable section */
char const *testdir = "tests";
if (argc > 1) {
testdir = argv[1];
}
/* do not change past this point */
std::size_t nsuccess = 0, nfailed = 0;
auto print_error = [&](string_range modname) {
2017-04-24 18:18:10 +02:00
write_padded(modname, 20);
writeln(COLOR_RED, COLOR_BOLD, "(runtime error)", COLOR_END);
++nfailed;
};
2018-04-18 03:57:22 +02:00
fs::directory_range dr{testdir};
for (auto &v: dr) {
if (!v.is_regular_file()) {
continue;
}
2018-04-18 03:57:22 +02:00
auto p = v.path();
2016-07-09 01:48:29 +02:00
#ifdef OSTD_PLATFORM_WIN32
2018-04-18 03:57:22 +02:00
if (p.suffix().string() != ".exe")
2017-04-24 18:18:10 +02:00
#else
2018-04-18 03:57:22 +02:00
if (p.has_suffix())
2016-07-09 01:48:29 +02:00
#endif
{
continue;
}
2018-04-18 03:57:22 +02:00
auto modname = p.stem();
auto exepath = p.string();
auto rf = popen(exepath.data(), "r");
if (!rf) {
print_error(modname);
continue;
}
2017-04-24 18:18:10 +02:00
int succ = 0, fail = 0;
if (fscanf(rf, "%d %d", &succ, &fail) != 2) {
pclose(rf);
print_error(modname);
continue;
}
2017-04-24 18:18:10 +02:00
if (pclose(rf)) {
print_error(modname);
continue;
}
2017-04-24 18:18:10 +02:00
write_padded(modname, 20);
writefln(
"%s%s%d out of %d%s (%d failures)",
fail ? COLOR_RED : COLOR_GREEN, COLOR_BOLD,
succ, succ + fail, COLOR_END, fail
);
if (fail) {
++nfailed;
} else {
++nsuccess;
}
}
2017-02-25 14:56:51 +01:00
writeln("\n", COLOR_BLUE, COLOR_BOLD, "testing done:", COLOR_END);
2017-04-24 18:18:10 +02:00
writeln(COLOR_GREEN, "SUCCESS: ", int(nsuccess), COLOR_END);
writeln(COLOR_RED, "FAILURE: ", int(nfailed), COLOR_END);
}