From: Sam Moore Date: Wed, 26 Mar 2014 16:17:12 +0000 (+0800) Subject: Tweak Makefile to make testing nicer X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=42737c4c096f6469fb27e261806b48338a8f672f;p=ipdf%2Fcode.git Tweak Makefile to make testing nicer Who said you needed fancy high level languages to do unit testing? It can't just be done using Make though; a bash script was also needed. --- diff --git a/.gitignore b/.gitignore index 82affd5..365a631 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ *.d *~ *.ipdf +*.test +*.out +*.err diff --git a/src/Makefile b/src/Makefile index 3e8d8a0..0a1bc96 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,10 +1,11 @@ #Makefile CXX = g++ -std=gnu++0x -Wall -Werror -Wshadow -pedantic -g -OBJ = log.o tests/saveload.o document.o view.o screen.o +MAIN = main.o +OBJ = log.o document.o view.o screen.o LIB = `sdl2-config --libs` -lGL OBJPATHS = $(OBJ:%=../obj/%) DEPS := $(OBJPATHS:%.o=%.d) -CFLAGS += `sdl2-config --cflags` +CFLAGS += `sdl2-config --cflags` -I`pwd` LINKOBJ = $(OBJPATHS) @@ -14,9 +15,16 @@ BIN = ../bin/ipdf all : $(BIN) +tests/% : tests/%.cpp ../obj/tests/%.o $(LINKOBJ) + $(CXX) -o $@.test $(LINKOBJ) ../obj/$@.o $(LIB) + +runtests : tests/runtests.sh + cd tests; ./runtests.sh + + $(BIN) : $(LINKOBJ) @mkdir -p $(dir $@) - $(CXX) -o $(BIN) $(LINKOBJ) $(LIB) + $(CXX) -o $(BIN) $(LINKOBJ) ../obj/$(MAIN) $(LIB) ../obj/%.o : %.cpp @mkdir -p $(dir $@) @@ -24,14 +32,21 @@ $(BIN) : $(LINKOBJ) -include $(DEPS) +clean_bin : + $(RM) $(BIN) + clean : $(RM) $(BIN) $(DEPS) $(LINKOBJ) + $(RM) tests/*~ + $(RM) tests/*.test + $(RM) tests/*.out + $(RM) tests/*.err + -clean_full: #cleans up all backup files - $(RM) $(BIN) $(DEPS) $(LINKOBJ) +clean_full: clean $(RM) *.*~ $(RM) *~ - $(RM) *.o + diff --git a/src/log.cpp b/src/log.cpp index 699036b..3af4aca 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -75,13 +75,13 @@ void LogEx(int level, const char * funct, const char * file, int line, ...) severity = "WARNING"; break; case LOG_NOTICE: - severity = "NOTICE"; + severity = "notice"; break; case LOG_INFO: - severity = "INFO"; + severity = "info"; break; default: - severity = "DEBUG"; + severity = "debug"; break; } diff --git a/src/main.cpp b/src/main.cpp index 273d82a..72f29e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,26 +1,16 @@ -#include "common.h" - -#include "document.h" -#include "view.h" -#include "screen.h" - -using namespace std; -using namespace IPDF; - +#include "main.h" +#include // Because we can. int main(int argc, char ** argv) -{ +{ Document doc; - doc.Add(0.5, 0.5, 0.5, 0.5); - - View view(doc); - - Screen scr; - - while (scr.PumpEvents()) + if (argc > 1) { - view.Render(); - scr.Present(); + for (int i = 2; i < argc; ++i) + { + if (fork() == 0) doc.Load(argv[i]); + } + doc.Load(argv[1]); } - + MainLoop(doc); return 0; } diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..ee8076a --- /dev/null +++ b/src/main.h @@ -0,0 +1,20 @@ +#include "common.h" + +#include "document.h" +#include "view.h" +#include "screen.h" + + +using namespace std; +using namespace IPDF; + +inline void MainLoop(Document & doc) +{ + View view(doc); + Screen scr; + while (scr.PumpEvents()) + { + view.Render(); + scr.Present(); + } +} diff --git a/src/tests/runtests.sh b/src/tests/runtests.sh new file mode 100755 index 0000000..e99cc1e --- /dev/null +++ b/src/tests/runtests.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +for source in *.cpp; do + t=${source%.cpp} + make -C ../ tests/$t 1>>/dev/null + (echo -n "Running $t.test ... ") 1>&2 + ./$t.test 2>$t.err 1>$t.out + if [ $? -ne 0 ]; then + (echo " FAILURE! Press enter to see stderr") 1>&2 + read + less $t.err + (echo "Stopping tests after failure.") 1>&2 + exit 1 + fi + (echo "Success!") 1>&2 +done diff --git a/src/tests/saveload.cpp b/src/tests/saveload.cpp index 32dd1d7..60bfda6 100644 --- a/src/tests/saveload.cpp +++ b/src/tests/saveload.cpp @@ -1,42 +1,46 @@ -#include "../common.h" - -#include "../document.h" -#include "../view.h" -#include "../screen.h" - -using namespace std; -using namespace IPDF; - +#include "main.h" +#include unsigned test_objects = 4; +void Cleanup() +{ + unlink("saveload.ipdf"); +} + int main(int argc, char ** argv) { + Debug("TEST STARTING %s", argv[0]); + atexit(Cleanup); srand(time(NULL)); Document doc; for (unsigned id = 0; id < test_objects; ++id) { doc.Add((ObjectType)(rand() % 2), Rect(Random(), Random(), Random(), Random())); } - doc.Save("test.ipdf"); + doc.Save("saveload.ipdf"); - Document equ("test.ipdf"); + Document equ("saveload.ipdf"); //doc.Add(Random(), Random(), Random(), Random()); if (doc != equ || equ != doc) { Error("Loaded document is not equivelant to saved document!"); doc.DebugDumpObjects(); equ.DebugDumpObjects(); + Fatal("TEST FAILED"); } - - View view(doc); - Screen scr; - - while (scr.PumpEvents()) + doc.Add((ObjectType)(0), Rect()); + if (doc == equ) { - view.Render(); - scr.Present(); + Error("Modified document is still equilant to saved document!?"); + doc.DebugDumpObjects(); + equ.DebugDumpObjects(); + Fatal("TEST FAILED"); } + Debug("TEST SUCCESSFUL"); + // Cleanup return 0; } + +