From ef2496b3cb7ce66b7f831278be66261834b732e5 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 27 Dec 2012 07:44:08 +0100 Subject: [PATCH] added first unit test --- .gitignore | 1 + build/config.mk | 3 + build/l2e.mk | 42 +++++++++-- build/targets.mk | 6 +- readme | 12 +++- tests/all.cpp | 31 +++++++++ tests/geometry/VectorTest.cpp | 127 ++++++++++++++++++++++++++++++++++ tests/geometry/VectorTest.h | 36 ++++++++++ 8 files changed, 252 insertions(+), 6 deletions(-) create mode 100644 tests/all.cpp create mode 100644 tests/geometry/VectorTest.cpp create mode 100644 tests/geometry/VectorTest.h diff --git a/.gitignore b/.gitignore index d266c4e..271fd28 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ *.l2o /build/*/l2e /build/*/local.mk +/build/*/test-all shots/ bin/* l2e.depend diff --git a/build/config.mk b/build/config.mk index 899e363..9c7dccf 100644 --- a/build/config.mk +++ b/build/config.mk @@ -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 = @ diff --git a/build/l2e.mk b/build/l2e.mk index 19d3f5b..cfc4e74 100644 --- a/build/l2e.mk +++ b/build/l2e.mk @@ -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 diff --git a/build/targets.mk b/build/targets.mk index 2cdfb7c..5793443 100644 --- a/build/targets.mk +++ b/build/targets.mk @@ -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 cfef816..ff710ac 100644 --- 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 index 0000000..e7ea831 --- /dev/null +++ b/tests/all.cpp @@ -0,0 +1,31 @@ +#include "../src/sdl/InitImage.h" +#include "../src/sdl/InitSDL.h" + +#include +#include +#include +#include +#include + +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 ®istry = 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 index 0000000..7ba58d2 --- /dev/null +++ b/tests/geometry/VectorTest.cpp @@ -0,0 +1,127 @@ +#include "VectorTest.h" + +#include + +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(0, 0), Vector(0, 0)); + + CPPUNIT_ASSERT(Vector(0, 0) != Vector(0, 1)); + CPPUNIT_ASSERT(Vector(0, 0) != Vector(1, 0)); + CPPUNIT_ASSERT(Vector(0, 0) != Vector(1, 1)); +} + +void VectorTest::testSum() { + CPPUNIT_ASSERT_EQUAL( + Vector(1, 1), + Vector(1, 1) + Vector(0, 0)); + CPPUNIT_ASSERT_EQUAL( + Vector(1, 1), + Vector(0, 1) + Vector(1, 0)); + + CPPUNIT_ASSERT_EQUAL( + Vector(1, 1), + Vector(1, 1) - Vector(0, 0)); + CPPUNIT_ASSERT_EQUAL( + Vector(0, 1), + Vector(1, 1) - Vector(1, 0)); +} + +void VectorTest::testProduct() { + CPPUNIT_ASSERT_EQUAL( + Vector(2, 3), + Vector(2, 3) * Vector(1, 1)); + CPPUNIT_ASSERT_EQUAL( + Vector(10, 12), + Vector(2, 3) * Vector(5, 4)); + CPPUNIT_ASSERT_EQUAL( + Vector(2, 3), + Vector(2, 3) * 1); + CPPUNIT_ASSERT_EQUAL( + Vector(4, 6), + Vector(2, 3) * 2); + CPPUNIT_ASSERT_EQUAL( + Vector(2, 3), + 1 * Vector(2, 3)); + CPPUNIT_ASSERT_EQUAL( + Vector(4, 6), + 2 * Vector(2, 3)); + + CPPUNIT_ASSERT_EQUAL( + Vector(10, 12), + Vector(10, 12) / Vector(1, 1)); + CPPUNIT_ASSERT_EQUAL( + Vector(2, 3), + Vector(10, 12) / Vector(5, 4)); + CPPUNIT_ASSERT_EQUAL( + Vector(4, 6), + Vector(4, 6) / 1); + CPPUNIT_ASSERT_EQUAL( + Vector(2, 3), + Vector(4, 6) / 2); + CPPUNIT_ASSERT_EQUAL( + Vector(3, 4), + 12 / Vector(4, 3)); +} + +void VectorTest::testModulo() { + CPPUNIT_ASSERT_EQUAL( + Vector(0, 0), + Vector(1, 2) % Vector(1, 1)); + CPPUNIT_ASSERT_EQUAL( + Vector(0, 0), + Vector(1, 2) % 1); + CPPUNIT_ASSERT_EQUAL( + Vector(3, 1), + Vector(3, 5) % Vector(4, 2)); + CPPUNIT_ASSERT_EQUAL( + Vector(3, 1), + Vector(3, 5) % 4); + + Vector vecf = Vector(4.0f, 5.0f) % Vector(2.0f, 3.0f); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0f, vecf.X(), std::numeric_limits::epsilon()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0f, vecf.Y(), std::numeric_limits::epsilon()); + + vecf = Vector(4.0f, 5.0f) % 4.0f; + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0f, vecf.X(), std::numeric_limits::epsilon()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0f, vecf.Y(), std::numeric_limits::epsilon()); + + Vector vecd = Vector(4.0, 5.0) % Vector(2.0, 3.0); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, vecd.X(), std::numeric_limits::epsilon()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0, vecd.Y(), std::numeric_limits::epsilon()); + + vecd = Vector(4.0, 5.0) % 4.0; + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, vecd.X(), std::numeric_limits::epsilon()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, vecd.Y(), std::numeric_limits::epsilon()); +} + +void VectorTest::testFunctional() { + const Vector vec(2, 5); + CPPUNIT_ASSERT_EQUAL(2, vec.X()); + CPPUNIT_ASSERT_EQUAL(5, vec.Y()); + CPPUNIT_ASSERT_EQUAL(52, vec.Index(10)); + + const Vector indexVec = Vector::FromIndex(52, 10); + CPPUNIT_ASSERT_EQUAL(vec, indexVec); + + Vector lockedVec = Vector(vec); + lockedVec.Lock(Vector(2, 2)); + CPPUNIT_ASSERT_EQUAL(Vector(2, 4), lockedVec); +} + +} diff --git a/tests/geometry/VectorTest.h b/tests/geometry/VectorTest.h new file mode 100644 index 0000000..e6e2b29 --- /dev/null +++ b/tests/geometry/VectorTest.h @@ -0,0 +1,36 @@ +#ifndef TEST_GEOMETRY_VECTORTEST_H_ +#define TEST_GEOMETRY_VECTORTEST_H_ + +#include "../../src/geometry/Vector.h" + +#include + + +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 -- 2.39.2