summaryrefslogtreecommitdiffstats
path: root/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2019-12-01 14:41:46 +0100
committerMattes D <github@xoft.cz>2019-12-28 22:43:35 +0100
commit2de6b7537d37dff82afe5563704949e9d4131a52 (patch)
treee82888542a559315dc59dacee61c09e6f7946c8e /tests/BlockTypeRegistry/BlockTypePaletteTest.cpp
parentUpdate submodules (#4439) (diff)
downloadcuberite-2de6b7537d37dff82afe5563704949e9d4131a52.tar
cuberite-2de6b7537d37dff82afe5563704949e9d4131a52.tar.gz
cuberite-2de6b7537d37dff82afe5563704949e9d4131a52.tar.bz2
cuberite-2de6b7537d37dff82afe5563704949e9d4131a52.tar.lz
cuberite-2de6b7537d37dff82afe5563704949e9d4131a52.tar.xz
cuberite-2de6b7537d37dff82afe5563704949e9d4131a52.tar.zst
cuberite-2de6b7537d37dff82afe5563704949e9d4131a52.zip
Diffstat (limited to 'tests/BlockTypeRegistry/BlockTypePaletteTest.cpp')
-rw-r--r--tests/BlockTypeRegistry/BlockTypePaletteTest.cpp108
1 files changed, 60 insertions, 48 deletions
diff --git a/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp b/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp
index ef79d8927..1337c7dc3 100644
--- a/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp
+++ b/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp
@@ -35,11 +35,12 @@ static void testBasic()
TEST_EQUAL(pal.count(), 5);
// Check the entry() API:
- TEST_EQUAL(pal.entry(0), (std::make_pair<AString, BlockState>("testblock", BlockState())));
- TEST_EQUAL(pal.entry(1), (std::make_pair<AString, BlockState>("another", BlockState())));
+ TEST_EQUAL(pal.entry(0), (std::make_pair<AString, BlockState>("testblock", BlockState())));
+ TEST_EQUAL(pal.entry(1), (std::make_pair<AString, BlockState>("another", BlockState())));
TEST_EQUAL(pal.entry(2), (std::make_pair<AString, BlockState>("multistate", BlockState(bs1)))); // make_pair requires a copy of the state
TEST_EQUAL(pal.entry(3), (std::make_pair<AString, BlockState>("multistate", BlockState(bs2))));
TEST_EQUAL(pal.entry(4), (std::make_pair<AString, BlockState>("multistate", BlockState(bs3))));
+ TEST_THROWS(pal.entry(5), BlockTypePalette::NoSuchIndexException);
}
@@ -47,26 +48,26 @@ static void testBasic()
/** Tests creating the transform map between two palettes. */
-static void testTransform()
+static void testTransformAddMissing()
{
- LOGD("Testing the createTransformMap API...");
+ LOGD("Testing the createTransformMapAddMissing API...");
// Create two palettes with some overlap:
BlockTypePalette pal1, pal2;
- pal1.index("block1", BlockState());
- pal1.index("block2", BlockState());
- pal1.index("block3", BlockState());
- pal1.index("block4", BlockState());
- pal1.index("block5", BlockState("key1", "value1"));
- pal2.index("block0", BlockState());
- pal2.index("block2", BlockState()); // overlap
- pal2.index("block3", BlockState()); // overlap
- pal2.index("block4", BlockState("key1", "value1"));
- pal2.index("block5", BlockState("key1", "value1")); // overlap
- pal2.index("block6", BlockState("key1", "value1"));
+ /* 0 */ pal1.index("block1", BlockState());
+ /* 1 */ pal1.index("block2", BlockState());
+ /* 2 */ pal1.index("block3", BlockState());
+ /* 3 */ pal1.index("block4", BlockState());
+ /* 4 */ pal1.index("block5", BlockState("key1", "value1"));
+ /* 0 */ pal2.index("block0", BlockState());
+ /* 1 */ pal2.index("block2", BlockState()); // overlap
+ /* 2 */ pal2.index("block4", BlockState()); // overlap
+ /* 3 */ pal2.index("block4", BlockState("key1", "value1"));
+ /* 4 */ pal2.index("block5", BlockState("key1", "value1")); // overlap
+ /* 5 */ pal2.index("block6", BlockState("key1", "value1"));
// Check the transform map:
- auto trans = pal1.createTransformMap(pal2);
+ auto trans = pal1.createTransformMapAddMissing(pal2);
TEST_EQUAL(pal1.maybeIndex("block1", BlockState()), (std::make_pair<UInt32, bool>(0, true)));
TEST_EQUAL(pal1.maybeIndex("block2", BlockState()), (std::make_pair<UInt32, bool>(1, true)));
TEST_EQUAL(pal1.maybeIndex("block3", BlockState()), (std::make_pair<UInt32, bool>(2, true)));
@@ -76,47 +77,58 @@ static void testTransform()
TEST_EQUAL(pal1.maybeIndex("block4", BlockState("key1", "value1")), (std::make_pair<UInt32, bool>(6, true)));
TEST_EQUAL(pal1.maybeIndex("block6", BlockState("key1", "value1")), (std::make_pair<UInt32, bool>(7, true)));
TEST_EQUAL(trans.size(), 6);
- TEST_EQUAL(trans[0], 5);
- TEST_EQUAL(trans[1], 1);
- TEST_EQUAL(trans[2], 2);
- TEST_EQUAL(trans[3], 6);
- TEST_EQUAL(trans[4], 4);
- TEST_EQUAL(trans[5], 7);
+ TEST_EQUAL(trans[0], 5); // Added
+ TEST_EQUAL(trans[1], 1); // Mapped
+ TEST_EQUAL(trans[2], 3); // Mapped
+ TEST_EQUAL(trans[3], 6); // Added
+ TEST_EQUAL(trans[4], 4); // Mapped
+ TEST_EQUAL(trans[5], 7); // Added
}
-int main()
+/** Tests creating the transform map between two palettes, with fallback. */
+static void testTransformWithFallback()
{
- LOGD("BlockTypePaletteTest started");
-
- try
- {
- testBasic();
- testTransform();
- }
- catch (const TestException & exc)
- {
- LOGERROR("BlockTypePaletteTest has failed, an exception was thrown: %s", exc.mMessage.c_str());
- return 1;
- }
- catch (const std::exception & exc)
- {
- LOGERROR("BlockTypePaletteTest has failed, an exception was thrown: %s", exc.what());
- return 1;
- }
- catch (...)
- {
- LOGERROR("BlockTypePaletteTest has failed, an unhandled exception was thrown.");
- return 1;
- }
-
- LOGD("BlockTypePaletteTest finished");
- return 0;
+ LOGD("Testing the createTransformMapWithFallback API...");
+
+ // Create two palettes with some overlap:
+ BlockTypePalette pal1, pal2;
+ /* 0 */ pal1.index("block1", BlockState());
+ /* 1 */ pal1.index("block2", BlockState());
+ /* 2 */ pal1.index("block3", BlockState());
+ /* 3 */ pal1.index("block4", BlockState());
+ /* 4 */ pal1.index("block5", BlockState("key1", "value1"));
+ /* 0 */ pal2.index("block0", BlockState());
+ /* 1 */ pal2.index("block2", BlockState()); // overlap
+ /* 2 */ pal2.index("block4", BlockState()); // overlap
+ /* 3 */ pal2.index("block4", BlockState("key1", "value1"));
+ /* 4 */ pal2.index("block5", BlockState("key1", "value1")); // overlap
+ /* 5 */ pal2.index("block6", BlockState("key1", "value1"));
+
+ // Check the transform map:
+ auto trans = pal1.createTransformMapWithFallback(pal2, 0);
+ TEST_EQUAL(trans.size(), 6);
+ TEST_EQUAL(trans[0], 0); // Fallback
+ TEST_EQUAL(trans[1], 1); // Mapped
+ TEST_EQUAL(trans[2], 3); // Mapped
+ TEST_EQUAL(trans[3], 0); // Fallback
+ TEST_EQUAL(trans[4], 4); // Mapped
+ TEST_EQUAL(trans[5], 0); // Fallback
}
+
+IMPLEMENT_TEST_MAIN("BlockTypePalette",
+ testBasic();
+ testTransformAddMissing();
+ testTransformWithFallback();
+)
+
+
+
+