]> git.localhorst.tv Git - blank.git/commitdiff
fix faceset unset
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 11 Jun 2015 12:34:41 +0000 (14:34 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 11 Jun 2015 13:28:23 +0000 (15:28 +0200)
unit tests. oh yeah

.gitignore
Makefile
src/world/Block.hpp
tst/test.cpp [new file with mode: 0644]
tst/world/BlockTest.cpp [new file with mode: 0644]
tst/world/BlockTest.hpp [new file with mode: 0644]

index d599a315e946d9f7ffa8f1bbb9ac715230ac7cd0..4c408d07a9981a8d424fea5fb51cd1547939e4fc 100644 (file)
@@ -3,6 +3,7 @@
 blank
 blank.debug
 blank.profile
+blank.test
 build
 cachegrind.out.*
 callgrind.out.*
index d063a9a75befe1793abccf50fcac8bcd010476c2..d0a3083a97a225c8026aa0a6c9cff8191967a7ff 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,6 +5,8 @@ LIBS = sdl2 SDL2_image glew
 
 PKGFLAGS := $(shell pkg-config --cflags $(LIBS))
 PKGLIBS := $(shell pkg-config --libs $(LIBS))
+TESTFLAGS := $(shell pkg-config --cflags cppunit)
+TESTLIBS := $(shell pkg-config --libs cppunit)
 
 CPPFLAGS ?=
 CPPFLAGS += $(PKGFLAGS)
@@ -16,28 +18,35 @@ LDXXFLAGS += $(PKGLIBS)
 DEBUG_FLAGS = -g3 -O0
 PROFILE_FLAGS = -DNDEBUG -O1 -g3
 RELEASE_FLAGS = -DNDEBUG -O2
+TEST_FLAGS = -g -O2 -I./src $(TESTFLAGS)
 
 SOURCE_DIR := src
+TEST_SRC_DIR := tst
 DEBUG_DIR := build/debug
 PROFILE_DIR := build/profile
 RELEASE_DIR := build/release
-DIR := $(RELEASE_DIR) $(DEBUG_DIR) $(PROFILE_DIR) build
+TEST_DIR := build/test
+DIR := $(RELEASE_DIR) $(DEBUG_DIR) $(PROFILE_DIR) $(TEST_DIR) build
 
 LIB_SRC := $(wildcard $(SOURCE_DIR)/*/*.cpp)
 BIN_SRC := $(wildcard $(SOURCE_DIR)/*.cpp)
 SRC := $(LIB_SRC) $(BIN_SRC)
+TEST_SRC := $(wildcard $(TEST_SRC_DIR)/*.cpp) $(wildcard $(TEST_SRC_DIR)/*/*.cpp)
 RELEASE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(RELEASE_DIR)/%.o, $(SRC))
 DEBUG_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(DEBUG_DIR)/%.o, $(SRC))
 PROFILE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(PROFILE_DIR)/%.o, $(SRC))
+TEST_OBJ := $(patsubst $(TEST_SRC_DIR)/%.cpp, $(TEST_DIR)/%.o, $(TEST_SRC)) $(patsubst $(SOURCE_DIR)/%.cpp, $(TEST_DIR)/src/%.o, $(LIB_SRC))
 RELEASE_DEP := $(RELEASE_OBJ:.o=.d)
 DEBUG_DEP := $(DEBUG_OBJ:.o=.d)
 PROFILE_DEP := $(PROFILE_OBJ:.o=.d)
+TEST_DEP := $(TEST_OBJ:.o=.d)
 RELEASE_BIN := blank
 DEBUG_BIN := blank.debug
 PROFILE_BIN := blank.profile
-OBJ := $(RELEASE_OBJ) $(DEBUG_OBJ) $(PROFILE_OBJ)
-DEP := $(RELEASE_DEP) $(DEBUG_DEP) $(PROFILE_DEP)
-BIN := $(RELEASE_BIN) $(DEBUG_BIN) $(PROFILE_BIN)
+TEST_BIN := blank.test
+OBJ := $(RELEASE_OBJ) $(DEBUG_OBJ) $(PROFILE_OBJ) $(TEST_OBJ)
+DEP := $(RELEASE_DEP) $(DEBUG_DEP) $(PROFILE_DEP) $(TEST_DEP)
+BIN := $(RELEASE_BIN) $(DEBUG_BIN) $(PROFILE_BIN) $(TEST_BIN)
 
 release: $(RELEASE_BIN)
 
@@ -47,6 +56,8 @@ debug: $(DEBUG_BIN)
 
 profile: $(PROFILE_BIN)
 
+tests: $(TEST_BIN)
+
 run: blank
        ./blank
 
@@ -63,13 +74,16 @@ callgrind: blank.profile
                --dump-instr=yes --simulate-hwpref=yes --simulate-wb=yes \
                ./blank.profile -n 128 -t 16 --no-keyboard --no-mouse -d --no-vsync
 
+test: blank.test
+       ./blank.test
+
 clean:
        rm -df $(OBJ) $(DEP) $(DIR)
 
 distclean: clean
        rm -f $(BIN) cachegrind.out.* callgrind.out.*
 
-.PHONY: all release debug profile run gdb cachegrind callgrind clean distclean
+.PHONY: all release debug profile tests run gdb cachegrind callgrind test clean distclean
 
 -include $(DEP)
 
@@ -85,6 +99,10 @@ $(PROFILE_BIN): $(PROFILE_OBJ)
        @echo link: $@
        @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(PROFILE_FLAGS) $^
 
+$(TEST_BIN): $(TEST_OBJ)
+       @echo link: $@
+       @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(TESTLIBS) $(TEST_FLAGS) $^
+
 $(RELEASE_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(RELEASE_DIR)
        @mkdir -p "$(@D)"
        @echo compile: $@
@@ -100,5 +118,15 @@ $(PROFILE_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(PROFILE_DIR)
        @echo compile: $@
        @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(PROFILE_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $<
 
+$(TEST_DIR)/%.o: $(TEST_SRC_DIR)/%.cpp | $(TEST_DIR)
+       @mkdir -p "$(@D)"
+       @echo compile: $@
+       @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(TEST_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $<
+
+$(TEST_DIR)/src/%.o: $(SOURCE_DIR)/%.cpp | $(TEST_DIR)
+       @mkdir -p "$(@D)"
+       @echo compile: $@
+       @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(TEST_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $<
+
 $(DIR):
        @mkdir -p "$@"
index 74af3e4ab4bbf025e342674e6bc7eff986bf2d7a..9fd545b3813ce5c24e244e42bbb593b75606e690 100644 (file)
@@ -98,7 +98,7 @@ struct Block {
                        value |= Mask(f);
                }
                void Unset(Face f) {
-                       value |= ~Mask(f);
+                       value &= ~Mask(f);
                }
 
                void Clear() {
diff --git a/tst/test.cpp b/tst/test.cpp
new file mode 100644 (file)
index 0000000..da6578e
--- /dev/null
@@ -0,0 +1,17 @@
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+
+using CppUnit::TestFactoryRegistry;
+using CppUnit::TextUi::TestRunner;
+
+
+int main(int, char **) {
+
+       TestRunner runner;
+       TestFactoryRegistry &registry = TestFactoryRegistry::getRegistry();
+       runner.addTest(registry.makeTest());
+       runner.run();
+
+       return 0;
+
+}
diff --git a/tst/world/BlockTest.cpp b/tst/world/BlockTest.cpp
new file mode 100644 (file)
index 0000000..b10e9c9
--- /dev/null
@@ -0,0 +1,213 @@
+#include "BlockTest.hpp"
+
+#include "world/Block.hpp"
+
+#include <glm/gtx/io.hpp>
+
+CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::BlockTest);
+
+
+namespace blank {
+namespace test {
+
+void BlockTest::setUp() {
+}
+
+void BlockTest::tearDown() {
+}
+
+
+void BlockTest::testFaceOpposite() {
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "DOWN not opposite of UP",
+               Block::FACE_DOWN, Block::Opposite(Block::FACE_UP)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "UP not opposite of DOWN",
+               Block::FACE_UP, Block::Opposite(Block::FACE_DOWN)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "LEFT not opposite of RIGHT",
+               Block::FACE_LEFT, Block::Opposite(Block::FACE_RIGHT)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "RIGHT not opposite of LEFT",
+               Block::FACE_RIGHT, Block::Opposite(Block::FACE_LEFT)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "BACK not opposite of FRONT",
+               Block::FACE_BACK, Block::Opposite(Block::FACE_FRONT)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "FRONT not opposite of BACk",
+               Block::FACE_FRONT, Block::Opposite(Block::FACE_BACK)
+       );
+}
+
+void BlockTest::testFaceAxis() {
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "1/Y not axis of UP",
+               1, Block::Axis(Block::FACE_UP)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "1/Y not axis of DOWN",
+               1, Block::Axis(Block::FACE_DOWN)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "0/X not axis of RIGHT",
+               0, Block::Axis(Block::FACE_RIGHT)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "0/X not axis of LEFT",
+               0, Block::Axis(Block::FACE_LEFT)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "2/Z not axis of FRONT",
+               2, Block::Axis(Block::FACE_FRONT)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "2/Z not axis of BACK",
+               2, Block::Axis(Block::FACE_BACK)
+       );
+}
+
+void BlockTest::testFaceNormal() {
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "[ 0, 1, 0 ] not normal of UP",
+               glm::tvec3<int>(0, 1, 0), Block::FaceNormal(Block::FACE_UP)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "[ 0, -1, 0 ] not normal of DOWN",
+               glm::tvec3<int>(0, -1, 0), Block::FaceNormal(Block::FACE_DOWN)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "[ 1, 0, 0 ] not normal of RIGHT",
+               glm::tvec3<int>(1, 0, 0), Block::FaceNormal(Block::FACE_RIGHT)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "[ -1, 0, 0 ] not normal of LEFT",
+               glm::tvec3<int>(-1, 0, 0), Block::FaceNormal(Block::FACE_LEFT)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "[ 0, 0, 1 ] not normal of FRONT",
+               glm::tvec3<int>(0, 0, 1), Block::FaceNormal(Block::FACE_FRONT)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "[ 0, 0, -1 ] not normal of BACK",
+               glm::tvec3<int>(0, 0, -1), Block::FaceNormal(Block::FACE_BACK)
+       );
+}
+
+void BlockTest::testNormalFace() {
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "UP not face of [ 0, 1, 0 ]",
+               Block::FACE_UP, Block::NormalFace(glm::vec3(0, 1, 0))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "DOWN not face of [ 0, -1, 0 ]",
+               Block::FACE_DOWN, Block::NormalFace(glm::vec3(0, -1, 0))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "RIGHT not face of [ 1, 0, 0 ]",
+               Block::FACE_RIGHT, Block::NormalFace(glm::vec3(1, 0, 0))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "LEFT not face of [ -1, 0, 0 ]",
+               Block::FACE_LEFT, Block::NormalFace(glm::vec3(-1, 0, 0))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "FRONT not face of [ 0, 0, 1 ]",
+               Block::FACE_FRONT, Block::NormalFace(glm::vec3(0, 0, 1))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "BACK not face of [ 0, 0, -1 ]",
+               Block::FACE_BACK, Block::NormalFace(glm::vec3(0, 0, -1))
+       );
+}
+
+
+void BlockTest::testFaceSet() {
+       Block::FaceSet set;
+       CPPUNIT_ASSERT_MESSAGE(
+               "default faceset not empty",
+               set.Empty()
+       );
+       for (int i = 0; i < Block::FACE_COUNT; ++i) {
+               CPPUNIT_ASSERT_MESSAGE(
+                       "something set on default faceset",
+                       !set.IsSet(Block::Face(i))
+               );
+       }
+       set.Fill();
+       CPPUNIT_ASSERT_MESSAGE(
+               "not all set on filled faceset",
+               set.All()
+       );
+       for (int i = 0; i < Block::FACE_COUNT; ++i) {
+               CPPUNIT_ASSERT_MESSAGE(
+                       "something not set on filled faceset",
+                       set.IsSet(Block::Face(i))
+               );
+       }
+       set.Clear();
+       CPPUNIT_ASSERT_MESSAGE(
+               "cleared faceset not empty",
+               set.Empty()
+       );
+       for (int i = 0; i < Block::FACE_COUNT; ++i) {
+               CPPUNIT_ASSERT_MESSAGE(
+                       "something set on cleared faceset",
+                       !set.IsSet(Block::Face(i))
+               );
+               set.Set(Block::Face(i));
+               CPPUNIT_ASSERT_MESSAGE(
+                       "face not set after setting",
+                       set.IsSet(Block::Face(i))
+               );
+       }
+       CPPUNIT_ASSERT_MESSAGE(
+               "set not filled after setting all faces",
+               set.All()
+       );
+       for (int i = 0; i < Block::FACE_COUNT; ++i) {
+               CPPUNIT_ASSERT_MESSAGE(
+                       "something not set after setting all faces",
+                       set.IsSet(Block::Face(i))
+               );
+               set.Unset(Block::Face(i));
+               CPPUNIT_ASSERT_MESSAGE(
+                       "face set after unsetting",
+                       !set.IsSet(Block::Face(i))
+               );
+       }
+       CPPUNIT_ASSERT_MESSAGE(
+               "set not empty after unsetting all faces",
+               set.Empty()
+       );
+}
+
+
+void BlockTest::testDefaultBlock() {
+       Block block;
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "0 type id of default block",
+               Block::Type(0), block.type
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "identity transform of default block",
+               glm::mat4(1), block.Transform()
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "no turn of default block",
+               Block::TURN_NONE, block.GetTurn()
+       );
+       for (int i = 0; i < Block::FACE_COUNT; ++i) {
+               CPPUNIT_ASSERT_EQUAL_MESSAGE(
+                       "face is oriented face of default block",
+                       Block::Face(i), block.OrientedFace(Block::Face(i))
+               );
+       }
+}
+
+}
+}
diff --git a/tst/world/BlockTest.hpp b/tst/world/BlockTest.hpp
new file mode 100644 (file)
index 0000000..467d260
--- /dev/null
@@ -0,0 +1,44 @@
+#ifndef BLANK_TEST_WORLD_BLOCKTEST_H_
+#define BLANK_TEST_WORLD_BLOCKTEST_H_
+
+#include <cppunit/extensions/HelperMacros.h>
+
+
+namespace blank {
+namespace test {
+
+class BlockTest
+: public CppUnit::TestFixture {
+
+CPPUNIT_TEST_SUITE(BlockTest);
+
+CPPUNIT_TEST(testFaceOpposite);
+CPPUNIT_TEST(testFaceAxis);
+CPPUNIT_TEST(testFaceNormal);
+CPPUNIT_TEST(testNormalFace);
+
+CPPUNIT_TEST(testFaceSet);
+
+CPPUNIT_TEST(testDefaultBlock);
+
+CPPUNIT_TEST_SUITE_END();
+
+public:
+       void setUp();
+       void tearDown();
+
+       void testFaceOpposite();
+       void testFaceAxis();
+       void testFaceNormal();
+       void testNormalFace();
+
+       void testFaceSet();
+
+       void testDefaultBlock();
+
+};
+
+}
+}
+
+#endif