diff --git a/.gitignore b/.gitignore index 0976a34..23f0bd6 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ example/range example/signal example/stream1 example/stream2 +test_runner diff --git a/Makefile b/Makefile index 9ac3dcb..3d347d1 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,23 @@ -EXAMPLES_CXXFLAGS = -O2 -g -std=c++14 -Wall -Wextra -Wshadow -I. +OSTD_CXXFLAGS = -O2 -g -std=c++14 -Wall -Wextra -Wshadow -I. EXAMPLES_OBJ = \ - examples/format.o \ - examples/listdir.o \ - examples/range.o \ - examples/signal.o \ - examples/stream1.o \ - examples/stream2.o + examples/format \ + examples/listdir \ + examples/range \ + examples/signal \ + examples/stream1 \ + examples/stream2 + +all: examples examples: $(EXAMPLES_OBJ) +test_runner: test_runner.o -.cc.o: - $(CXX) $(CXXFLAGS) $(EXAMPLES_CXXFLAGS) -o $@ $< +.cc: + $(CXX) $(CXXFLAGS) $(OSTD_CXXFLAGS) -o $@ $< + +test: test_runner + @./test_runner clean: - rm -f $(EXAMPLES_OBJ) + rm -f $(EXAMPLES_OBJ) test_runner diff --git a/run_tests.py b/run_tests.py deleted file mode 100644 index 65c6a28..0000000 --- a/run_tests.py +++ /dev/null @@ -1,74 +0,0 @@ -from sys import stdout, exit -from os import listdir, remove, name as osname, getenv -from os.path import splitext, join as joinp -import subprocess as sp - -# configuration - you can modify this - -COMPILER = getenv("CXX", "c++") -CXXFLAGS = [ - "-std=c++14", - "-Wall", "-Wextra", "-Wshadow", - "-Wno-missing-braces", # clang false positive - "-I." -] + getenv("CXXFLAGS", "").split() -COLORS = (osname != "nt") -TESTDIR = getenv("TESTDIR", "tests") -SRCEXT = ".cc" - -# don't modify past these lines - -nsuccess = 0 -nfailed = 0 - -if COLORS: - colors = { - "red": "\033[91m", - "green": "\033[92m", - "blue": "\033[94m", - "bold": "\033[1m", - "end": "\033[0m" - } -else: - colors = { "red": "", "green": "", "blue": "", "bold": "", "end": "" } - -def print_result(modname, fmsg = None): - global nsuccess, nfailed - if fmsg: - print modname + ("...\t%(red)s%(bold)s(" + fmsg + ")%(end)s") % colors - nfailed += 1 - else: - print modname + "...\t%(green)s%(bold)s(success)%(end)s" % colors - nsuccess += 1 - -for fname in listdir(TESTDIR): - (modname, modext) = splitext(fname) - - if modext != SRCEXT: - continue - - srcpath = joinp(TESTDIR, fname) - exepath = joinp(TESTDIR, modname) - - pc = sp.Popen([ COMPILER, srcpath, "-o", exepath ] + CXXFLAGS, - stdout = sp.PIPE, stderr = sp.STDOUT) - stdout.write(pc.communicate()[0]) - - if pc.returncode != 0: - print_result(modname, "compile error") - continue - - pc = sp.Popen(exepath, stdout = sp.PIPE, stderr = sp.STDOUT) - stdout.write(pc.communicate()[0]) - - if pc.returncode != 0: - remove(exepath) - print_result(modname, "runtime error") - continue - - remove(exepath) - print_result(modname) - -print "\n%(blue)s%(bold)stesting done:%(end)s" % colors -print "%(green)sSUCCESS: " % colors + str(nsuccess) + colors["end"] -print "%(red)sFAILURE: " % colors + str(nfailed) + colors["end"] diff --git a/test_runner.cc b/test_runner.cc new file mode 100644 index 0000000..381851d --- /dev/null +++ b/test_runner.cc @@ -0,0 +1,101 @@ +#include +#include + +#include +#include +#include +#include +#include + +using namespace ostd; + +ConstCharRange get_env(ConstCharRange var, ConstCharRange def = nullptr) { + const char *ret = getenv(String(var).data()); + if (!ret || !ret[0]) + return def; + return ret; +} + +int main() { + ConstCharRange compiler = get_env("CXX", "c++"); + ConstCharRange cxxflags = "-std=c++14 -Wall -Wextra -Wshadow " + "-Wno-missing-braces " /* clang false positive */ + "-I."; + ConstCharRange testdir = get_env("TESTDIR", "tests"); + ConstCharRange srcext = ".cc"; + + Map colors = { +#ifndef OSTD_PLATFORM_WIN32 + { "red", "\033[91m" }, + { "green", "\033[92m" }, + { "blue", "\033[94m" }, + { "bold", "\033[1m" }, + { "end", "\033[0m" } +#else + { "red", "" }, + { "green", "" }, + { "blue", "" }, + { "bold", "" }, + { "end", "" } +#endif + }; + + String cxxf = cxxflags; + cxxf += get_env("CXXFLAGS", ""); + + int nsuccess = 0, nfailed = 0; + + auto print_result = [&colors, &nsuccess, &nfailed] + (ConstCharRange modname, ConstCharRange fmsg = nullptr) { + if (!fmsg.empty()) { + writeln(modname, "...\t", colors["red"], colors["bold"], + "(", fmsg, ")", colors["end"]); + ++nfailed; + } else { + writeln(modname, "...\t", colors["green"], colors["bold"], + "(success)", colors["end"]); + ++nsuccess; + } + }; + + DirectoryStream ds(testdir); + for (auto v: ds.iter()) { + if (v.type() != FileType::regular) + continue; + if (v.extension() != srcext) + continue; + + String exepath = testdir; + exepath += PathSeparator; + exepath += v.stem(); + + String cxxcmd = compiler; + cxxcmd += " "; + cxxcmd += testdir; + cxxcmd += PathSeparator; + cxxcmd += v.filename(); + cxxcmd += " -o "; + cxxcmd += exepath; + cxxcmd += " "; + cxxcmd += cxxf; + + int ret = system(cxxcmd.data()); + if (ret) { + print_result(v.stem(), "compile errror"); + continue; + } + + ret = system(exepath.data()); + if (ret) { + print_result(v.stem(), "runtime error"); + continue; + } + + remove(exepath.data()); + print_result(v.stem()); + } + + writeln("\n", colors["blue"], colors["bold"], "testing done:", colors["end"]); + writeln(colors["green"], "SUCCESS: ", nsuccess, colors["end"]); + writeln(colors["red"], "FAILURE: ", nfailed, colors["end"]); +}