From 1e19813fd2425df265e2c25f2b848f8729c50bbb Mon Sep 17 00:00:00 2001 From: Alexey Fedoseev Date: Sat, 13 Apr 2024 23:48:48 +0300 Subject: [PATCH] library testing infrastructure --- Makefile | 29 +++++++++++++------- run-tests.sh | 50 +++++++++++++++++++++++++++++++++++ tests/01-empty-doc-except.cpp | 38 ++++++++++++++++++++++++++ tests/02-empty-doc-valid.cpp | 38 ++++++++++++++++++++++++++ tests/02-output.graphml | 38 ++++++++++++++++++++++++++ 5 files changed, 184 insertions(+), 9 deletions(-) create mode 100755 run-tests.sh create mode 100644 tests/01-empty-doc-except.cpp create mode 100644 tests/02-empty-doc-valid.cpp create mode 100644 tests/02-output.graphml diff --git a/Makefile b/Makefile index 7054a1c..421d5da 100644 --- a/Makefile +++ b/Makefile @@ -7,11 +7,15 @@ else LIB_TARGET := $(LIB_TARGET_STATIC) endif -TEST_TARGET := cyberiadapp_test +TESTS_DIR := tests +MAIN_TARGET := cyberiadapp LIB_SOURCES := cyberiadamlpp.cpp -TEST_SOURCES := main.cpp +MAIN_SOURCES := main.cpp LIB_OBJECTS := $(patsubst %.cpp, %.o, $(LIB_SOURCES)) +MAIN_OBJECTS := $(patsubst %.cpp, %.o, $(MAIN_SOURCES)) +TEST_SOURCES := $(wildcard $(addsuffix /*.cpp, $(TESTS_DIR))) TEST_OBJECTS := $(patsubst %.cpp, %.o, $(TEST_SOURCES)) +TEST_TARGETS := $(patsubst %.cpp, %.test, $(TEST_SOURCES)) ifeq ($(DEBUG), 1) CFLAGS := -Wall -Wshadow -Wconversion -fPIC -g3 -D__DEBUG__ @@ -23,7 +27,7 @@ endif INCLUDE := -I. -I/usr/include/libxml2 -I./cyberiadaml LIBS := -L/usr/lib -lxml2 -L./cyberiadaml -lcyberiadaml -TEST_LIBS := -L. -lcyberiadamlpp +MAIN_LIBS := -L. -lcyberiadamlpp $(LIB_TARGET): $(LIB_OBJECTS) ifeq ($(DYNAMIC), 1) @@ -32,17 +36,24 @@ else ar rcs $@ $(LIB_OBJECTS) endif -$(TEST_TARGET): $(TEST_OBJECTS) $(LIB_TARGET) $(LIB_ORJECTS) - g++ $(TEST_OBJECTS) -Wl,-\( $(LIBS) $(TEST_LIBS) -Wl,-\) -o $@ +$(MAIN_TARGET): $(MAIN_OBJECTS) $(LIB_TARGET) $(LIB_ORJECTS) + g++ $(MAIN_OBJECTS) -Wl,-\( $(LIBS) $(MAIN_LIBS) -Wl,-\) -o $@ + +%.test: %.o + g++ $< -Wl,-\( $(LIBS) $(MAIN_LIBS) -Wl,-\) -o $@ %.o: %.cpp g++ -c $< $(CFLAGS) $(INCLUDE) -o $@ clean: - rm -f *~ *.o $(TARGET) $(TEST_TARGET) $(LIB_TARGET_STATIC) $(LIB_TARGET_DYNAMIC) + rm -f *~ *.o $(TARGET) $(MAIN_TARGET) $(LIB_TARGET_STATIC) $(LIB_TARGET_DYNAMIC) + rm -f $(TESTS_DIR)/*~ $(TESTS_DIR)/*.o $(TESTS_DIR)/*.test.graphml $(TESTS_DIR)/*.test.txt $(TEST_TARGETS) -main: $(TEST_TARGET) +main: $(MAIN_TARGET) -all: $(LIB_TARGET) $(TEST_TARGET) +tests: $(TEST_TARGETS) + @echo >/dev/null -.PHONY: all clean main +all: $(LIB_TARGET) $(MAIN_TARGET) $(TEST_TARGETS) + +.PHONY: all clean main tests diff --git a/run-tests.sh b/run-tests.sh new file mode 100755 index 0000000..8ec07c3 --- /dev/null +++ b/run-tests.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +DEBUG=1 make +DEBUG=1 make tests +if [ $? != 0 ] +then + echo "make test failed!" + exit 1 +fi + +echo +echo "tests ready!" +echo + +for t in $(ls tests/*.test); do + num=$(echo $t | grep -Poe '\d+') + echo -n "$num $t... " + if [ -f "tests/$num-input.graphml" ] + then + $t > "$t.txt" + else + $t + fi + if [ $? != 0 ] + then + echo "test run failed!" + exit 2 + fi + if [ -f "tests/$num-output.graphml" ] + then + diff "$t.graphml" "tests/$num-output.graphml" >/dev/null + if [ $? != 0 ] + then + echo "test failed: graphml file didn't match the pattern!" + exit 3 + fi + fi + if [ -f "tests/$num-output.txt" ] + then + diff "$t.txt" "tests/$num-output.txt" >/dev/null + if [ $? != 0 ] + then + echo "test failed: output didn't match the pattern!" + exit 3 + fi + fi + echo "ok" +done + +exit 0 diff --git a/tests/01-empty-doc-except.cpp b/tests/01-empty-doc-except.cpp new file mode 100644 index 0000000..69f7698 --- /dev/null +++ b/tests/01-empty-doc-except.cpp @@ -0,0 +1,38 @@ +/* ----------------------------------------------------------------------------- + * The Cyberiada GraphML C++ library implemention + * + * The test + * + * Copyright (C) 2024 Alexey Fedoseev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see https://www.gnu.org/licenses/ + * ----------------------------------------------------------------------------- */ + +#include +#include "cyberiadamlpp.h" + +using namespace Cyberiada; +using namespace std; + +int main(int argc, char** argv) +{ + Document d; + try { + d.save(string(argv[0]) + ".graphml", formatCyberiada10); + } catch (const Cyberiada::ParametersException&) { + return 0; + } catch (const Cyberiada::Exception&) { + } + return 1; +} diff --git a/tests/02-empty-doc-valid.cpp b/tests/02-empty-doc-valid.cpp new file mode 100644 index 0000000..1b9ac34 --- /dev/null +++ b/tests/02-empty-doc-valid.cpp @@ -0,0 +1,38 @@ +/* ----------------------------------------------------------------------------- + * The Cyberiada GraphML C++ library implemention + * + * The test + * + * Copyright (C) 2024 Alexey Fedoseev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see https://www.gnu.org/licenses/ + * ----------------------------------------------------------------------------- */ + +#include +#include "cyberiadamlpp.h" + +using namespace Cyberiada; +using namespace std; + +int main(int argc, char** argv) +{ + Document d; + d.new_state_machine("SM"); + try { + d.save(string(argv[0]) + ".graphml", formatCyberiada10); + } catch (const Cyberiada::Exception&) { + return 1; + } + return 0; +} diff --git a/tests/02-output.graphml b/tests/02-output.graphml new file mode 100644 index 0000000..5d1cfb3 --- /dev/null +++ b/tests/02-output.graphml @@ -0,0 +1,38 @@ + + + Cyberiada-GraphML-1.0 + + + + + + + + + + + + + + + + + + + + + + SM + + formal + CGML_META + standardVersion/ 1.0 + +transitionOrder/ transitionFirst + +eventPropagation/ propagate + + + + +