From 2d6f6a574d8dd6a94dc13cb51628626d99c1853a Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 20 Dec 2019 22:10:24 +0100 Subject: BlockTypePalette: Load from TSV or original reports' JSON. --- tests/BlockTypeRegistry/BlockTypePaletteTest.cpp | 215 +++++++++++++++-------- 1 file changed, 141 insertions(+), 74 deletions(-) (limited to 'tests/BlockTypeRegistry/BlockTypePaletteTest.cpp') 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(0, true))); - TEST_EQUAL(palette.maybeIndex("b", BlockState({{"foo", "baz"}})), (std::make_pair(0, false))); - TEST_EQUAL(palette.maybeIndex("a", BlockState({{"foo", "bar"}})), (std::make_pair(0, false))); + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState()), (std::make_pair(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(21, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:oak_sapling", {{"stage", "1"}}), (std::make_pair(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(0, true))); - TEST_EQUAL(palette.maybeIndex("b", {{"foo", "baz"}, {"moo", "bar"}}), (std::make_pair(1001, true))); - TEST_EQUAL(palette.maybeIndex("c", {{"foo", "baz"}, {"moo", "bar"}}).second, false); + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState()), (std::make_pair(0, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:stone", BlockState()), (std::make_pair(1, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState({{"snow_covered", "0"}})), (std::make_pair(2, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState({{"snow_covered", "1"}})), (std::make_pair(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(0, true))); - TEST_EQUAL(palette.maybeIndex("b", BlockState()), (std::make_pair(1, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState()), (std::make_pair(0, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:stone", BlockState()), (std::make_pair(16, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState({{"snow_covered", "0"}})), (std::make_pair(32, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:grass", BlockState({{"snow_covered", "1"}})), (std::make_pair(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(0, true))); TEST_EQUAL(palette.maybeIndex("minecraft:stone", BlockState()), (std::make_pair(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(0, true))); - TEST_EQUAL(palette.maybeIndex("minecraft:stone", BlockState()), (std::make_pair(1, true))); - TEST_EQUAL(palette.maybeIndex("minecraft:dirt", BlockState()), (std::make_pair(10, true))); + { + auto fileContents = cFile::ReadWholeFile("UpgradeBlockTypePalette.txt"); + cStopwatch sw("Loading upgrade palette"); + palette.loadFromString(fileContents); + } + + TEST_EQUAL(palette.entry(0), (std::make_pair("minecraft:air", {}))); + TEST_EQUAL(palette.entry(44 * 16 + 8), (std::make_pair("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(); ) -- cgit v1.2.3