add native (c++ based) test runner instead of python script
parent
b1feda7c5b
commit
2bc3c94daa
|
@ -6,3 +6,4 @@ example/range
|
|||
example/signal
|
||||
example/stream1
|
||||
example/stream2
|
||||
test_runner
|
||||
|
|
26
Makefile
26
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
|
||||
|
|
74
run_tests.py
74
run_tests.py
|
@ -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"]
|
|
@ -0,0 +1,101 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <ostd/platform.hh>
|
||||
#include <ostd/io.hh>
|
||||
#include <ostd/string.hh>
|
||||
#include <ostd/map.hh>
|
||||
#include <ostd/filesystem.hh>
|
||||
|
||||
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<ConstCharRange, ConstCharRange> 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"]);
|
||||
}
|
Loading…
Reference in New Issue