]> git.localhorst.tv Git - blank.git/commitdiff
some tests for Chunk
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 11 Jun 2015 15:46:07 +0000 (17:46 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 11 Jun 2015 15:46:07 +0000 (17:46 +0200)
src/world/Chunk.hpp
tst/model/GeometryTest.cpp
tst/test.cpp
tst/world/ChunkTest.cpp [new file with mode: 0644]
tst/world/ChunkTest.hpp [new file with mode: 0644]

index 8493ef6e2ef1b7f4f61ef5439484282a8e3ffbab..d986874445cc09829a87f6646df94585303d53f0 100644 (file)
@@ -71,6 +71,15 @@ public:
        }
        glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
 
+       static bool IsBorder(const Pos &pos) noexcept {
+               return
+                       pos.x == 0 ||
+                       pos.x == width - 1 ||
+                       pos.y == 0 ||
+                       pos.y == height - 1 ||
+                       pos.z == 0 ||
+                       pos.z == depth - 1;
+       }
        static constexpr bool IsBorder(int idx) noexcept {
                return
                        idx < width * height ||                    // low Z plane
index 3776da14fd0172eac29a59a7bd8e9a7d6d8d8095..51985aef7a70e38df8df09051d44911adf02cd72 100644 (file)
@@ -3,6 +3,7 @@
 #include "model/geometry.hpp"
 
 #include <limits>
+#include <glm/gtx/io.hpp>
 
 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::GeometryTest);
 
@@ -29,12 +30,36 @@ void GeometryTest::testRayAABBIntersection() {
 
        CPPUNIT_ASSERT_MESSAGE(
                "ray at origin not intersecting box at origin",
-               Intersection(ray, box, M, &distance, &normal)
+               Intersection(ray, box, M, &distance)
        );
        CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
                "intersection distance way off",
                0.0f, distance, delta
        );
+       // normal undefined, so can't test
+
+       // move ray outside the box, but have it still point at it
+       // should be 4 units to the left now
+       ray.orig.x = -5;
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray pointing at box doesn't intersect",
+               Intersection(ray, box, M, &distance, &normal)
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "intersection distance way off",
+               4.0f, distance, delta
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "wrong surface normal at intersection point",
+               glm::vec3(-1, 0, 0), normal
+       );
+
+       // move ray to the other side, so it's pointing away now
+       ray.orig.x = 5;
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray pointing away from box still intersects",
+               !Intersection(ray, box, M)
+       );
 }
 
 }
index da6578e4708f6df6c8dd9f01f5a83a49e0907dfc..180b0edca640286a520c767d37a74f2a4547eea0 100644 (file)
@@ -1,3 +1,5 @@
+#include "app/init.hpp"
+
 #include <cppunit/extensions/TestFactoryRegistry.h>
 #include <cppunit/ui/text/TestRunner.h>
 
@@ -6,6 +8,11 @@ using CppUnit::TextUi::TestRunner;
 
 
 int main(int, char **) {
+       blank::InitSDL sdl;
+       blank::InitGL gl;
+       blank::Window win;
+       blank::GLContext ctx(win.CreateContext());
+       blank::InitGLEW glew;
 
        TestRunner runner;
        TestFactoryRegistry &registry = TestFactoryRegistry::getRegistry();
diff --git a/tst/world/ChunkTest.cpp b/tst/world/ChunkTest.cpp
new file mode 100644 (file)
index 0000000..79d3495
--- /dev/null
@@ -0,0 +1,340 @@
+#include "ChunkTest.hpp"
+
+#include "world/BlockType.hpp"
+#include "world/Chunk.hpp"
+
+#include <memory>
+
+CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::ChunkTest);
+
+using std::unique_ptr;
+
+
+namespace blank {
+namespace test {
+
+void ChunkTest::setUp() {
+       types = BlockTypeRegistry();
+
+       BlockType obstacle;
+       obstacle.visible = true;
+       obstacle.block_light = true;
+       types.Add(obstacle);
+
+       BlockType source;
+       source.visible = true;
+       source.luminosity = 5;
+       source.block_light = true;
+       types.Add(source);
+}
+
+void ChunkTest::tearDown() {
+}
+
+
+void ChunkTest::testBounds() {
+       CPPUNIT_ASSERT_MESSAGE(
+               "valid position out of bounds",
+               Chunk::InBounds(Chunk::Pos(0, 0, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "valid position out of bounds",
+               Chunk::InBounds(Chunk::Pos(15, 0, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "valid position out of bounds",
+               Chunk::InBounds(Chunk::Pos(0, 15, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "valid position out of bounds",
+               Chunk::InBounds(Chunk::Pos(0, 0, 15))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "valid position out of bounds",
+               Chunk::InBounds(Chunk::Pos(15, 15, 15))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "invalid position in bounds",
+               !Chunk::InBounds(Chunk::Pos(-1, -1, -1))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "invalid position in bounds",
+               !Chunk::InBounds(Chunk::Pos(-1, 1, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "invalid position in bounds",
+               !Chunk::InBounds(Chunk::Pos(16, -16, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "invalid position in bounds",
+               !Chunk::InBounds(Chunk::Pos(16, 16, 16))
+       );
+}
+
+void ChunkTest::testBorder() {
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(0, 0, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(0, 0, 8))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(0, 0, 15))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(0, 8, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(0, 8, 8))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(0, 8, 15))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(0, 15, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(0, 15, 8))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(0, 15, 15))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(8, 0, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(8, 0, 8))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(8, 0, 15))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(8, 8, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position is border",
+               !Chunk::IsBorder(Chunk::Pos(8, 8, 8))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(8, 8, 15))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(8, 15, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(8, 15, 8))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(8, 15, 15))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(15, 0, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(15, 0, 8))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(15, 0, 15))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(15, 8, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(15, 8, 8))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(15, 8, 15))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(15, 15, 0))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(15, 15, 8))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::Pos(15, 15, 15))
+       );
+
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(0, 0, 0)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(0, 0, 8)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(0, 0, 15)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(0, 8, 0)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(0, 8, 8)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(0, 8, 15)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(0, 15, 0)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(0, 15, 8)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(0, 15, 15)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(8, 0, 0)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(8, 0, 8)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(8, 0, 15)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(8, 8, 0)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position is border",
+               !Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(8, 8, 8)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(8, 8, 15)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(8, 15, 0)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(8, 15, 8)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(8, 15, 15)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(15, 0, 0)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(15, 0, 8)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(15, 0, 15)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(15, 8, 0)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(15, 8, 8)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(15, 8, 15)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(15, 15, 0)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(15, 15, 8)))
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "position not border",
+               Chunk::IsBorder(Chunk::ToIndex(Chunk::Pos(15, 15, 15)))
+       );
+}
+
+void ChunkTest::testNeighbor() {
+       unique_ptr<Chunk> chunk(new Chunk(types));
+       chunk->Position({0, 0, 0});
+       for (int i = 0; i < Block::FACE_COUNT; ++i) {
+               CPPUNIT_ASSERT_MESSAGE(
+                       "sole chunk has neighbor",
+                       !chunk->HasNeighbor(Block::Face(i))
+               );
+       }
+
+       unique_ptr<Chunk> neighbor(new Chunk(types));
+       for (int i = 0; i < Block::FACE_COUNT; ++i) {
+               Block::Face face = Block::Face(i);
+               neighbor->Position(Block::FaceNormal(face));
+               chunk->SetNeighbor(*neighbor);
+               CPPUNIT_ASSERT_MESSAGE(
+                       "chunk did not link right neighbor",
+                       chunk->HasNeighbor(face)
+               );
+               CPPUNIT_ASSERT_MESSAGE(
+                       "chunk did not link right neighbor",
+                       neighbor->HasNeighbor(Block::Opposite(face))
+               );
+               CPPUNIT_ASSERT_EQUAL_MESSAGE(
+                       "chunk did not link correct neighbor",
+                       &*neighbor, &chunk->GetNeighbor(face)
+               );
+               CPPUNIT_ASSERT_EQUAL_MESSAGE(
+                       "chunk did not link correct neighbor",
+                       &*chunk, &neighbor->GetNeighbor(Block::Opposite(face))
+               );
+               chunk->Unlink();
+               chunk->ClearNeighbors();
+       }
+
+       neighbor->Position({1, 1, 1});
+       chunk->SetNeighbor(*neighbor);
+       for (int i = 0; i < Block::FACE_COUNT; ++i) {
+               CPPUNIT_ASSERT_MESSAGE(
+                       "chunk linked with non-neighbor",
+                       !chunk->HasNeighbor(Block::Face(i))
+               );
+       }
+}
+
+}
+}
diff --git a/tst/world/ChunkTest.hpp b/tst/world/ChunkTest.hpp
new file mode 100644 (file)
index 0000000..00d0570
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef BLANK_TEST_WORLD_CHUNKTEST_H_
+#define BLANK_TEST_WORLD_CHUNKTEST_H_
+
+#include "world/BlockTypeRegistry.hpp"
+
+#include <cppunit/extensions/HelperMacros.h>
+
+
+namespace blank {
+namespace test {
+
+class ChunkTest
+: public CppUnit::TestFixture {
+
+CPPUNIT_TEST_SUITE(ChunkTest);
+
+CPPUNIT_TEST(testBounds);
+CPPUNIT_TEST(testBorder);
+CPPUNIT_TEST(testNeighbor);
+
+CPPUNIT_TEST_SUITE_END();
+
+public:
+       void setUp();
+       void tearDown();
+
+       void testBounds();
+       void testBorder();
+       void testNeighbor();
+
+private:
+       BlockTypeRegistry types;
+
+};
+
+}
+}
+
+#endif