libostd/test_runner.cc

166 lines
4.5 KiB
C++
Raw Normal View History

2017-04-24 18:18:10 +02:00
#include <atomic>
#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>
#include <ostd/filesystem.hh>
#include <ostd/environ.hh>
2017-04-24 18:18:10 +02:00
#include <ostd/thread_pool.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
#ifndef OSTD_DEFAULT_LIB
#define OSTD_DEFAULT_LIB "libostd.a"
#endif
static void write_test_src(file_stream &s, string_range modname) {
s.writefln(
"#define OSTD_BUILD_TESTS libostd_%s\n"
2017-04-24 18:18:10 +02:00
"\n"
"#include <ostd/unit_test.hh>\n"
"#include <ostd/%s.hh>\n"
"#include <ostd/io.hh>\n"
"\n"
"int main() {\n"
" auto [ succ, fail ] = ostd::test::run();\n"
" ostd::writeln(succ, \" \", fail);\n"
" return 0;\n"
"}",
modname, modname
);
}
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() {
/* configurable section */
2016-07-08 20:48:11 +02:00
auto compiler = env_get("CXX").value_or("c++");
2017-01-29 21:22:40 +01:00
auto cxxflags = "-std=c++1z -I. -Wall -Wextra -Wshadow -Wold-style-cast "
2016-03-15 23:28:43 +01:00
"-Wno-missing-braces"; /* clang false positive */
2016-07-08 20:48:11 +02:00
auto userflags = env_get("CXXFLAGS").value_or("");
/* do not change past this point */
2017-04-24 18:18:10 +02:00
std::atomic_int nsuccess = 0, nfailed = 0;
2017-04-24 18:18:10 +02:00
auto print_error = [&](string_range modname, string_range fmsg) {
write_padded(modname, 20);
writeln(COLOR_RED, COLOR_BOLD, '(', fmsg, ')', COLOR_END);
++nfailed;
};
2017-04-24 18:18:10 +02:00
ostd::thread_pool tpool;
tpool.start(std::thread::hardware_concurrency());
filesystem::directory_iterator ds{"ostd"};
for (auto &v: ds) {
auto p = filesystem::path{v};
if (
(!filesystem::is_regular_file(p)) ||
2017-04-24 18:18:10 +02:00
(p.extension().string() != ".hh")
) {
continue;
}
2017-04-24 18:18:10 +02:00
tpool.push([&, modname = p.stem().string()]() {
2016-07-09 01:48:29 +02:00
#ifdef OSTD_PLATFORM_WIN32
2017-04-24 18:18:10 +02:00
std::string exepath = ".\\test_";
exepath += modname;
exepath += ".exe";
#else
std::string exepath = "./test_";
exepath += modname;
2016-07-09 01:48:29 +02:00
#endif
2017-04-24 18:18:10 +02:00
auto cxxcmd = appender<std::string>();
format(cxxcmd, "%s -o %s %s", compiler, exepath, cxxflags);
if (!userflags.empty()) {
cxxcmd.get() += ' ';
cxxcmd.get() += userflags;
}
cxxcmd.get() += " -xc++ - -xnone ";
cxxcmd.get() += OSTD_DEFAULT_LIB;
/* compile */
auto cf = popen(cxxcmd.get().data(), "w");
if (!cf) {
print_error(modname, "compile errror");
return;
}
file_stream cfs{cf};
write_test_src(cfs, modname);
if (pclose(cf)) {
print_error(modname, "compile errror");
return;
}
/* run */
auto rf = popen(exepath.data(), "r");
if (!rf) {
print_error(modname, "runtime error");
return;
}
int succ = 0, fail = 0;
if (fscanf(rf, "%d %d", &succ, &fail) != 2) {
print_error(modname, "runtime error");
}
if (pclose(rf)) {
print_error(modname, "runtime error");
return;
}
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
);
remove(exepath.data());
2017-04-28 01:54:28 +02:00
if (fail) {
++nfailed;
} else {
++nsuccess;
}
2017-04-24 18:18:10 +02:00
});
}
2017-04-24 18:18:10 +02:00
/* wait for tasks */
tpool.destroy();
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);
}