diff options
author | Mattes D <github@xoft.cz> | 2019-12-20 22:10:24 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2019-12-28 22:43:35 +0100 |
commit | 2d6f6a574d8dd6a94dc13cb51628626d99c1853a (patch) | |
tree | 450b60da6dba1b8f7d603f6d3f2323235a0d83b7 /tests/BlockTypeRegistry | |
parent | Moved ProtocolBlockTypePalette functionality into BlockTypePalette. (diff) | |
download | cuberite-2d6f6a574d8dd6a94dc13cb51628626d99c1853a.tar cuberite-2d6f6a574d8dd6a94dc13cb51628626d99c1853a.tar.gz cuberite-2d6f6a574d8dd6a94dc13cb51628626d99c1853a.tar.bz2 cuberite-2d6f6a574d8dd6a94dc13cb51628626d99c1853a.tar.lz cuberite-2d6f6a574d8dd6a94dc13cb51628626d99c1853a.tar.xz cuberite-2d6f6a574d8dd6a94dc13cb51628626d99c1853a.tar.zst cuberite-2d6f6a574d8dd6a94dc13cb51628626d99c1853a.zip |
Diffstat (limited to 'tests/BlockTypeRegistry')
-rw-r--r-- | tests/BlockTypeRegistry/BlockTypePaletteTest.cpp | 215 | ||||
-rw-r--r-- | tests/BlockTypeRegistry/CMakeLists.txt | 4 | ||||
-rw-r--r-- | tests/BlockTypeRegistry/test.btp.json | 146 |
3 files changed, 143 insertions, 222 deletions
diff --git a/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp b/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp index dddf80348..995552085 100644 --- a/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp +++ b/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp @@ -1,6 +1,7 @@ #include "Globals.h" #include "../TestHelpers.h" #include "BlockTypePalette.h" +#include "Stopwatch.h" @@ -123,110 +124,171 @@ static void testTransformWithFallback() +/** Tests that loading fails for nonsense input */ +static void testLoadErrors(void) +{ + LOG("Testing palette load error reporting."); + + BlockTypePalette palette; + TEST_THROWS(palette.loadFromString(""), BlockTypePalette::LoadFailedException); + TEST_THROWS(palette.loadFromString("[]"), BlockTypePalette::LoadFailedException); + TEST_THROWS(palette.loadFromString("a = {}"), BlockTypePalette::LoadFailedException); + TEST_THROWS(palette.loadFromString("{x = 1}"), BlockTypePalette::LoadFailedException); // Lua style + TEST_THROWS(palette.loadFromString("$#^%&"), BlockTypePalette::LoadFailedException); +} + + + + + /** Tests that loading a simple JSON palette succeeds. */ -static void testLoadSimpleSuccess(void) +static void testLoadJsonSimple(void) { LOG("Testing loading a simple JSON palette"); BlockTypePalette palette; - auto example = "{\"Metadata\":{\"ProtocolBlockTypePaletteVersion\":1}, \"Palette\":[{\ - \"props\": {\ - \"foo\": \"bar\"\ - }, \ - \"name\": \"b\", \ - \"id\": \"0\"\ - }]}"; + auto example = " \ + { \ + \"minecraft:air\": { \ + \"states\": [ \ + { \ + \"id\": 0, \ + \"default\": true \ + } \ + ] \ + } \ + }"; palette.loadFromString(example); - TEST_EQUAL(palette.maybeIndex("b", BlockState({{"foo", "bar"}})), (std::make_pair<UInt32, bool>(0, true))); - TEST_EQUAL(palette.maybeIndex("b", BlockState({{"foo", "baz"}})), (std::make_pair<UInt32, bool>(0, false))); - TEST_EQUAL(palette.maybeIndex("a", BlockState({{"foo", "bar"}})), (std::make_pair<UInt32, bool>(0, false))); + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState()), (std::make_pair<UInt32, bool>(0, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState({{"foo", "baz"}})).second, false); + TEST_EQUAL(palette.maybeIndex("minecraft:a", BlockState()).second, false); } -static void testLoadErrors(void) +/** Tests loading a complex block with multiple states and duplicates. */ +static void testLoadJsonComplex(void) { - LOG("Testing palette load error reporting."); - + LOG("Testing loading a complex JSON palette"); BlockTypePalette palette; - TEST_THROWS(palette.loadFromString(""), BlockTypePalette::LoadFailedException); - TEST_THROWS(palette.loadFromString("[]"), BlockTypePalette::LoadFailedException); - TEST_THROWS(palette.loadFromString("a = {}"), BlockTypePalette::LoadFailedException); - TEST_THROWS(palette.loadFromString("{x = 1}"), BlockTypePalette::LoadFailedException); // Lua style - TEST_THROWS(palette.loadFromString("$#^%&"), BlockTypePalette::LoadFailedException); + auto str = " \ + { \ + \"minecraft:oak_sapling\": { \ + \"properties\": { \ + \"stage\": [ \ + \"0\", \ + \"1\" \ + ] \ + }, \ + \"states\": [ \ + { \ + \"properties\": { \ + \"stage\": \"0\" \ + }, \ + \"id\" : 21, \ + \"default\" : true \ + }, \ + { \ + \"properties\": { \ + \"stage\": \"1\" \ + }, \ + \"id\" : 22 \ + }, \ + { \ + \"properties\": { \ + \"stage\": \"1\" \ + }, \ + \"id\" : 23 \ + }\ + ] \ + } \ + }"; + + // Note: The palette has a duplicate entry with differrent IDs, the latter ID wins + palette.loadFromString(str); + TEST_EQUAL(palette.maybeIndex("minecraft:oak_sapling", {{"stage", "10"}}).second, false); + TEST_EQUAL(palette.maybeIndex("minecraft:oak_sapling", {{"stage", "0"}}), (std::make_pair<UInt32, bool>(21, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:oak_sapling", {{"stage", "1"}}), (std::make_pair<UInt32, bool>(23, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:oak_sapling", {{"foo", "baz"}}).second, false); + TEST_EQUAL(palette.maybeIndex("minecraft:oak_sap", {{"stage", "0"}}).second, false); } -static void testLoadComplex1(void) +/** Tests loading a palette from simple regular TSV text data. */ +static void testLoadTsvRegular(void) { - LOG("Testing loading a complex palette (1)"); + LOG("Testing loading a simple regular TSV palette"); BlockTypePalette 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\"\ - }]}"; - // Note: The palette has a duplicate entry with differrent IDs, the latter ID wins + auto str = "\ +BlockTypePalette\r\n\ +FileVersion\t1\n\ +CommonPrefix\tminecraft:\r\n\ +\n\ +0\tair\r\n\ +1\tstone\n\ +2\tgrass\tsnow_covered\t0\n\ +3\tgrass\tsnow_covered\t1\n\ +"; palette.loadFromString(str); - TEST_EQUAL(palette.maybeIndex("b", {{"foo", "bar"}}).second, false); - TEST_EQUAL(palette.maybeIndex("b", {{"foo", "bar"}, {"moo", "baz"}}), (std::make_pair<UInt32, bool>(0, true))); - TEST_EQUAL(palette.maybeIndex("b", {{"foo", "baz"}, {"moo", "bar"}}), (std::make_pair<UInt32, bool>(1001, true))); - TEST_EQUAL(palette.maybeIndex("c", {{"foo", "baz"}, {"moo", "bar"}}).second, false); + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState()), (std::make_pair<UInt32, bool>(0, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:stone", BlockState()), (std::make_pair<UInt32, bool>(1, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState({{"snow_covered", "0"}})), (std::make_pair<UInt32, bool>(2, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState({{"snow_covered", "1"}})), (std::make_pair<UInt32, bool>(3, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState({{"snow_covered", "0"}})).second, false); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState({{"snow_covered", "2"}})).second, false); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState()).second, false); } -static void testLoadComplex2(void) +/** Tests loading a palette from simple upgrade TSV text data. */ +static void testLoadTsvUpgrade(void) { - LOG("Testing loading a complex palette (2)"); + LOG("Testing loading a simple upgrade TSV palette"); BlockTypePalette palette; - auto str = "{\"Metadata\":{\"ProtocolBlockTypePaletteVersion\":1}, \"Palette\":[{\ - \"id\": \"0\", \ - \"name\": \"a\"\ - }, {\ - \"id\": \"1\", \ - \"name\": \"b\"\ - }]}"; + auto str = "\ +UpgradeBlockTypePalette\r\n\ +FileVersion\t1\n\ +CommonPrefix\tminecraft:\r\n\ +\n\ +0\t0\tair\r\n\ +1\t0\tstone\n\ +2\t0\tgrass\tsnow_covered\t0\n\ +2\t1\tgrass\tsnow_covered\t1\n\ +"; palette.loadFromString(str); - TEST_EQUAL(palette.maybeIndex("a", BlockState()), (std::make_pair<UInt32, bool>(0, true))); - TEST_EQUAL(palette.maybeIndex("b", BlockState()), (std::make_pair<UInt32, bool>(1, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState()), (std::make_pair<UInt32, bool>(0, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:stone", BlockState()), (std::make_pair<UInt32, bool>(16, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState({{"snow_covered", "0"}})), (std::make_pair<UInt32, bool>(32, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState({{"snow_covered", "1"}})), (std::make_pair<UInt32, bool>(33, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState({{"snow_covered", "0"}})).second, false); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState({{"snow_covered", "2"}})).second, false); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState()).second, false); } -static void testLoadFromFile1(void) +/** Tests loading a palette from a real-life protocol base file (1.13). */ +static void testLoadFromBaseFile(void) { - LOG("Testing loading a palette from file \"test.btp.json\""); + LOG("Testing loading a palette from file \"base.btp.txt\" (1.13)"); BlockTypePalette palette; - palette.loadFromString(cFile::ReadWholeFile("test.btp.json")); + { + auto fileContents = cFile::ReadWholeFile("base.btp.txt"); + cStopwatch sw("Loading palette"); + palette.loadFromString(fileContents); + } TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState()), (std::make_pair<UInt32, bool>(0, true))); TEST_EQUAL(palette.maybeIndex("minecraft:stone", BlockState()), (std::make_pair<UInt32, bool>(1, true))); @@ -250,15 +312,19 @@ static void testLoadFromFile1(void) -static void testLoadFromFile2(void) +/** Tests loading an upgrade-palette from a real-life upgrade file. */ +static void testLoadFromUpgradeFile(void) { - LOG("Testing loading a palette from file \"base.btp.json\" (version 1.13)"); + LOG("Testing loading an upgrade palette from file \"UpgradeBlockTypePalette.txt\"."); BlockTypePalette palette; - palette.loadFromString(cFile::ReadWholeFile("base.btp.json")); - - TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState()), (std::make_pair<UInt32, bool>(0, true))); - TEST_EQUAL(palette.maybeIndex("minecraft:stone", BlockState()), (std::make_pair<UInt32, bool>(1, true))); - TEST_EQUAL(palette.maybeIndex("minecraft:dirt", BlockState()), (std::make_pair<UInt32, bool>(10, true))); + { + auto fileContents = cFile::ReadWholeFile("UpgradeBlockTypePalette.txt"); + cStopwatch sw("Loading upgrade palette"); + palette.loadFromString(fileContents); + } + + TEST_EQUAL(palette.entry(0), (std::make_pair<AString, BlockState>("minecraft:air", {}))); + TEST_EQUAL(palette.entry(44 * 16 + 8), (std::make_pair<AString, BlockState>("minecraft:stone_slab", {{"type", "top"}}))); } @@ -269,12 +335,13 @@ IMPLEMENT_TEST_MAIN("BlockTypePalette", testBasic(); testTransformAddMissing(); testTransformWithFallback(); - testLoadSimpleSuccess(); testLoadErrors(); - testLoadComplex1(); - testLoadComplex2(); - testLoadFromFile1(); - testLoadFromFile2(); + testLoadJsonSimple(); + testLoadJsonComplex(); + testLoadTsvRegular(); + testLoadTsvUpgrade(); + testLoadFromBaseFile(); + testLoadFromUpgradeFile(); ) diff --git a/tests/BlockTypeRegistry/CMakeLists.txt b/tests/BlockTypeRegistry/CMakeLists.txt index beadc8af3..8118bc93c 100644 --- a/tests/BlockTypeRegistry/CMakeLists.txt +++ b/tests/BlockTypeRegistry/CMakeLists.txt @@ -58,8 +58,8 @@ target_link_libraries(PalettedBlockAreaTest fmt::fmt jsoncpp_lib_static) # Extra files for BlockTypePalette test: file (COPY - test.btp.json - ../../Server/Protocol/1.13/base.btp.json + ../../Server/Protocol/1.13/base.btp.txt + ../../Server/Protocol/UpgradeBlockTypePalette.txt DESTINATION ./ ) diff --git a/tests/BlockTypeRegistry/test.btp.json b/tests/BlockTypeRegistry/test.btp.json deleted file mode 100644 index 264e56185..000000000 --- a/tests/BlockTypeRegistry/test.btp.json +++ /dev/null @@ -1,146 +0,0 @@ -{ - "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" - } - } - ] -} |