diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/BlockTypeRegistry/BlockStateTest.cpp | 58 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/ProtocolBlockTypePalette/CMakeLists.txt | 47 | ||||
-rw-r--r-- | tests/ProtocolBlockTypePalette/ProtocolBlockTypePaletteTest.cpp | 168 | ||||
-rw-r--r-- | tests/ProtocolBlockTypePalette/test.btp.json | 146 |
5 files changed, 420 insertions, 0 deletions
diff --git a/tests/BlockTypeRegistry/BlockStateTest.cpp b/tests/BlockTypeRegistry/BlockStateTest.cpp index 57317cbdf..a9af34a9a 100644 --- a/tests/BlockTypeRegistry/BlockStateTest.cpp +++ b/tests/BlockTypeRegistry/BlockStateTest.cpp @@ -109,8 +109,66 @@ static void testReplacing() +/** Tests the comparison operator. */ +static void testComparison() +{ + LOGD("Testing comparison of BlockStates..."); + + // Simple property value tests + TEST_FALSE((BlockState({{"a", "a"}}) < BlockState({{"a", "a"}}))); + TEST_FALSE((BlockState() < BlockState())); + TEST_TRUE((BlockState() < BlockState({{"foo", "bar"}}))); + TEST_FALSE((BlockState({{"foo", "bar"}}) < BlockState())); +} + + + + + +/** Tests the comparison operator using crafted data to defeat the checksum. */ +static void testComparison2() +{ + /* The following test ensures that items inserted in different order result + in the same map. I.e. that the < operator is stable. */ + std::vector<BlockState> v; + std::map<BlockState, bool> map1; + std::map<BlockState, bool> map2; + + for (int i = 0; i < 128; ++i) + { + v.push_back(BlockState({{std::string(1, static_cast<char>(0x1F)), std::string(1, static_cast<char>(i))}})); + v.push_back(BlockState({{std::string(1, static_cast<char>(0x10)), std::string(1, static_cast<char>(i | 0x80))}, + {std::string(1, static_cast<char>(0x0F)), std::string(1, static_cast<char>(0x80))}})); + } + + for (size_t i = 0; i < v.size(); ++i) + { + map1[v[i]] = true; + } + + for (auto i = v.size(); i > 0; --i) + { + map2[v[i - 1]] = true; + } + + // Check result + TEST_EQUAL(v.size(), 2 * 128); + TEST_EQUAL(map1.size(), v.size()); + TEST_EQUAL(map1.size(), map2.size()); + for (const auto & item: map1) + { + TEST_EQUAL(map1[item.first], map2[item.first]); + } +} + + + + + IMPLEMENT_TEST_MAIN("BlockStateTest", testStaticCreation(); testDynamicCreation(); testReplacing(); + testComparison(); + testComparison2(); ) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 74e4323ec..2ea55af25 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,3 +19,4 @@ add_subdirectory(Network) add_subdirectory(OSSupport) add_subdirectory(SchematicFileSerializer) add_subdirectory(UUID) +add_subdirectory(ProtocolBlockTypePalette) diff --git a/tests/ProtocolBlockTypePalette/CMakeLists.txt b/tests/ProtocolBlockTypePalette/CMakeLists.txt new file mode 100644 index 000000000..43c2df676 --- /dev/null +++ b/tests/ProtocolBlockTypePalette/CMakeLists.txt @@ -0,0 +1,47 @@ +cmake_minimum_required(VERSION 3.0.2) +enable_testing() +add_definitions(-DTEST_GLOBALS=1) + +include_directories(SYSTEM "../../lib/jsoncpp/include") +include_directories(${CMAKE_SOURCE_DIR}/src/) + + +add_definitions(-DTEST_GLOBALS=1) + +set (SHARED_SRCS + ${CMAKE_SOURCE_DIR}/src/Protocol/ProtocolBlockTypePalette.cpp + ${CMAKE_SOURCE_DIR}/src/BlockState.cpp + ${CMAKE_SOURCE_DIR}/src/StringUtils.cpp +) + +set (SHARED_HDRS + ../TestHelpers.h + ${CMAKE_SOURCE_DIR}/src/Protocol/ProtocolBlockTypePalette.h + ${CMAKE_SOURCE_DIR}/src/BlockState.h + ${CMAKE_SOURCE_DIR}/src/StringUtils.h +) + +set (SRCS + ProtocolBlockTypePaletteTest.cpp +) + +file (COPY + test.btp.json + ../../Server/Protocol/1.13/base.btp.json + DESTINATION ./) + +source_group("Shared" FILES ${SHARED_SRCS} ${SHARED_HDRS}) +source_group("Sources" FILES ${SRCS}) +add_executable(ProtocolBlockTypePaletteTest-exe ${SRCS} ${SHARED_SRCS} ${SHARED_HDRS}) +target_link_libraries(ProtocolBlockTypePaletteTest-exe fmt::fmt jsoncpp_lib_static) +add_test(NAME ProtocolBlockTypePaletteTest-test COMMAND ProtocolBlockTypePaletteTest-exe) + + + + + +# Put the projects into solution folders (MSVC): +set_target_properties( + ProtocolBlockTypePaletteTest-exe + PROPERTIES FOLDER Tests +) diff --git a/tests/ProtocolBlockTypePalette/ProtocolBlockTypePaletteTest.cpp b/tests/ProtocolBlockTypePalette/ProtocolBlockTypePaletteTest.cpp new file mode 100644 index 000000000..948fd2219 --- /dev/null +++ b/tests/ProtocolBlockTypePalette/ProtocolBlockTypePaletteTest.cpp @@ -0,0 +1,168 @@ +// ProtocolBlockTypePaletteTest.cpp + +#include <string> +#include <fstream> +#include <streambuf> +#include "Globals.h" + +#include "Protocol/ProtocolBlockTypePalette.h" + +#include "../TestHelpers.h" + + + + + +static void TestSuccess(void) +{ + LOG("Test TestSuccess"); + ProtocolBlockTypePalette palette; + + auto example = "{\"Metadata\":{\"ProtocolBlockTypePaletteVersion\":1}, \"Palette\":[{\ + \"props\": {\ + \"foo\": \"bar\"\ + }, \ + \"name\": \"b\", \ + \"id\": \"0\"\ + }]}"; + + palette.clear(); + TEST_TRUE(palette.loadFromString(example)); + TEST_EQUAL(palette.index("b", BlockState({{"foo", "bar"}})), 0); + TEST_EQUAL(palette.index("b", BlockState({{"foo", "baz"}})), ProtocolBlockTypePalette::NOT_FOUND); + TEST_EQUAL(palette.index("a", BlockState({{"foo", "bar"}})), ProtocolBlockTypePalette::NOT_FOUND); +} + + + + + +static void TestErrors(void) +{ + LOG("Test TestErrors"); + ProtocolBlockTypePalette palette; + TEST_FALSE(palette.loadFromString("")); + + palette.clear(); + TEST_FALSE(palette.loadFromString("[]")); + + palette.clear(); + TEST_FALSE(palette.loadFromString("a = {}")); + + palette.clear(); + TEST_FALSE(palette.loadFromString("{x = 1}")); // Lua style + + palette.clear(); + TEST_FALSE(palette.loadFromString("$#^%&")); +} + + + + + +static void TestComplex1(void) +{ + LOG("Test TestComplex1"); + ProtocolBlockTypePalette palette; + auto str = "{\"Metadata\":{\"ProtocolBlockTypePaletteVersion\":1}, \"Palette\":[{\ + \"props\": {\ + \"foo\": \"bar\", \ + \"moo\": \"baz\"\ + }, \ + \"id\": \"0\", \ + \"name\": \"b\"\ + }, {\ + \"props\": {\ + \"foo\": \"baz\", \ + \"moo\": \"bar\"\ + }, \ + \"id\": \"1\", \ + \"name\": \"b\"\ + }, {\ + \"props\": {\ + \"foo\": \"baz\", \ + \"moo\": \"bar\"\ + }, \ + \"id\": \"1001\", \ + \"name\": \"b\"\ + }]}"; + TEST_TRUE(palette.loadFromString(str)); // This should print info message about duplicate ID + TEST_EQUAL(palette.index("b", BlockState({{"foo","bar"}})), ProtocolBlockTypePalette::NOT_FOUND); + TEST_EQUAL(palette.index("b", BlockState({{"foo","bar"}, {"moo","baz"}})), 0); + TEST_EQUAL(palette.index("b", BlockState({{"foo","baz"}, {"moo","bar"}})), 1); + TEST_EQUAL(palette.index("c", BlockState({{"foo","baz"}, {"moo","bar"}})), ProtocolBlockTypePalette::NOT_FOUND); +} + + + + + +static void TestComplex2(void) +{ + LOG("Test TestComplex2"); + ProtocolBlockTypePalette palette; + auto str = "{\"Metadata\":{\"ProtocolBlockTypePaletteVersion\":1}, \"Palette\":[{\ + \"id\": \"0\", \ + \"name\": \"a\"\ + }, {\ + \"id\": \"1\", \ + \"name\": \"b\"\ + }]}"; + TEST_TRUE(palette.loadFromString(str)); + TEST_EQUAL(palette.index("a", BlockState()), 0); + TEST_EQUAL(palette.index("b", BlockState({})), 1); +} + + + + + +static void TestFile(void) +{ + LOG("Test TestFile"); + std::ifstream f("base.btp.json"); + ProtocolBlockTypePalette palette; + TEST_TRUE(palette.loadFromStream(f)); + + // This is a bit problematic - the only permanently fixed block Id is air... + TEST_EQUAL(palette.index("minecraft:air", BlockState()), 0); + TEST_NOTEQUAL(palette.index("minecraft:stone", BlockState()), ProtocolBlockTypePalette::NOT_FOUND); + TEST_NOTEQUAL(palette.index("minecraft:dirt", BlockState()), ProtocolBlockTypePalette::NOT_FOUND); +} + + + + + +static void TestFile2(void) +{ + LOG("Test TestFile2"); + std::ifstream f("test.btp.json"); + ProtocolBlockTypePalette palette; + TEST_TRUE(palette.loadFromStream(f)); + + TEST_EQUAL(palette.index("minecraft:air", BlockState({})), 0); + TEST_EQUAL(palette.index("minecraft:stone", BlockState()), 1); + TEST_EQUAL(palette.index("minecraft:stone", BlockState()), 1); + TEST_EQUAL(palette.index( + "minecraft:dark_oak_leaves", + BlockState({{"persistent", "false"}, {"distance", "6"}}) + ), 225); + TEST_EQUAL(palette.index( + "minecraft:dark_oak_leaves", + BlockState({{"persistent", "false"}}) + ), ProtocolBlockTypePalette::NOT_FOUND); +} + + + + + +IMPLEMENT_TEST_MAIN("ProtocolBlockTypePaletteTest", + TestSuccess(); + TestErrors(); + TestComplex1(); + TestComplex2(); + TestFile(); + TestFile2(); +) diff --git a/tests/ProtocolBlockTypePalette/test.btp.json b/tests/ProtocolBlockTypePalette/test.btp.json new file mode 100644 index 000000000..264e56185 --- /dev/null +++ b/tests/ProtocolBlockTypePalette/test.btp.json @@ -0,0 +1,146 @@ +{ + "Metadata": { + "ProtocolBlockTypePaletteVersion": 1 + }, + "Palette": [{ + "id": 0, + "name": "minecraft:air" + }, { + "id": 1, + "name": "minecraft:stone" + }, { + "id": 221, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "false", + "distance": "4" + } + }, { + "id": 222, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "true", + "distance": "5" + } + }, { + "id": 223, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "false", + "distance": "5" + } + }, { + "id": 224, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "true", + "distance": "6" + } + }, { + "id": 225, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "false", + "distance": "6" + } + }, { + "id": 226, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "true", + "distance": "7" + } + }, { + "id": 227, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "false", + "distance": "7" + } + }, { + "id": 9988, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "north_south" + } + }, { + "id": 9989, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "east_west" + } + }, { + "id": 9990, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "ascending_east" + } + }, { + "id": 9991, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "ascending_west" + } + }, { + "id": 9992, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "ascending_north" + } + }, { + "id": 9993, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "ascending_south" + } + }, { + "id": 9994, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "north_south" + } + }, { + "id": 9995, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "east_west" + } + }, { + "id": 9996, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "ascending_east" + } + }, { + "id": 9997, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "ascending_west" + } + }, { + "id": 9998, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "ascending_north" + } + }, { + "id": 9999, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "ascending_south" + } + } + ] +} |