2017-04-24 16:18:10 +00:00
|
|
|
#include <atomic>
|
2017-04-23 13:30:51 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2016-03-12 18:26:14 +00:00
|
|
|
#include <ostd/platform.hh>
|
2017-04-24 16:18:10 +00:00
|
|
|
#include <ostd/range.hh>
|
|
|
|
#include <ostd/format.hh>
|
2016-03-12 18:26:14 +00:00
|
|
|
#include <ostd/io.hh>
|
|
|
|
#include <ostd/string.hh>
|
|
|
|
#include <ostd/filesystem.hh>
|
2016-03-13 01:28:39 +00:00
|
|
|
#include <ostd/environ.hh>
|
2017-04-24 16:18:10 +00:00
|
|
|
#include <ostd/thread_pool.hh>
|
2016-03-12 18:26:14 +00:00
|
|
|
|
|
|
|
using namespace ostd;
|
|
|
|
|
|
|
|
#ifndef OSTD_PLATFORM_WIN32
|
2017-02-25 13:56:51 +00: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";
|
2016-03-12 18:26:14 +00:00
|
|
|
#else
|
2017-02-25 13:56:51 +00:00
|
|
|
constexpr auto COLOR_RED = "";
|
|
|
|
constexpr auto COLOR_GREEN = "";
|
|
|
|
constexpr auto COLOR_BLUE = "";
|
|
|
|
constexpr auto COLOR_BOLD = "";
|
|
|
|
constexpr auto COLOR_END = "";
|
2017-04-24 16:18:10 +00:00
|
|
|
#define popen _popen
|
2016-03-12 18:26:14 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-24 16:18:10 +00: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 %s\n"
|
|
|
|
"\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(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 01:28:39 +00:00
|
|
|
int main() {
|
|
|
|
/* configurable section */
|
|
|
|
|
2016-07-08 18:48:11 +00:00
|
|
|
auto compiler = env_get("CXX").value_or("c++");
|
2017-01-29 20:22:40 +00:00
|
|
|
auto cxxflags = "-std=c++1z -I. -Wall -Wextra -Wshadow -Wold-style-cast "
|
2016-03-15 22:28:43 +00:00
|
|
|
"-Wno-missing-braces"; /* clang false positive */
|
2016-03-13 01:28:39 +00:00
|
|
|
|
2016-07-08 18:48:11 +00:00
|
|
|
auto userflags = env_get("CXXFLAGS").value_or("");
|
2016-03-13 01:28:39 +00:00
|
|
|
|
|
|
|
/* do not change past this point */
|
|
|
|
|
2017-04-24 16:18:10 +00:00
|
|
|
std::atomic_int nsuccess = 0, nfailed = 0;
|
2016-03-12 18:26:14 +00:00
|
|
|
|
2017-04-24 16:18:10 +00:00
|
|
|
auto print_error = [&](string_range modname, string_range fmsg) {
|
|
|
|
write_padded(modname, 20);
|
2017-04-23 15:54:51 +00:00
|
|
|
writeln(COLOR_RED, COLOR_BOLD, '(', fmsg, ')', COLOR_END);
|
|
|
|
++nfailed;
|
2016-03-12 18:26:14 +00:00
|
|
|
};
|
|
|
|
|
2017-04-24 16:18:10 +00:00
|
|
|
ostd::thread_pool tpool;
|
|
|
|
tpool.start(std::thread::hardware_concurrency());
|
|
|
|
|
|
|
|
filesystem::directory_iterator ds{"ostd"};
|
2017-03-10 17:19:59 +00:00
|
|
|
for (auto &v: ds) {
|
|
|
|
auto p = filesystem::path{v};
|
|
|
|
if (
|
|
|
|
(!filesystem::is_regular_file(p)) ||
|
2017-04-24 16:18:10 +00:00
|
|
|
(p.extension().string() != ".hh")
|
2017-03-10 17:19:59 +00:00
|
|
|
) {
|
2016-03-12 18:26:14 +00:00
|
|
|
continue;
|
2017-03-10 17:19:59 +00:00
|
|
|
}
|
2017-04-24 16:18:10 +00:00
|
|
|
tpool.push([&, modname = p.stem().string()]() {
|
2016-07-08 23:48:29 +00:00
|
|
|
#ifdef OSTD_PLATFORM_WIN32
|
2017-04-24 16:18:10 +00:00
|
|
|
std::string exepath = ".\\test_";
|
|
|
|
exepath += modname;
|
|
|
|
exepath += ".exe";
|
|
|
|
#else
|
|
|
|
std::string exepath = "./test_";
|
|
|
|
exepath += modname;
|
2016-07-08 23:48:29 +00:00
|
|
|
#endif
|
2016-03-13 01:28:39 +00:00
|
|
|
|
2017-04-24 16:18:10 +00: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());
|
|
|
|
++nsuccess;
|
|
|
|
});
|
2016-03-12 18:26:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 16:18:10 +00:00
|
|
|
/* wait for tasks */
|
|
|
|
tpool.destroy();
|
|
|
|
|
2017-02-25 13:56:51 +00:00
|
|
|
writeln("\n", COLOR_BLUE, COLOR_BOLD, "testing done:", COLOR_END);
|
2017-04-24 16:18:10 +00:00
|
|
|
writeln(COLOR_GREEN, "SUCCESS: ", int(nsuccess), COLOR_END);
|
|
|
|
writeln(COLOR_RED, "FAILURE: ", int(nfailed), COLOR_END);
|
2016-03-12 18:26:14 +00:00
|
|
|
}
|