From d71d636af8b6f493abe537088464d1dc6b816c04 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 11 Jun 2015 14:34:41 +0200 Subject: [PATCH] fix faceset unset unit tests. oh yeah --- .gitignore | 1 + Makefile | 38 ++++++- src/world/Block.hpp | 2 +- tst/test.cpp | 17 ++++ tst/world/BlockTest.cpp | 213 ++++++++++++++++++++++++++++++++++++++++ tst/world/BlockTest.hpp | 44 +++++++++ 6 files changed, 309 insertions(+), 6 deletions(-) create mode 100644 tst/test.cpp create mode 100644 tst/world/BlockTest.cpp create mode 100644 tst/world/BlockTest.hpp diff --git a/.gitignore b/.gitignore index d599a31..4c408d0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ blank blank.debug blank.profile +blank.test build cachegrind.out.* callgrind.out.* diff --git a/Makefile b/Makefile index d063a9a..d0a3083 100644 --- 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 "$@" diff --git a/src/world/Block.hpp b/src/world/Block.hpp index 74af3e4..9fd545b 100644 --- a/src/world/Block.hpp +++ b/src/world/Block.hpp @@ -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 index 0000000..da6578e --- /dev/null +++ b/tst/test.cpp @@ -0,0 +1,17 @@ +#include +#include + +using CppUnit::TestFactoryRegistry; +using CppUnit::TextUi::TestRunner; + + +int main(int, char **) { + + TestRunner runner; + TestFactoryRegistry ®istry = 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 index 0000000..b10e9c9 --- /dev/null +++ b/tst/world/BlockTest.cpp @@ -0,0 +1,213 @@ +#include "BlockTest.hpp" + +#include "world/Block.hpp" + +#include + +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(0, 1, 0), Block::FaceNormal(Block::FACE_UP) + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "[ 0, -1, 0 ] not normal of DOWN", + glm::tvec3(0, -1, 0), Block::FaceNormal(Block::FACE_DOWN) + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "[ 1, 0, 0 ] not normal of RIGHT", + glm::tvec3(1, 0, 0), Block::FaceNormal(Block::FACE_RIGHT) + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "[ -1, 0, 0 ] not normal of LEFT", + glm::tvec3(-1, 0, 0), Block::FaceNormal(Block::FACE_LEFT) + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "[ 0, 0, 1 ] not normal of FRONT", + glm::tvec3(0, 0, 1), Block::FaceNormal(Block::FACE_FRONT) + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "[ 0, 0, -1 ] not normal of BACK", + glm::tvec3(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 index 0000000..467d260 --- /dev/null +++ b/tst/world/BlockTest.hpp @@ -0,0 +1,44 @@ +#ifndef BLANK_TEST_WORLD_BLOCKTEST_H_ +#define BLANK_TEST_WORLD_BLOCKTEST_H_ + +#include + + +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 -- 2.39.2