From c592d2d6e230851bd7ed74d98f9046469f4086fd Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 11 Jun 2015 17:46:07 +0200 Subject: [PATCH] some tests for Chunk --- src/world/Chunk.hpp | 9 + tst/model/GeometryTest.cpp | 27 ++- tst/test.cpp | 7 + tst/world/ChunkTest.cpp | 340 +++++++++++++++++++++++++++++++++++++ tst/world/ChunkTest.hpp | 39 +++++ 5 files changed, 421 insertions(+), 1 deletion(-) create mode 100644 tst/world/ChunkTest.cpp create mode 100644 tst/world/ChunkTest.hpp diff --git a/src/world/Chunk.hpp b/src/world/Chunk.hpp index 8493ef6..d986874 100644 --- a/src/world/Chunk.hpp +++ b/src/world/Chunk.hpp @@ -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 diff --git a/tst/model/GeometryTest.cpp b/tst/model/GeometryTest.cpp index 3776da1..51985ae 100644 --- a/tst/model/GeometryTest.cpp +++ b/tst/model/GeometryTest.cpp @@ -3,6 +3,7 @@ #include "model/geometry.hpp" #include +#include 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) + ); } } diff --git a/tst/test.cpp b/tst/test.cpp index da6578e..180b0ed 100644 --- a/tst/test.cpp +++ b/tst/test.cpp @@ -1,3 +1,5 @@ +#include "app/init.hpp" + #include #include @@ -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 ®istry = TestFactoryRegistry::getRegistry(); diff --git a/tst/world/ChunkTest.cpp b/tst/world/ChunkTest.cpp new file mode 100644 index 0000000..79d3495 --- /dev/null +++ b/tst/world/ChunkTest.cpp @@ -0,0 +1,340 @@ +#include "ChunkTest.hpp" + +#include "world/BlockType.hpp" +#include "world/Chunk.hpp" + +#include + +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(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 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 index 0000000..00d0570 --- /dev/null +++ b/tst/world/ChunkTest.hpp @@ -0,0 +1,39 @@ +#ifndef BLANK_TEST_WORLD_CHUNKTEST_H_ +#define BLANK_TEST_WORLD_CHUNKTEST_H_ + +#include "world/BlockTypeRegistry.hpp" + +#include + + +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 -- 2.39.2