From de80a6ab7be1ec47e261ee13c66264124b840ce9 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Fri, 25 Apr 2014 00:45:45 +0200 Subject: [PATCH] example tests of alib2 --- alib2/makefile | 21 +++++++++++++++++++-- alib2/test-src/main.cpp | 24 ++++++++++++++++++++++++ alib2/test-src/regexp/RegExpTest.cpp | 27 +++++++++++++++++++++++++++ alib2/test-src/regexp/RegExpTest.h | 19 +++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 alib2/test-src/main.cpp create mode 100644 alib2/test-src/regexp/RegExpTest.cpp create mode 100644 alib2/test-src/regexp/RegExpTest.h diff --git a/alib2/makefile b/alib2/makefile index f32e2ff62b..819b1046f9 100644 --- a/alib2/makefile +++ b/alib2/makefile @@ -1,12 +1,18 @@ CC=g++ LIBRARY=libalib2.so +TESTBIN=alib2test CCFLAGS= -std=c++11 -O2 -g -c -Wall -pedantic -fPIC -I/usr/include/libxml2/ LDFLAGS= -shared -lxml2 +TEST_CCFLAGS= -std=c++11 -O2 -g -c -Wall -pedantic -I../alib2/src -I/usr/include/libxml2/ +TEST_LDFLAGS= -L../alib2/lib -lxml2 -lalib2 -lcppunit -Wl,-rpath,. SOURCES=$(shell find src/ -name *cpp) OBJECTS=$(patsubst src/%.cpp, obj/%.o, $(SOURCES)) -all: $(SOURCES) lib/$(LIBRARY) +TEST_SOURCES=$(shell find test-src/ -name *cpp) +TEST_OBJECTS=$(patsubst test-src/%.cpp, test-obj/%.o, $(TEST_SOURCES)) + +all: lib/$(LIBRARY) test-bin/$(TESTBIN) test lib/$(LIBRARY): $(OBJECTS) mkdir -p lib @@ -16,5 +22,16 @@ obj/%.o: src/%.cpp mkdir -p $(dir $@) $(CC) $(CCFLAGS) $< -o $@ +test-bin/$(TESTBIN): $(TEST_OBJECTS) lib/$(LIBRARY) + mkdir -p test-bin + $(CC) $(TEST_OBJECTS) -o $@ $(TEST_LDFLAGS) + +test-obj/%.o: test-src/%.cpp + mkdir -p $(dir $@) + $(CC) $(TEST_CCFLAGS) $< -o $@ + +test: test-bin/$(TESTBIN) + LD_LIBRARY_PATH=lib test-bin/$(TESTBIN) + clean: - $(RM) -r *.o *.d lib obj + $(RM) -r *.o *.d lib obj test-bin test-obj diff --git a/alib2/test-src/main.cpp b/alib2/test-src/main.cpp new file mode 100644 index 0000000000..fdbbccd10a --- /dev/null +++ b/alib2/test-src/main.cpp @@ -0,0 +1,24 @@ +//#include "stdafx.h" +#include <cppunit/CompilerOutputter.h> +#include <cppunit/extensions/TestFactoryRegistry.h> +#include <cppunit/ui/text/TestRunner.h> + + +int main(int , char*[]) +{ + // 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. + bool wasSucessful = runner.run(); + + // Return error code 1 if the one of test failed. + return wasSucessful ? 0 : 1; +} diff --git a/alib2/test-src/regexp/RegExpTest.cpp b/alib2/test-src/regexp/RegExpTest.cpp new file mode 100644 index 0000000000..e99b479dc6 --- /dev/null +++ b/alib2/test-src/regexp/RegExpTest.cpp @@ -0,0 +1,27 @@ +#include "RegExpTest.h" +#include "regexp/RegExp.h" +#include "regexp/RegExpFromStringParser.h" +#include "regexp/RegExpToStringComposer.h" + +CPPUNIT_TEST_SUITE_REGISTRATION( RegExpTest ); + +void RegExpTest::setUp() { +} + +void RegExpTest::tearDown() { +} + +void RegExpTest::testEqual() { + std::string input = "a+a"; + + regexp::RegExpFromStringParser parser(input); + regexp::RegExp regexp = parser.parse(); + + regexp::RegExpToStringComposer composer; + std::string output = composer.compose(regexp); + + regexp::RegExpFromStringParser parser2(input); + regexp::RegExp regexp2 = parser2.parse(); + + CPPUNIT_ASSERT( regexp == regexp2 ); +} diff --git a/alib2/test-src/regexp/RegExpTest.h b/alib2/test-src/regexp/RegExpTest.h new file mode 100644 index 0000000000..6c343586d4 --- /dev/null +++ b/alib2/test-src/regexp/RegExpTest.h @@ -0,0 +1,19 @@ +#ifndef REG_EXP_TEST_H_ +#define REG_EXP_TEST_H_ + +#include <cppunit/extensions/HelperMacros.h> + +class RegExpTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( RegExpTest ); + CPPUNIT_TEST( testEqual ); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + void tearDown(); + + void testEqual(); +}; + +#endif // REG_EXP_TEST_H_ -- GitLab