]> git.localhorst.tv Git - l2e.git/commitdiff
added first unit test
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 27 Dec 2012 06:44:08 +0000 (07:44 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 27 Dec 2012 06:44:08 +0000 (07:44 +0100)
.gitignore
build/config.mk
build/l2e.mk
build/targets.mk
readme
tests/all.cpp [new file with mode: 0644]
tests/geometry/VectorTest.cpp [new file with mode: 0644]
tests/geometry/VectorTest.h [new file with mode: 0644]

index d266c4eab2bccaec02aad4a0bfe62596cf65e7a9..271fd2889027a4f1ab56e9af2d78c880b1eee270 100644 (file)
@@ -6,6 +6,7 @@
 *.l2o
 /build/*/l2e
 /build/*/local.mk
+/build/*/test-all
 shots/
 bin/*
 l2e.depend
index 899e36329e5c0086e16c20da2c03ecacc31257fa..9c7dccff2b1c2470e5f2a09e29a8a5aa986c290f 100644 (file)
@@ -23,6 +23,9 @@ SDL_LIBS = $(shell pkg-config --libs sdl)
 SDL_IMG_FLAGS = $(shell pkg-config --cflags SDL_image)
 SDL_IMG_LIBS = $(shell pkg-config --libs SDL_image)
 
+CPPUNIT_FLAGS = $(shell pkg-config --cflags cppunit)
+CPPUNIT_LIBS = $(shell pkg-config --libs cppunit)
+
 # set to empty to show tool invocations
 VERBOSE = @
 
index 19d3f5b15211ae2a9fd338a0719fbf1cea4f29d4..cfc4e745be50a7d02e738415790f83d0a2ee5d62 100644 (file)
@@ -1,9 +1,15 @@
-L2E_DIRS := $(shell cd $(srcdir) && find * -type d)
-L2E_SRCS := $(shell cd $(srcdir)&& find * -type f -name '*.cpp')
+L2E_DIRS := $(shell cd $(TOP) && find src -type d)
+L2E_SRCS := $(shell cd $(TOP) && find src -type f -name '*.cpp')
 L2E_DEPS = $(L2E_SRCS:%.cpp=%.d)
 L2E_OBJS = $(L2E_SRCS:%.cpp=%.o)
 L2E_EXES = l2e
 
+L2E_TEST_DIRS := $(shell cd $(TOP) && find tests -type d)
+L2E_TEST_SRCS := $(shell cd $(TOP) && find tests -type f -name '*.cpp')
+L2E_TEST_DEPS := $(L2E_TEST_SRCS:%.cpp=%.d)
+L2E_TEST_OBJS := $(L2E_TEST_SRCS:%.cpp=%.o)
+L2E_TEST_EXES := test-all
+
 L2E_FLAGS = $(sort $(strip \
                $(SDL_FLAGS) \
                $(SDL_IMG_FLAGS) \
@@ -13,9 +19,13 @@ L2E_LIBS = $(sort $(strip \
                $(SDL_IMG_LIBS) \
                ))
 
+L2E_TEST_FLAGS = $(L2E_FLAGS) $(CPPUNIT_FLAGS)
+L2E_TEST_LIBS = $(L2E_LIBS) $(CPPUNIT_LIBS)
+
 -include $(L2E_DEPS)
+-include $(L2E_TEST_DEPS)
 
-$(L2E_OBJS): %.o: $(srcdir)/%.cpp
+$(L2E_OBJS): %.o: $(TOP)/%.cpp
        -@$(MKDIR) "$(@D)"
        @echo "compile: $@"
        $(VERBOSE) $(CXX) -c -o "$@" -MMD -MP -MF"$*.d" -MT"$@" "$<" \
@@ -27,17 +37,41 @@ $(L2E_EXES): $(L2E_OBJS)
        $(VERBOSE) $(CXX) -o "$@" $^ \
                        $(L2E_FLAGS) $(L2E_LIBS) $(LDFLAGS)
 
+$(L2E_TEST_OBJS): %.o: $(TOP)/%.cpp
+       -@$(MKDIR) "$(@D)"
+       @echo "compile: $@"
+       $(VERBOSE) $(CXX) -c -o "$@" -MMD -MP -MF"$*.d" -MT"$@" "$<" \
+                       $(L2E_TEST_FLAGS) $(CPPFLAGS) $(CXXFLAGS)
+
+$(L2E_TEST_EXES): $(L2E_TEST_OBJS) $(filter-out src/main.o, $(L2E_OBJS))
+       -@$(MKDIR) "$(@D)"
+       @echo "link: $@"
+       $(VERBOSE) $(CXX) -o "$@" $^ \
+                       $(L2E_TEST_FLAGS) $(L2E_TEST_LIBS) $(LDFLAGS)
+
 l2e-all: $(L2E_EXES)
 
 l2e-clean:
        $(VERBOSE) -$(RM) $(L2E_DEPS)
        $(VERBOSE) -$(RM) $(L2E_OBJS)
        $(VERBOSE) -$(RM) $(L2E_EXES)
+       $(VERBOSE) -$(RM) $(L2E_TEST_DEPS)
+       $(VERBOSE) -$(RM) $(L2E_TEST_OBJS)
+       $(VERBOSE) -$(RM) $(L2E_TEST_EXES)
        $(VERBOSE) -$(RMDIR) $(L2E_DIRS)
+       $(VERBOSE) -$(RMDIR) $(L2E_TEST_DIRS)
+
+l2e-tests: $(L2E_TEST_EXES)
+
+l2e-test-all: test-all
+       @echo "test: test-all"
+       $(VERBOSE) ./test-all
 
 all: l2e-all
 clean: l2e-clean
+tests: l2e-tests
+test: l2e-test-all
 
-.PHONY: l2e-all l2e-clean
+.PHONY: l2e-all l2e-clean l2e-tests l2e-test-all
 
 -include $(BUILD)l2e-local.mk
index 2cdfb7c04b4e7eb3e91f80c3a9194b78edd38b2b..5793443fda5609f05893d23a93d2ffcd6cc5f764 100644 (file)
@@ -3,4 +3,8 @@ all:
 clean:
        @echo "clean"
 
-.PHONY: all clean
+tests:
+
+test:
+
+.PHONY: all clean tests test
diff --git a/readme b/readme
index cfef816069af088ba1646e75ca6f5858bbbebcb3..ff710ac8ed632975ad7ed2a199e2dd498da3da9d 100644 (file)
--- a/readme
+++ b/readme
@@ -3,7 +3,7 @@ About
        Its long-term goal is feature-completeness with Lufia 2.
 
 Quickstart
-       cd build/release; make all; ./2e ../../test-data/*.l2s
+       cd build/release; make all; ./l2e ../../test-data/*.l2s
 
 Building
        To build the binary, simply enter the desired build configuration directory
@@ -23,6 +23,14 @@ Building
         * Build confiuration specific settings should go into
           `/build/$config/local.mk', e.g. `/build/debug/local.mk'.
 
+       Unit tests can be built with `make tests' and run with `make test'.
+       If a unit test should fail for you, please file a bug at
+       http://luke.redirectme.net/redmine/projects/l2e/issues/new and describe
+        * your architecture and operating system,
+        * versions of the libs,
+        * and which test(s) failed.
+       Patches are also welcome :)
+
 Dependencies
        Runtime
         * SDL
@@ -34,6 +42,8 @@ Dependencies
         * pkg-config
         * and a number of core utilities such as rm, mkdir, and an sh-compatible
           shell.
+       Building and running unit tests
+        * cppunit
 
 Launching
        The current version required huge amounts of symbols to be defined and does
diff --git a/tests/all.cpp b/tests/all.cpp
new file mode 100644 (file)
index 0000000..e7ea831
--- /dev/null
@@ -0,0 +1,31 @@
+#include "../src/sdl/InitImage.h"
+#include "../src/sdl/InitSDL.h"
+
+#include <iostream>
+#include <SDL.h>
+#include <SDL_image.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+
+using CppUnit::TestFactoryRegistry;
+using CppUnit::TextUi::TestRunner;
+
+
+int main(int argc, char **argv) {
+
+       try {
+               sdl::InitSDL sdl;
+               sdl::InitImage image(IMG_INIT_PNG);
+
+               TestRunner runner;
+               TestFactoryRegistry &registry = TestFactoryRegistry::getRegistry();
+               runner.addTest(registry.makeTest());
+               runner.run();
+       } catch (std::exception &e) {
+               std::cerr << "exception in main(): " << e.what() << std::endl;
+               return 1;
+       }
+
+       return 0;
+
+}
diff --git a/tests/geometry/VectorTest.cpp b/tests/geometry/VectorTest.cpp
new file mode 100644 (file)
index 0000000..7ba58d2
--- /dev/null
@@ -0,0 +1,127 @@
+#include "VectorTest.h"
+
+#include <limits>
+
+CPPUNIT_TEST_SUITE_REGISTRATION(test_geometry::VectorTest);
+
+using geometry::Vector;
+
+
+namespace test_geometry {
+
+void VectorTest::setUp() {
+
+}
+
+void VectorTest::tearDown() {
+
+}
+
+
+void VectorTest::testComparison() {
+       CPPUNIT_ASSERT_EQUAL(Vector<int>(0, 0), Vector<int>(0, 0));
+
+       CPPUNIT_ASSERT(Vector<int>(0, 0) != Vector<int>(0, 1));
+       CPPUNIT_ASSERT(Vector<int>(0, 0) != Vector<int>(1, 0));
+       CPPUNIT_ASSERT(Vector<int>(0, 0) != Vector<int>(1, 1));
+}
+
+void VectorTest::testSum() {
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(1, 1),
+                       Vector<int>(1, 1) + Vector<int>(0, 0));
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(1, 1),
+                       Vector<int>(0, 1) + Vector<int>(1, 0));
+
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(1, 1),
+                       Vector<int>(1, 1) - Vector<int>(0, 0));
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(0, 1),
+                       Vector<int>(1, 1) - Vector<int>(1, 0));
+}
+
+void VectorTest::testProduct() {
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(2, 3),
+                       Vector<int>(2, 3) * Vector<int>(1, 1));
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(10, 12),
+                       Vector<int>(2, 3) * Vector<int>(5, 4));
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(2, 3),
+                       Vector<int>(2, 3) * 1);
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(4, 6),
+                       Vector<int>(2, 3) * 2);
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(2, 3),
+                       1 * Vector<int>(2, 3));
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(4, 6),
+                       2 * Vector<int>(2, 3));
+
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(10, 12),
+                       Vector<int>(10, 12) / Vector<int>(1, 1));
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(2, 3),
+                       Vector<int>(10, 12) / Vector<int>(5, 4));
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(4, 6),
+                       Vector<int>(4, 6) / 1);
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(2, 3),
+                       Vector<int>(4, 6) / 2);
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(3, 4),
+                       12 / Vector<int>(4, 3));
+}
+
+void VectorTest::testModulo() {
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(0, 0),
+                       Vector<int>(1, 2) % Vector<int>(1, 1));
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(0, 0),
+                       Vector<int>(1, 2) % 1);
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(3, 1),
+                       Vector<int>(3, 5) % Vector<int>(4, 2));
+       CPPUNIT_ASSERT_EQUAL(
+                       Vector<int>(3, 1),
+                       Vector<int>(3, 5) % 4);
+
+       Vector<float> vecf = Vector<float>(4.0f, 5.0f) % Vector<float>(2.0f, 3.0f);
+       CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0f, vecf.X(), std::numeric_limits<float>::epsilon());
+       CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0f, vecf.Y(), std::numeric_limits<float>::epsilon());
+
+       vecf = Vector<float>(4.0f, 5.0f) % 4.0f;
+       CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0f, vecf.X(), std::numeric_limits<float>::epsilon());
+       CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0f, vecf.Y(), std::numeric_limits<float>::epsilon());
+
+       Vector<double> vecd = Vector<double>(4.0, 5.0) % Vector<double>(2.0, 3.0);
+       CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, vecd.X(), std::numeric_limits<double>::epsilon());
+       CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0, vecd.Y(), std::numeric_limits<double>::epsilon());
+
+       vecd = Vector<double>(4.0, 5.0) % 4.0;
+       CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, vecd.X(), std::numeric_limits<double>::epsilon());
+       CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, vecd.Y(), std::numeric_limits<double>::epsilon());
+}
+
+void VectorTest::testFunctional() {
+       const Vector<int> vec(2, 5);
+       CPPUNIT_ASSERT_EQUAL(2, vec.X());
+       CPPUNIT_ASSERT_EQUAL(5, vec.Y());
+       CPPUNIT_ASSERT_EQUAL(52, vec.Index(10));
+
+       const Vector<int> indexVec = Vector<int>::FromIndex(52, 10);
+       CPPUNIT_ASSERT_EQUAL(vec, indexVec);
+
+       Vector<int> lockedVec = Vector<int>(vec);
+       lockedVec.Lock(Vector<int>(2, 2));
+       CPPUNIT_ASSERT_EQUAL(Vector<int>(2, 4), lockedVec);
+}
+
+}
diff --git a/tests/geometry/VectorTest.h b/tests/geometry/VectorTest.h
new file mode 100644 (file)
index 0000000..e6e2b29
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef TEST_GEOMETRY_VECTORTEST_H_
+#define TEST_GEOMETRY_VECTORTEST_H_
+
+#include "../../src/geometry/Vector.h"
+
+#include <cppunit/extensions/HelperMacros.h>
+
+
+namespace test_geometry {
+
+class VectorTest
+: public CppUnit::TestFixture {
+
+CPPUNIT_TEST_SUITE(VectorTest);
+CPPUNIT_TEST(testComparison);
+CPPUNIT_TEST(testSum);
+CPPUNIT_TEST(testProduct);
+CPPUNIT_TEST(testModulo);
+CPPUNIT_TEST(testFunctional);
+CPPUNIT_TEST_SUITE_END();
+
+public:
+       void setUp();
+       void tearDown();
+
+       void testComparison();
+       void testSum();
+       void testProduct();
+       void testModulo();
+       void testFunctional();
+
+};
+
+}
+
+#endif