From 63eae71e385137a4f0808a8592870168729a0ff6 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sun, 29 Jun 2014 20:37:12 +0200 Subject: [PATCH] draft acat2 --- acat2/makefile | 131 ++++++++++++++++++++++++++++++++++++++++ acat2/src/acat.cpp | 64 ++++++++++++++++++++ acat2/test-src/main.cpp | 120 ++++++++++++++++++++++++++++++++++++ 3 files changed, 315 insertions(+) create mode 100644 acat2/makefile create mode 100644 acat2/src/acat.cpp create mode 100644 acat2/test-src/main.cpp diff --git a/acat2/makefile b/acat2/makefile new file mode 100644 index 0000000000..4d61bc69ee --- /dev/null +++ b/acat2/makefile @@ -0,0 +1,131 @@ +SHELL:=/bin/bash +EXECUTABLE:=acat2 +TESTBIN:=acat2test + +LDFLAGS= -L../alib2/lib -rdynamic -lxml2 -lalib2 -Wl,-rpath,. +TEST_LDFLAGS:= -L../alib2/lib -rdynamic -lxml2 -lalib2 -lcppunit -Wl,-rpath,. + +OBJECTS:=$(patsubst src/%.cpp, obj/%.o, $(shell find src/ -name *cpp)) +TEST_OBJECTS:=$(patsubst test-src/%.cpp, test-obj/%.o, $(shell find test-src/ -name *cpp)) + +.PHONY: all build test clean + +all: build test + + + +bin/$(EXECUTABLE): obj/ $(OBJECTS) + mkdir -p bin + $(CXX) $(OBJECTS) -o $@ $(LDFLAGS) + +obj/makefile: makefile + mkdir -p $(dir $@) + echo "SHELL:=/bin/bash" >> $@ + echo "SRCDIR:=" >> $@ + echo "DEPTH:=" >> $@ + echo "" >> $@ + echo "CXXFLAGS:= -std=c++11 -Og -g -c -Wall -pedantic -Wextra -I../../alib2/src/ -I/usr/include/libxml2/" >> $@ + echo "" >> $@ + echo "SOURCES:= \$$(shell find ../\$$(DEPTH)src/\$$(SRCDIR) -maxdepth 1 -type f -name \"*.cpp\")" >> $@ + echo "DEPENDENCIES:= \$$(patsubst ../\$$(DEPTH)src/\$$(SRCDIR)%.cpp, ../\$$(DEPTH)obj/\$$(SRCDIR)%.d, \$$(SOURCES))" >> $@ + echo "OBJECTS:= \$$(patsubst %.d, %.o, \$$(DEPENDENCIES))" >> $@ + echo "SOURCES_DIRS:= \$$(shell find ../\$$(DEPTH)src/\$$(SRCDIR) -maxdepth 1 -mindepth 1 -type d)" >> $@ + echo "OBJECTS_DIRS:= \$$(patsubst ../\$$(DEPTH)src/\$$(SRCDIR)%, %/, \$$(SOURCES_DIRS))" >> $@ + echo "OBJECTS_DIRS_MAKEFILES:= \$$(patsubst %, %makefile, \$$(OBJECTS_DIRS))" >> $@ + echo "" >> $@ + echo ".PHONY: all" >> $@ + echo ".PRECIOUS: \$$(DEPENDECIES) \$$(OBJECTS_DIRS_MAKEFILES)" >> $@ + echo "" >> $@ + echo "all: \$$(OBJECTS_DIRS) \$$(OBJECTS)" >> $@ + echo "" >> $@ + echo "%.d:" >> $@ + echo " @echo \"\$$(shell sha1sum <<< \"\$$@\" | sed \"s/ -//g\") = \\$$\$$(shell (\\$$\$$(CXX) -MM \\$$\$$(CXXFLAGS) \$$(patsubst ../\$$(DEPTH)obj/\$$(SRCDIR)%.d,../\$$(DEPTH)src/\$$(SRCDIR)%.cpp, \$$@) 2>/dev/null || echo \\\"\$$(patsubst ../\$$(DEPTH)obj/\$$(SRCDIR)%.d,../\$$(DEPTH)src/\$$(SRCDIR)%.cpp, \$$@) FORCE\\\") | sed \\\"s/.*://g;s/\\\\\\\\\\\\\\\\//g\\\")\" >> \$$@" >> $@ + echo " @echo \"\$$(patsubst %.d,%.o, \$$@): \\$$\$$(\$$(shell sha1sum <<< \"\$$@\" | sed \"s/ -//g\"))\" >> \$$@" >> $@ + echo " @echo \" \\$$\$$(CXX) \\$$\$$(CXXFLAGS) \\$$\$$< -o \$$(patsubst %.d,%.o, \$$@)\" >> \$$@" >> $@ + echo "" >> $@ + echo "%/makefile:" >> $@ + echo " mkdir -p \$$(dir \$$@)" >> $@ + echo " cp makefile \$$@" >> $@ + echo "" >> $@ + echo "%/: FORCE | %/makefile" >> $@ + echo " @accesstime=\`stat -c %Y \$$@\` && \\" >> $@ + echo " \$$(MAKE) -C \$$@ SRCDIR=\$$(SRCDIR)\$$(notdir \$$(patsubst %/, %, \$$@))/ DEPTH=\$$(DEPTH)../ && \\" >> $@ + echo " accesstime2=\`stat -c %Y \$$@\` && \\" >> $@ + echo " if [ "\$$\$$accesstime" -ne "\$$\$$accesstime2" ]; then \\" >> $@ + echo " touch .; \\" >> $@ + echo " fi" >> $@ + echo "" >> $@ + echo "FORCE:" >> $@ + echo "" >> $@ + echo "-include \$$(DEPENDENCIES)" >> $@ + +obj/: FORCE | obj/makefile + $(MAKE) -C $@ + +$(OBJECTS): obj/ + + +build: bin/$(EXECUTABLE) + + + +test-bin/$(TESTBIN): test-obj/ bin/$(EXECUTABLE) $(TEST_OBJECTS) + mkdir -p test-bin + $(CXX) $(TEST_OBJECTS) -o $@ $(TEST_LDFLAGS) + +test-obj/makefile: + mkdir -p $(dir $@) + echo "SHELL:=/bin/bash" >> $@ + echo "SRCDIR:=" >> $@ + echo "DEPTH:=" >> $@ + echo "" >> $@ + echo "CXXFLAGS:= -std=c++11 -Og -g -c -Wall -pedantic -Wextra -I../\$$(DEPTH)src/ -I/usr/include/libxml2/" >> $@ + echo "" >> $@ + echo "SOURCES:= \$$(shell find ../\$$(DEPTH)test-src/\$$(SRCDIR) -maxdepth 1 -type f -name \"*.cpp\")" >> $@ + echo "DEPENDENCIES:= \$$(patsubst ../\$$(DEPTH)test-src/\$$(SRCDIR)%.cpp, ../\$$(DEPTH)test-obj/\$$(SRCDIR)%.d, \$$(SOURCES))" >> $@ + echo "OBJECTS:= \$$(patsubst %.d, %.o, \$$(DEPENDENCIES))" >> $@ + echo "SOURCES_DIRS:= \$$(shell find ../\$$(DEPTH)test-src/\$$(SRCDIR) -maxdepth 1 -mindepth 1 -type d)" >> $@ + echo "OBJECTS_DIRS:= \$$(patsubst ../\$$(DEPTH)test-src/\$$(SRCDIR)%, %/, \$$(SOURCES_DIRS))" >> $@ + echo "OBJECTS_DIRS_MAKEFILES:= \$$(patsubst %, %makefile, \$$(OBJECTS_DIRS))" >> $@ + echo "" >> $@ + echo ".PHONY: all" >> $@ + echo ".PRECIOUS: \$$(DEPENDECIES) \$$(OBJECTS_DIRS_MAKEFILES)" >> $@ + echo "" >> $@ + echo "all: \$$(OBJECTS_DIRS) \$$(OBJECTS)" >> $@ + echo "" >> $@ + echo "%.d:" >> $@ + echo " @echo \"\$$(shell sha1sum <<< \"\$$@\" | sed \"s/ -//g\") = \\$$\$$(shell (\\$$\$$(CXX) -MM \\$$\$$(CXXFLAGS) \$$(patsubst ../\$$(DEPTH)test-obj/\$$(SRCDIR)%.d,../\$$(DEPTH)test-src/\$$(SRCDIR)%.cpp, \$$@) 2>/dev/null || echo \\\"\$$(patsubst ../\$$(DEPTH)test-obj/\$$(SRCDIR)%.d,../\$$(DEPTH)test-src/\$$(SRCDIR)%.cpp, \$$@) FORCE\\\") | sed \\\"s/.*://g;s/\\\\\\\\\\\\\\\\//g\\\")\" >> \$$@" >> $@ + echo " @echo \"\$$(patsubst %.d,%.o, \$$@): \\$$\$$(\$$(shell sha1sum <<< \"\$$@\" | sed \"s/ -//g\"))\" >> \$$@" >> $@ + echo " @echo \" \\$$\$$(CXX) \\$$\$$(CXXFLAGS) \\$$\$$< -o \$$(patsubst %.d,%.o, \$$@)\" >> \$$@" >> $@ + echo "" >> $@ + echo "%/makefile:" >> $@ + echo " mkdir -p \$$(dir \$$@)" >> $@ + echo " cp makefile \$$@" >> $@ + echo "" >> $@ + echo "%/: FORCE | %/makefile" >> $@ + echo " @accesstime=\`stat -c %Y \$$@\` && \\" >> $@ + echo " \$$(MAKE) -C \$$@ SRCDIR=\$$(SRCDIR)\$$(notdir \$$(patsubst %/, %, \$$@))/ DEPTH=\$$(DEPTH)../ && \\" >> $@ + echo " accesstime2=\`stat -c %Y \$$@\` && \\" >> $@ + echo " if [ "\$$\$$accesstime" -ne "\$$\$$accesstime2" ]; then \\" >> $@ + echo " touch .; \\" >> $@ + echo " fi" >> $@ + echo "" >> $@ + echo "FORCE:" >> $@ + echo "" >> $@ + echo "-include \$$(DEPENDENCIES)" >> $@ + +test-obj/: FORCE | test-obj/makefile + $(MAKE) -C $@ + +$(TEST_OBJECTS): test-obj/ + +test: test-bin/$(TESTBIN) + LD_LIBRARY_PATH=../alib2/lib test-bin/$(TESTBIN) + + + +clean: + $(RM) -r *.o *.d lib obj test-bin test-obj + +FORCE: + diff --git a/acat2/src/acat.cpp b/acat2/src/acat.cpp new file mode 100644 index 0000000000..e9e6269e37 --- /dev/null +++ b/acat2/src/acat.cpp @@ -0,0 +1,64 @@ +//============================================================================ +// Name : acat.cpp +// Author : Martin Zak +//============================================================================ + +#include <iostream> +#include <string> +#include <set> +#include <cstdlib> +#include <unistd.h> +#include <exception> +#include "AlibException.h" +#include "FromXMLParsers.h" +#include "sax/SaxParseInterface.h" +#include "sax/ParserException.h" + + +#define VERSION "0.0.1" +#define USAGE "Usage: catPDA [-c] [input]"; + +void getAutomaton(std::list<sax::Token>& tokens) { + automaton::Automaton automaton = alib::FromXMLParsers::automatonParser.parseValue(tokens); + std::cout << automaton; +} + +void getRegExp(std::list<sax::Token>& tokens) { + regexp::RegExp regexp = alib::FromXMLParsers::regexpParser.parseValue(tokens); + std::cout << regexp; +} + +int main(int argc, char** argv) { + if(argc == 2 && std::string(argv[1])=="-v" ) { + std::cout << argv[0] << " version " << VERSION << "\n"; + std::cout << USAGE; + std::cout << std::endl; + return EXIT_SUCCESS; + } + + int i=getopt(argc, argv, "t:"); + while(i!=-1) { + switch(i){ + default: + optind--; + break; + } + i=getopt(argc, argv, "t:"); + } + + std::list<sax::Token> tokens; + if(optind == argc) { + std::string input(std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>()); + sax::SaxParseInterface::parseMemory(input, tokens); + } else { + sax::SaxParseInterface::parseFile(argv[optind],tokens); + } + + if (tokens.front().getData() == "automaton") { + getAutomaton(tokens); + } else if (tokens.front().getData() == "regexp") { + getRegExp(tokens); + } else { + throw alib::AlibException( "Expected root tag automaton, grammar or regexp. Read: " + tokens.front().getData()); + } +} diff --git a/acat2/test-src/main.cpp b/acat2/test-src/main.cpp new file mode 100644 index 0000000000..1b3e032257 --- /dev/null +++ b/acat2/test-src/main.cpp @@ -0,0 +1,120 @@ +//#include "stdafx.h" +#include <cppunit/CompilerOutputter.h> +#include <cppunit/extensions/TestFactoryRegistry.h> +#include <cppunit/ui/text/TestRunner.h> +#include <cppunit/TestResultCollector.h> +#include <cppunit/TestResult.h> + +#include <cppunit/Test.h> +#include <cppunit/TestFailure.h> +#include <cppunit/portability/Stream.h> +#include <cppunit/TestListener.h> +#include <cppunit/SourceLine.h> +#include <cppunit/Exception.h> + +CPPUNIT_NS_BEGIN + +class CPPUNIT_API TestProgressListener : public TestListener +{ +public: + TestProgressListener(); + + virtual ~TestProgressListener(); + + void startTest( Test *test ); + + void addFailure( const TestFailure &failure ); + + void endTest( Test *test ); + + int getResult() const; + + void printResults() const; + +private: + TestProgressListener( const TestProgressListener © ); + + void operator =( const TestProgressListener © ); + +private: + int m_Failures; + int m_Tests; + int m_Assertions; + bool m_lastTestFailed; +}; + +TestProgressListener::TestProgressListener() : m_Failures( 0 ), m_Tests(0), m_Assertions(0), m_lastTestFailed( false ) +{ +} + +TestProgressListener::~TestProgressListener() +{ +} + +void TestProgressListener::startTest( Test * test ) +{ + stdCOut() << test->getName() << ":" << "\n"; + stdCOut().flush(); + + m_lastTestFailed = false; + m_Tests++; +} + +void TestProgressListener::addFailure( const TestFailure &failure ) +{ + stdCOut() << (failure.isError() ? "error" : "assertion") << " : " << failure.failedTestName() << " : " << failure.sourceLine().lineNumber() << "\n"; + stdCOut() << "Exception " << failure.thrownException()->message().details(); + + m_lastTestFailed = true; + if(failure.isError()) m_Failures++; else m_Assertions++; +} + +void TestProgressListener::endTest( Test * test) +{ + stdCOut() << "Result (" << test->getName() << ")"; + stdCOut().flush(); + + if ( !m_lastTestFailed ) + stdCOut() << " : OK"; + else + stdCOut() << " : Fail"; + stdCOut() << "\n\n"; +} + +int TestProgressListener::getResult() const { + return m_Failures + m_Assertions; +} + +void TestProgressListener::printResults() const { + stdCOut() << "Overal result: Tests: " << m_Tests << " Assertions: " << m_Assertions << " Failures: " << m_Failures << "\n"; +} + +CPPUNIT_NS_END + +int main(int , char*[]) +{ + CppUnit::TestResult controller; + + CppUnit::TestResultCollector result; + controller.addListener( &result ); + + CppUnit::TestProgressListener progressListener; + controller.addListener( &progressListener ); + + // Get the top level suite from the registry + CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); + + // Adds the test to the list of test to run + CppUnit::TextUi::TestRunner runner; + runner.addTest( suite ); + + // Change the default outputter to a compiler error format outputter + runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), + std::cerr ) ); + // Run the tests. + runner.run( controller ); + + progressListener.printResults(); + + return progressListener.getResult(); +} -- GitLab