First test infrastructure

This commit is contained in:
Robin Jadoul 2016-05-28 21:04:12 +02:00
parent e19380b4ab
commit e2517cf16a
7 changed files with 189 additions and 3 deletions

42
run_tests.py Executable file
View File

@ -0,0 +1,42 @@
#!/usr/bin/python3
import unittest, subprocess, filecmp, argparse, os.path, os, shutil
REFERENCE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "tests")
REGEXES_DATA = [ #TODO
[r"[a-zA-Z0-9]*"]
]
def make_test(regexes, idx):
def test(self):
p = subprocess.Popen([os.path.join(args.builddir, "src", "Lexesis-test"), "test_%s" % idx, data_dir], stdin=subprocess.PIPE)
p.communicate(bytes("\n".join(regexes), "utf-8"))
self.assertEqual(0, p.returncode)
self.assertTrue(filecmp.cmp(os.path.join(REFERENCE_DIR, "test_%s.re" % idx), os.path.join(data_dir, "test_%s.re" % idx)))
self.assertTrue(filecmp.cmp(os.path.join(REFERENCE_DIR, "test_%s.enfa" % idx), os.path.join(data_dir, "test_%s.enfa" % idx)))
self.assertTrue(filecmp.cmp(os.path.join(REFERENCE_DIR, "test_%s.mssc" % idx), os.path.join(data_dir, "test_%s.mssc" % idx)))
self.assertTrue(filecmp.cmp(os.path.join(REFERENCE_DIR, "test_%s.min" % idx), os.path.join(data_dir, "test_%s.min" % idx)))
return test
class Tests(unittest.TestCase):
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--builddir", metavar="builddir", default=os.path.join(os.path.abspath(os.path.dirname(__file__)), "build"), required=False)
args = parser.parse_args()
data_dir = os.path.join(args.builddir, "test_data")
try:
shutil.rmtree(data_dir)
except Exception as err:
pass
os.mkdir(data_dir)
for i, regexes in enumerate(REGEXES_DATA):
t = make_test(regexes, i)
setattr(Tests, "test_%s" % i, t)
unittest.main()

View File

@ -5,8 +5,7 @@ add_library(Lexesis-backends
backends/cpp.cpp
)
add_executable(Lexesis
main.cpp
add_library(lxs
automata.cpp
backend.cpp
backendmanager.cpp
@ -15,7 +14,18 @@ add_executable(Lexesis
inputparser.cpp
template.cpp
)
target_link_libraries(Lexesis Lexesis-backends mstch::mstch)
add_executable(Lexesis
main.cpp
)
target_link_libraries(Lexesis Lexesis-backends lxs mstch::mstch)
if (CMAKE_BUILD_TYPE MATCHES Debug)
add_executable(Lexesis-test
test.cpp
)
target_link_libraries(Lexesis-test Lexesis-backends lxs mstch::mstch)
endif()
install(TARGETS Lexesis
RUNTIME DESTINATION bin

55
src/test.cpp Normal file
View File

@ -0,0 +1,55 @@
#include "Lexesis/automata.h"
#include "Lexesis/driver.h"
#include "Lexesis/re.h"
#include <cassert>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
int main(int argc, char** argv) {
assert(argc == 3);
std::vector<std::shared_ptr<lxs::RE> > regexes;
std::string testname = argv[1];
std::string outputdir = argv[2];
std::string line;
std::ofstream reOut(outputdir + "/" + testname + ".re");
while (getline(std::cin, line)) {
std::shared_ptr<lxs::RE> re = lxs::parseRE(line);
reOut << re->toRe() << std::endl;
regexes.push_back(re);
}
reOut.close();
std::vector<lxs::ENFA> enfas;
for (std::size_t i = 0; i < regexes.size(); i++) {
lxs::ENFA enfa;
regexes[i]->toENFA(enfa, 0);
enfa.numStates++;
enfa.starting = 0;
enfa.priority[*enfa.accepting.begin()] = i;
enfa.acceptingToken[*enfa.accepting.begin()] = std::to_string(i);
enfas.push_back(enfa);
}
lxs::ENFA merged = lxs::merge(enfas);
std::ofstream enfaOut(outputdir + "/" + testname + ".enfa");
enfaOut << lxs::toDot(merged) << std::endl;
enfaOut.close();
lxs::DFA dfa = lxs::mssc(merged);
std::ofstream msscOut(outputdir + "/" + testname + ".mssc");
msscOut << lxs::toDot(dfa) << std::endl;
msscOut.close();
lxs::DFA min = lxs::minimize(dfa);
std::ofstream minOut(outputdir + "/" + testname + ".min");
minOut << lxs::toDot(min) << std::endl;
minOut.close();
return 0;
}

78
tests/test_0.enfa Normal file
View File

@ -0,0 +1,78 @@
digraph {
rankdir=LR
in [shape=point style=invis]
0 [label="0" color=yellow]
1 [label="1"]
2 [label="2"]
3 [label="3"]
4 [label="4" color=green shape=doublecircle]
2 -> 3 [label="0"]
2 -> 3 [label="1"]
2 -> 3 [label="2"]
2 -> 3 [label="3"]
2 -> 3 [label="4"]
2 -> 3 [label="5"]
2 -> 3 [label="6"]
2 -> 3 [label="7"]
2 -> 3 [label="8"]
2 -> 3 [label="9"]
2 -> 3 [label="A"]
2 -> 3 [label="B"]
2 -> 3 [label="C"]
2 -> 3 [label="D"]
2 -> 3 [label="E"]
2 -> 3 [label="F"]
2 -> 3 [label="G"]
2 -> 3 [label="H"]
2 -> 3 [label="I"]
2 -> 3 [label="J"]
2 -> 3 [label="K"]
2 -> 3 [label="L"]
2 -> 3 [label="M"]
2 -> 3 [label="N"]
2 -> 3 [label="O"]
2 -> 3 [label="P"]
2 -> 3 [label="Q"]
2 -> 3 [label="R"]
2 -> 3 [label="S"]
2 -> 3 [label="T"]
2 -> 3 [label="U"]
2 -> 3 [label="V"]
2 -> 3 [label="W"]
2 -> 3 [label="X"]
2 -> 3 [label="Y"]
2 -> 3 [label="Z"]
2 -> 3 [label="a"]
2 -> 3 [label="b"]
2 -> 3 [label="c"]
2 -> 3 [label="d"]
2 -> 3 [label="e"]
2 -> 3 [label="f"]
2 -> 3 [label="g"]
2 -> 3 [label="h"]
2 -> 3 [label="i"]
2 -> 3 [label="j"]
2 -> 3 [label="k"]
2 -> 3 [label="l"]
2 -> 3 [label="m"]
2 -> 3 [label="n"]
2 -> 3 [label="o"]
2 -> 3 [label="p"]
2 -> 3 [label="q"]
2 -> 3 [label="r"]
2 -> 3 [label="s"]
2 -> 3 [label="t"]
2 -> 3 [label="u"]
2 -> 3 [label="v"]
2 -> 3 [label="w"]
2 -> 3 [label="x"]
2 -> 3 [label="y"]
2 -> 3 [label="z"]
0 -> 1 [label="ε"]
1 -> 2 [label="ε"]
1 -> 4 [label="ε"]
3 -> 2 [label="ε"]
3 -> 4 [label="ε"]
in -> 0
}

BIN
tests/test_0.min Normal file

Binary file not shown.

BIN
tests/test_0.mssc Normal file

Binary file not shown.

1
tests/test_0.re Normal file
View File

@ -0,0 +1 @@
([0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz])*