]> git.localhorst.tv Git - blank.git/commitdiff
more chunk tests
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 12 Jun 2015 13:46:46 +0000 (15:46 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 12 Jun 2015 13:46:46 +0000 (15:46 +0200)
src/world/Block.hpp
src/world/block.cpp
tst/world/ChunkTest.cpp
tst/world/ChunkTest.hpp

index 9fd545b3813ce5c24e244e42bbb593b75606e690..0bb5fd8964b0655dae6addb1dd49903cf89d08ff 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef BLANK_WORLD_BLOCK_HPP_
 #define BLANK_WORLD_BLOCK_HPP_
 
+#include <iosfwd>
 #include <glm/glm.hpp>
 
 
@@ -130,6 +131,12 @@ private:
 
 };
 
+bool operator ==(const Block &, const Block &);
+
+std::ostream &operator <<(std::ostream &, const Block &);
+std::ostream &operator <<(std::ostream &, const Block::Face &);
+std::ostream &operator <<(std::ostream &, const Block::Turn &);
+
 }
 
 #endif
index 6cbaa264975d44ba316429525a706938efc0a5aa..aabb2b19a9fe47551282f615986a4007deb2726c 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "../model/geometry.hpp"
 
+#include <ostream>
 #include <glm/gtx/euler_angles.hpp>
 #include <glm/gtx/transform.hpp>
 
@@ -12,6 +13,66 @@ namespace blank {
 
 const NullShape BlockType::DEFAULT_SHAPE;
 
+
+bool operator ==(const Block &a, const Block &b) {
+       return a.type == b.type && a.orient == b.orient;
+}
+
+std::ostream &operator <<(std::ostream &out, const Block &block) {
+       return out << "Block(" << block.type << ", " << block.GetFace() << ", " << block.GetTurn() << ')';
+}
+
+std::ostream &operator <<(std::ostream &out, const Block::Face &face) {
+       switch (face) {
+               case Block::FACE_UP:
+                       out << "FACE_UP";
+                       break;
+               case Block::FACE_DOWN:
+                       out << "FACE_DOWN";
+                       break;
+               case Block::FACE_RIGHT:
+                       out << "FACE_RIGHT";
+                       break;
+               case Block::FACE_LEFT:
+                       out << "FACE_LEFT";
+                       break;
+               case Block::FACE_FRONT:
+                       out << "FACE_FRONT";
+                       break;
+               case Block::FACE_BACK:
+                       out << "FACE_BACK";
+                       break;
+               default:
+               case Block::FACE_COUNT:
+                       out << "invalid Block::Face";
+                       break;
+       }
+       return out;
+}
+
+std::ostream &operator <<(std::ostream &out, const Block::Turn &turn) {
+       switch (turn) {
+               case Block::TURN_NONE:
+                       out << "TURN_NONE";
+                       break;
+               case Block::TURN_LEFT:
+                       out << "TURN_LEFT";
+                       break;
+               case Block::TURN_AROUND:
+                       out << "TURN_AROUND";
+                       break;
+               case Block::TURN_RIGHT:
+                       out << "TURN_RIGHT";
+                       break;
+               default:
+               case Block::TURN_COUNT:
+                       out << "invalid Block::Turn";
+                       break;
+       }
+       return out;
+}
+
+
 BlockType::BlockType(bool v, const glm::vec3 &col, const Shape *s) noexcept
 : shape(s)
 , color(col)
index 79d349546a568ac602bfbb31a8eca541ae17f8d5..3ecf0f6912b7d42ae2f5853c5ad71e6effbad170 100644 (file)
@@ -336,5 +336,122 @@ void ChunkTest::testNeighbor() {
        }
 }
 
+
+void ChunkTest::testBlock() {
+       unique_ptr<Chunk> chunk(new Chunk(types));
+
+       for (int index = 0; index < Chunk::size; ++index) {
+               CPPUNIT_ASSERT_EQUAL_MESSAGE(
+                       "default chunk has non-default block",
+                       Block(), chunk->BlockAt(index)
+               );
+       }
+
+       Block block(1, Block::FACE_LEFT, Block::TURN_RIGHT);
+       chunk->SetBlock(0, block);
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "wrong type on set block",
+               block.type, chunk->BlockAt(0).type
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "wrong orientation on set block",
+               block.orient, chunk->BlockAt(0).orient
+       );
+       for (int index = 1; index < Chunk::size; ++index) {
+               CPPUNIT_ASSERT_EQUAL_MESSAGE(
+                       "changing block at index 0 affected some other block",
+                       Block(), chunk->BlockAt(index)
+               );
+       }
+}
+
+void ChunkTest::testLight() {
+       unique_ptr<Chunk> chunk(new Chunk(types));
+
+       for (int index = 0; index < Chunk::size; ++index) {
+               CPPUNIT_ASSERT_EQUAL_MESSAGE(
+                       "default chunk has non-zero light level",
+                       0, chunk->GetLight(index)
+               );
+       }
+
+       chunk->SetLight(0, 15);
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "wrong light level on set index",
+               15, chunk->GetLight(0)
+       );
+       for (int index = 1; index < Chunk::size; ++index) {
+               CPPUNIT_ASSERT_EQUAL_MESSAGE(
+                       "changing light at index 0 affected some other index",
+                       0, chunk->GetLight(index)
+               );
+       }
+}
+
+void ChunkTest::testLightPropagation() {
+       unique_ptr<Chunk> chunk(new Chunk(types));
+
+       // 0 air, 1 solid, 2 solid and emits light level of 5
+       chunk->SetBlock(Chunk::Pos(7, 7, 7), Block(2));
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "adding luminant block did not set correct light level",
+               5, chunk->GetLight(Chunk::Pos(7, 7, 7))
+       );
+
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in +X",
+               4, chunk->GetLight(Chunk::Pos(8, 7, 7))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in -X",
+               4, chunk->GetLight(Chunk::Pos(6, 7, 7))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in +Y",
+               4, chunk->GetLight(Chunk::Pos(7, 8, 7))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in -Y",
+               4, chunk->GetLight(Chunk::Pos(7, 6, 7))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in +Z",
+               4, chunk->GetLight(Chunk::Pos(7, 7, 8))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in -Z",
+               4, chunk->GetLight(Chunk::Pos(7, 7, 6))
+       );
+
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in 2D diagonal",
+               3, chunk->GetLight(Chunk::Pos(8, 8, 7))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in 2D diagonal",
+               3, chunk->GetLight(Chunk::Pos(7, 6, 8))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in 2D diagonal",
+               3, chunk->GetLight(Chunk::Pos(6, 7, 8))
+       );
+
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in 3D diagonal",
+               2, chunk->GetLight(Chunk::Pos(8, 6, 6))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in 3D diagonal",
+               2, chunk->GetLight(Chunk::Pos(6, 6, 8))
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "light did not propagate correctly in 3D diagonal",
+               2, chunk->GetLight(Chunk::Pos(6, 8, 8))
+       );
+
+       // now block the light to the left
+       chunk->SetBlock(Chunk::Pos(6, 7, 7), Block(1));
+}
+
 }
 }
index 00d05705deae3b2437827ab989ac58452307c5b5..fb52fa7bc7cbf888bbce05e7e6a6d33bbcc9d1fe 100644 (file)
@@ -18,6 +18,10 @@ CPPUNIT_TEST(testBounds);
 CPPUNIT_TEST(testBorder);
 CPPUNIT_TEST(testNeighbor);
 
+CPPUNIT_TEST(testBlock);
+CPPUNIT_TEST(testLight);
+CPPUNIT_TEST(testLightPropagation);
+
 CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -28,6 +32,10 @@ public:
        void testBorder();
        void testNeighbor();
 
+       void testBlock();
+       void testLight();
+       void testLightPropagation();
+
 private:
        BlockTypeRegistry types;