diff options
Diffstat (limited to 'tests/ChunkData')
-rw-r--r-- | tests/ChunkData/ArraytoCoord.cpp | 89 | ||||
-rw-r--r-- | tests/ChunkData/CMakeLists.txt | 29 | ||||
-rw-r--r-- | tests/ChunkData/Coordinates.cpp | 143 | ||||
-rw-r--r-- | tests/ChunkData/Copies.cpp | 134 | ||||
-rw-r--r-- | tests/ChunkData/CopyBlocks.cpp | 76 | ||||
-rw-r--r-- | tests/ChunkData/creatable.cpp | 9 |
6 files changed, 480 insertions, 0 deletions
diff --git a/tests/ChunkData/ArraytoCoord.cpp b/tests/ChunkData/ArraytoCoord.cpp new file mode 100644 index 000000000..0daf38f7b --- /dev/null +++ b/tests/ChunkData/ArraytoCoord.cpp @@ -0,0 +1,89 @@ + +#include "Globals.h" +#include "ChunkData.h" + + + +int main(int argc, char** argv) +{ + { + // Test first segment + cChunkData buffer; + + BLOCKTYPE SrcBlockBuffer[16 * 16 * 256]; + memset(SrcBlockBuffer, 0x00, sizeof(SrcBlockBuffer)); + SrcBlockBuffer[7 + (4 * 16) + (5 * 16 * 16)] = 0xcd; + buffer.SetBlockTypes(SrcBlockBuffer); + testassert(buffer.GetBlock(7, 5, 4) == 0xcd); + + NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; + memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); + SrcNibbleBuffer[(6 + (1 * 16) + (2 * 16 * 16)) / 2] = 0xe; + buffer.SetMetas(SrcNibbleBuffer); + testassert(buffer.GetMeta(6, 2, 1) == 0xe); + + memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); + SrcNibbleBuffer[(6 + (1 * 16) + (2 * 16 * 16)) / 2] = 0xe; + buffer.SetBlockLight(SrcNibbleBuffer); + testassert(buffer.GetBlockLight(6, 2, 1) == 0xe); + + memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); + SrcNibbleBuffer[(6 + (1 * 16) + (2 * 16 * 16)) / 2] = 0xe; + buffer.SetSkyLight(SrcNibbleBuffer); + testassert(buffer.GetSkyLight(6, 2, 1) == 0xe); + } + + { + // test following segment + cChunkData buffer; + + BLOCKTYPE SrcBlockBuffer[16 * 16 * 256]; + memset(SrcBlockBuffer, 0x00, sizeof(SrcBlockBuffer)); + SrcBlockBuffer[7 + (4 * 16) + (24 * 16 * 16)] = 0xcd; + buffer.SetBlockTypes(SrcBlockBuffer); + testassert(buffer.GetBlock(7, 24, 4) == 0xcd); + + NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; + memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); + SrcNibbleBuffer[(6 + (1 * 16) + (24 * 16 * 16)) / 2] = 0xe; + buffer.SetMetas(SrcNibbleBuffer); + testassert(buffer.GetMeta(6, 24, 1) == 0xe); + + memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); + SrcNibbleBuffer[(6 + 1 * 16 + 24 * 16 * 16) / 2] = 0xe; + buffer.SetBlockLight(SrcNibbleBuffer); + testassert(buffer.GetBlockLight(6, 24, 1) == 0xe); + + memset(SrcNibbleBuffer, 0xff, sizeof(SrcNibbleBuffer)); + SrcNibbleBuffer[(6 + (1 * 16) + (24 * 16 * 16)) / 2] = 0xe; + buffer.SetSkyLight(SrcNibbleBuffer); + testassert(buffer.GetSkyLight(6, 24, 1) == 0xe); + } + + { + // test zeros + cChunkData buffer; + + BLOCKTYPE SrcBlockBuffer[16 * 16 * 256]; + memset(SrcBlockBuffer, 0x00, sizeof(SrcBlockBuffer)); + buffer.SetBlockTypes(SrcBlockBuffer); + testassert(buffer.GetBlock(7, 24, 4) == 0x00); + + NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; + memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); + buffer.SetMetas(SrcNibbleBuffer); + testassert(buffer.GetMeta(6, 24, 1) == 0x0); + + memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); + buffer.SetBlockLight(SrcNibbleBuffer); + testassert(buffer.GetBlockLight(6, 24, 1) == 0x0); + + memset(SrcNibbleBuffer, 0xff, sizeof(SrcNibbleBuffer)); + buffer.SetSkyLight(SrcNibbleBuffer); + testassert(buffer.GetSkyLight(6, 24, 1) == 0xf); + } + + // All tests passed: + return 0; +} + diff --git a/tests/ChunkData/CMakeLists.txt b/tests/ChunkData/CMakeLists.txt new file mode 100644 index 000000000..381e11cc2 --- /dev/null +++ b/tests/ChunkData/CMakeLists.txt @@ -0,0 +1,29 @@ +cmake_minimum_required (VERSION 2.6) + +enable_testing() + +include_directories(${CMAKE_SOURCE_DIR}/src/) + +add_definitions(-DTEST_GLOBALS=1) +add_library(ChunkBuffer ${CMAKE_SOURCE_DIR}/src/ChunkData.cpp ${CMAKE_SOURCE_DIR}/src/StringUtils.cpp) + + +add_executable(creatable-exe creatable.cpp) +target_link_libraries(creatable-exe ChunkBuffer) +add_test(NAME creatable-test COMMAND creatable-exe) + +add_executable(coordinates-exe Coordinates.cpp) +target_link_libraries(coordinates-exe ChunkBuffer) +add_test(NAME coordinates-test COMMAND coordinates-exe) + +add_executable(copies-exe Copies.cpp) +target_link_libraries(copies-exe ChunkBuffer) +add_test(NAME copies-test COMMAND copies-exe) + +add_executable(arraystocoords-exe ArraytoCoord.cpp) +target_link_libraries(arraystocoords-exe ChunkBuffer) +add_test(NAME arraystocoords-test COMMAND arraystocoords-exe) + +add_executable(copyblocks-exe CopyBlocks.cpp) +target_link_libraries(copyblocks-exe ChunkBuffer) +add_test(NAME copyblocks-test COMMAND copyblocks-exe) diff --git a/tests/ChunkData/Coordinates.cpp b/tests/ChunkData/Coordinates.cpp new file mode 100644 index 000000000..48d731c7e --- /dev/null +++ b/tests/ChunkData/Coordinates.cpp @@ -0,0 +1,143 @@ + +#include "Globals.h" +#include "ChunkData.h" + + + +int main(int argc, char** argv) +{ + { + cChunkData buffer; + + // Empty chunks + buffer.SetBlock(0, 0, 0, 0xAB); + testassert(buffer.GetBlock(0, 0, 0) == 0xAB); + buffer.SetMeta(0, 16, 0, 0xC); + testassert(buffer.GetMeta(0, 16, 0) == 0xC); + + // loaded but not written segments + testassert(buffer.GetBlock(1, 0, 0) == 0x0); + testassert(buffer.GetMeta(1, 16, 0) == 0x0); + + // Notloaded segments + testassert(buffer.GetBlock(0, 32, 0) == 0x0); + testassert(buffer.GetMeta(0, 48, 0) == 0x0); + + // Out of Range + CheckAsserts( + buffer.SetBlock(-1, 0, 0, 0); + ); + CheckAsserts( + buffer.SetBlock(0, -1, 0, 0); + ); + CheckAsserts( + buffer.SetBlock(0, 0, -1, 0); + ); + CheckAsserts( + buffer.SetBlock(256, 0, 0, 0); + ); + CheckAsserts( + buffer.SetBlock(0, 256, 0, 0); + ); + CheckAsserts( + buffer.SetBlock(0, 0, 256, 0); + ); + + // Out of Range + CheckAsserts( + buffer.GetBlock(-1, 0, 0); + ); + CheckAsserts( + buffer.GetBlock(0, -1, 0); + ); + CheckAsserts( + buffer.GetBlock(0, 0, -1); + ); + CheckAsserts( + buffer.GetBlock(256, 0, 0); + ); + CheckAsserts( + buffer.GetBlock(0, 256, 0); + ); + CheckAsserts( + buffer.GetBlock(0, 0, 256); + ); + + // Out of Range + CheckAsserts( + buffer.SetMeta(-1, 0, 0, 0); + ); + CheckAsserts( + buffer.SetMeta(0, -1, 0, 0); + ); + CheckAsserts( + buffer.SetMeta(0, 0, -1, 0); + ); + CheckAsserts( + buffer.SetMeta(256, 0, 0, 0); + ); + CheckAsserts( + buffer.SetMeta(0, 256, 0, 0); + ); + CheckAsserts( + buffer.SetMeta(0, 0, 256, 0); + ); + + // Out of Range + CheckAsserts( + buffer.GetMeta(-1, 0, 0); + ); + CheckAsserts( + buffer.GetMeta(0, -1, 0); + ); + CheckAsserts( + buffer.GetMeta(0, 0, -1); + ); + CheckAsserts( + buffer.GetMeta(256, 0, 0); + ); + CheckAsserts( + buffer.GetMeta(0, 256, 0); + ); + CheckAsserts( + buffer.GetMeta(0, 0, 256); + ); + } + + { + cChunkData buffer; + + // Zero's + buffer.SetBlock(0, 0, 0, 0x0); + buffer.SetBlock(0, 0, 1, 0xab); + testassert(buffer.GetBlock(0, 0, 0) == 0x0); + testassert(buffer.GetBlock(0, 0, 1) == 0xab); + + buffer.SetMeta(0, 16, 0, 0x0); + buffer.SetMeta(0, 16, 1, 0xc); + testassert(buffer.GetMeta(0, 16, 0) == 0x0); + testassert(buffer.GetMeta(0, 16, 1) == 0xc); + } + + + { + // Operator = + cChunkData buffer; + buffer.SetBlock(0, 0, 0, 0x42); + cChunkData copy; + #if __cplusplus < 201103L + copy = buffer; + #else + copy = std::move(buffer); + #endif + testassert(copy.GetBlock(0, 0, 0) == 0x42); + #if __cplusplus < 201103L + copy = copy; + #else + copy = std::move(copy); + #endif + testassert(copy.GetBlock(0, 0, 0) == 0x42); + } + + return 0; +} diff --git a/tests/ChunkData/Copies.cpp b/tests/ChunkData/Copies.cpp new file mode 100644 index 000000000..312441eee --- /dev/null +++ b/tests/ChunkData/Copies.cpp @@ -0,0 +1,134 @@ + +#include "Globals.h" +#include "ChunkData.h" + + + +int main(int argc, char** argv) +{ + { + cChunkData buffer; + + buffer.SetBlock(3, 1, 4, 0xDE); + buffer.SetMeta(3, 1, 4, 0xA); + + cChunkData copy = buffer.Copy(); + testassert(copy.GetBlock(3, 1, 4) == 0xDE); + testassert(copy.GetMeta(3, 1, 4) == 0xA); + + BLOCKTYPE SrcBlockBuffer[16 * 16 * 256]; + for (int i = 0; i < 16 * 16 * 256; i += 4) + { + SrcBlockBuffer[i + 0] = 0xde; + SrcBlockBuffer[i + 1] = 0xad; + SrcBlockBuffer[i + 2] = 0xbe; + SrcBlockBuffer[i + 3] = 0xef; + } + + buffer.SetBlockTypes(SrcBlockBuffer); + BLOCKTYPE DstBlockBuffer[16 * 16 * 256]; + buffer.CopyBlockTypes(DstBlockBuffer); + testassert(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1) == 0); + + memset(SrcBlockBuffer, 0x00, 16 * 16 * 256); + buffer.SetBlockTypes(SrcBlockBuffer); + buffer.CopyBlockTypes(DstBlockBuffer); + testassert(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1) == 0); + } + + { + cChunkData buffer; + + NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; + for (int i = 0; i < 16 * 16 * 256 / 2; i += 4) + { + SrcNibbleBuffer[i + 0] = 0xde; + SrcNibbleBuffer[i + 1] = 0xad; + SrcNibbleBuffer[i + 2] = 0xbe; + SrcNibbleBuffer[i + 3] = 0xef; + } + + buffer.SetMetas(SrcNibbleBuffer); + NIBBLETYPE DstNibbleBuffer[16 * 16 * 256/ 2]; + buffer.CopyMetas(DstNibbleBuffer); + testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + + memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 /2); + buffer.SetMetas(SrcNibbleBuffer); + buffer.CopyMetas(DstNibbleBuffer); + testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + } + + { + cChunkData buffer; + + NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; + for (int i = 0; i < 16 * 16 * 256 / 2; i += 4) + { + SrcNibbleBuffer[i + 0] = 0xde; + SrcNibbleBuffer[i + 1] = 0xad; + SrcNibbleBuffer[i + 2] = 0xbe; + SrcNibbleBuffer[i + 3] = 0xef; + } + + buffer.SetBlockLight(SrcNibbleBuffer); + NIBBLETYPE DstNibbleBuffer[16 * 16 * 256 / 2]; + buffer.CopyBlockLight(DstNibbleBuffer); + testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 /2) - 1) == 0); + + memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 /2); + buffer.SetBlockLight(SrcNibbleBuffer); + buffer.CopyBlockLight(DstNibbleBuffer); + testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 /2) - 1) == 0); + } + + { + cChunkData buffer; + + NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; + for (int i = 0; i < 16 * 16 * 256 / 2; i += 4) + { + SrcNibbleBuffer[i + 0] = 0xde; + SrcNibbleBuffer[i + 1] = 0xad; + SrcNibbleBuffer[i + 2] = 0xbe; + SrcNibbleBuffer[i + 3] = 0xef; + } + + buffer.SetSkyLight(SrcNibbleBuffer); + NIBBLETYPE DstNibbleBuffer[16 * 16 * 256/ 2]; + buffer.CopySkyLight(DstNibbleBuffer); + testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + + memset(SrcNibbleBuffer, 0xFF, 16 * 16 * 256 / 2); + buffer.SetSkyLight(SrcNibbleBuffer); + buffer.CopySkyLight(DstNibbleBuffer); + testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + } + + { + cChunkData buffer; + + BLOCKTYPE SrcBlockBuffer[16 * 16 * 256]; + memset(SrcBlockBuffer, 0x00, 16 * 16 * 256); + BLOCKTYPE DstBlockBuffer[16 * 16 * 256]; + buffer.CopyBlockTypes(DstBlockBuffer); + testassert(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1) == 0); + + NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; + memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 / 2); + NIBBLETYPE DstNibbleBuffer[16 * 16 * 256 / 2]; + buffer.CopyMetas(DstNibbleBuffer); + testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + + memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 / 2); + buffer.CopyBlockLight(DstNibbleBuffer); + testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + + memset(SrcNibbleBuffer, 0xFF, 16 * 16 * 256 / 2); + buffer.CopySkyLight(DstNibbleBuffer); + testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + } + + // All tests successful: + return 0; +} diff --git a/tests/ChunkData/CopyBlocks.cpp b/tests/ChunkData/CopyBlocks.cpp new file mode 100644 index 000000000..be8cab234 --- /dev/null +++ b/tests/ChunkData/CopyBlocks.cpp @@ -0,0 +1,76 @@ + +// CopyBlocks.cpp + +// Implements the test for cChunkData::CopyBlockTypes() range copying + + + + + +#include "Globals.h" +#include "ChunkData.h" + + + + + +int main(int argc, char ** argv) +{ + // Set up a cChunkData with known contents - all blocks 0x01, all metas 0x02: + cChunkData Data; + cChunkDef::BlockTypes BlockTypes; + cChunkDef::BlockNibbles BlockMetas; + memset(BlockTypes, 0x01, sizeof(BlockTypes)); + memset(BlockMetas, 0x02, sizeof(BlockMetas)); + Data.SetBlockTypes(BlockTypes); + Data.SetMetas(BlockMetas); + + // Try to read varying amounts of blocktypes from the cChunkData. + // Verify that the exact amount of memory is copied, by copying to a larger buffer and checking its boundaries + BLOCKTYPE TestBuffer[5 * cChunkDef::NumBlocks]; + size_t WritePosIdx = 2 * cChunkDef::NumBlocks; + BLOCKTYPE * WritePosition = &TestBuffer[WritePosIdx]; + memset(TestBuffer, 0x03, sizeof(TestBuffer)); + size_t LastReportedStep = 1; + for (size_t idx = 0; idx < 5000; idx += 7) + { + if (idx / 500 != LastReportedStep) + { + printf("Testing index %u...\n", (unsigned)idx); + LastReportedStep = idx / 500; + } + + for (size_t len = 3; len < 1000; len += 13) + { + Data.CopyBlockTypes(WritePosition, idx, len); + + // Verify the data copied: + for (size_t i = 0; i < len; i++) + { + assert_test(WritePosition[i] == 0x01); + } + // Verify the space before the copied data hasn't been changed: + for (size_t i = 0; i < WritePosIdx; i++) + { + assert_test(TestBuffer[i] == 0x03); + } + // Verify the space after the copied data hasn't been changed: + for (size_t i = WritePosIdx + idx + len; i < ARRAYCOUNT(TestBuffer); i++) + { + assert_test(TestBuffer[i] == 0x03); + } + + // Re-initialize the buffer for the next test: + for (size_t i = 0; i < len; i++) + { + WritePosition[i] = 0x03; + } + } // for len + } // for idx + return 0; +} + + + + + diff --git a/tests/ChunkData/creatable.cpp b/tests/ChunkData/creatable.cpp new file mode 100644 index 000000000..1321bf49b --- /dev/null +++ b/tests/ChunkData/creatable.cpp @@ -0,0 +1,9 @@ + +#include "Globals.h" +#include "ChunkData.h" + +int main(int argc, char** argv) +{ + cChunkData buffer; + return 0; +} |