Tweak Makefile to make testing nicer
authorSam Moore <[email protected]>
Wed, 26 Mar 2014 16:17:12 +0000 (00:17 +0800)
committerSam Moore <[email protected]>
Wed, 26 Mar 2014 16:17:12 +0000 (00:17 +0800)
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.

.gitignore
src/Makefile
src/log.cpp
src/main.cpp
src/main.h [new file with mode: 0644]
src/tests/runtests.sh [new file with mode: 0755]
src/tests/saveload.cpp

index 82affd5..365a631 100644 (file)
@@ -2,3 +2,6 @@
 *.d
 *~
 *.ipdf
+*.test
+*.out
+*.err
index 3e8d8a0..0a1bc96 100644 (file)
@@ -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 [email protected] $(LINKOBJ) ../obj/[email protected] $(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
+
 
 
        
index 699036b..3af4aca 100644 (file)
@@ -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;
        }
 
index 273d82a..72f29e3 100644 (file)
@@ -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 <unistd.h> // 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 (file)
index 0000000..ee8076a
--- /dev/null
@@ -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 (executable)
index 0000000..e99cc1e
--- /dev/null
@@ -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
index 32dd1d7..60bfda6 100644 (file)
@@ -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 <unistd.h>
 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;
 }
+
+

UCC git Repository :: git.ucc.asn.au