diff options
author | Mattes D <github@xoft.cz> | 2019-08-23 16:17:05 +0200 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2019-08-24 12:03:38 +0200 |
commit | 02fbf1686565ace00df5af938d95fbe18783fc37 (patch) | |
tree | e109749b5823f758d605dd2a8f5d2c8b8b22878b /tests/BlockTypeRegistry | |
parent | Upgrade Appveyor's MSVC (diff) | |
download | cuberite-02fbf1686565ace00df5af938d95fbe18783fc37.tar cuberite-02fbf1686565ace00df5af938d95fbe18783fc37.tar.gz cuberite-02fbf1686565ace00df5af938d95fbe18783fc37.tar.bz2 cuberite-02fbf1686565ace00df5af938d95fbe18783fc37.tar.lz cuberite-02fbf1686565ace00df5af938d95fbe18783fc37.tar.xz cuberite-02fbf1686565ace00df5af938d95fbe18783fc37.tar.zst cuberite-02fbf1686565ace00df5af938d95fbe18783fc37.zip |
Diffstat (limited to 'tests/BlockTypeRegistry')
-rw-r--r-- | tests/BlockTypeRegistry/BlockStateTest.cpp | 145 | ||||
-rw-r--r-- | tests/BlockTypeRegistry/CMakeLists.txt | 13 |
2 files changed, 158 insertions, 0 deletions
diff --git a/tests/BlockTypeRegistry/BlockStateTest.cpp b/tests/BlockTypeRegistry/BlockStateTest.cpp new file mode 100644 index 000000000..d3ce92455 --- /dev/null +++ b/tests/BlockTypeRegistry/BlockStateTest.cpp @@ -0,0 +1,145 @@ +#include "Globals.h" +#include "BlockState.h" +#include "../TestHelpers.h" + + + + +/** Tests the class constructors with static (hard-coded) data. */ +static void testStaticCreation() +{ + LOGD("Testing BlockState creation from static data..."); + + // Create a few BlockStates using the static-data constructors: + BlockState bs1v1; + BlockState bs2v1("property", "value"); + BlockState bs3v1({{"property1", "value1"}, {"property2", "value2"}}); + BlockState bs1v2(bs1v1); + BlockState bs2v2(bs2v1); + BlockState bs3v2(bs3v1); + BlockState bs1v3(bs1v2, {{"added property", "value1"}}); + BlockState bs2v3(bs2v2, {{"added property", "value2"}}); + BlockState bs3v3(bs3v2, {{"added property", "value3"}}); + + // Test (in-)equality (v1 = v2 != v3): + TEST_EQUAL(bs1v1, bs1v2); + TEST_EQUAL(bs2v1, bs2v2); + TEST_EQUAL(bs3v1, bs3v2); + TEST_NOTEQUAL(bs1v1, bs1v3); + TEST_NOTEQUAL(bs2v1, bs2v3); + TEST_NOTEQUAL(bs3v1, bs3v3); + TEST_NOTEQUAL(bs1v2, bs1v3); + TEST_NOTEQUAL(bs2v2, bs2v3); + TEST_NOTEQUAL(bs3v2, bs3v3); + + // Test that the values are actually stored: + TEST_EQUAL(bs1v1.value("property"), ""); + TEST_EQUAL(bs2v1.value("property"), "value"); + TEST_EQUAL(bs2v1.value("property1"), ""); + TEST_EQUAL(bs3v1.value("property1"), "value1"); + TEST_EQUAL(bs3v1.value("property2"), "value2"); + TEST_EQUAL(bs1v3.value("added property"), "value1"); + TEST_EQUAL(bs2v3.value("added property"), "value2"); + TEST_EQUAL(bs3v3.value("added property"), "value3"); +} + + + + + +/** Tests the dynamic-data constructors (map param, deep-copy). */ +static void testDynamicCreation() +{ + LOGD("Testing BlockState creation from dynamic data..."); + + using Map = std::map<AString, AString>; + + // Construct from scratch: + { + BlockState bs1a({{"property", "value"}}); + Map map1({{"property", "value"}}); + BlockState bs1b(map1); + TEST_EQUAL(bs1a, bs1b); // Creation works + map1.clear(); + TEST_EQUAL(bs1a, bs1b); // Created a copy independent of map1 + } + + // Construct by moving: + { + BlockState bs2a({{"property", "value"}}); + Map map2({{"property", "value"}}); + BlockState bs2b(std::move(map2)); + TEST_EQUAL(bs2a, bs2b); // Creation works + } + + // Construct by modifying: + { + BlockState bsSrc("property1", "value1"); + BlockState bs3a(bsSrc, {{"property2", "value2"}}); + Map map3({{"property2", "value2"}}); + BlockState bs3b(bsSrc, map3); + TEST_EQUAL(bs3a, bs3b); + map3.clear(); + TEST_EQUAL(bs3a, bs3b); + } +} + + + + + +/** Tests replacing the properties in the copy-and-modify constructors. */ +static void testReplacing() +{ + LOGD("Testing replacing / removing properties in BlockState copies..."); + + // Test replacing: + BlockState bs1("property1", "value1v1"); + BlockState bs2(bs1, {{"property1", "value1v2"}}); + TEST_EQUAL(bs2.value("property1"), "value1v2"); // Stored the new one + TEST_EQUAL(bs1.value("property1"), "value1v1"); // Didn't replace in the original + + // Test removing: + BlockState bs3(bs1, {{"property1", ""}}); + BlockState bsEmpty; + TEST_EQUAL(bs3, bsEmpty); +} + + + + + +int main() +{ + LOGD("BlockStateTest started"); + + try + { + testStaticCreation(); + testDynamicCreation(); + testReplacing(); + } + catch (const TestException & exc) + { + LOGERROR("BlockStateTest has failed explicitly: %s", exc.mMessage.c_str()); + return 1; + } + catch (const std::runtime_error & exc) + { + LOGERROR("BlockStateTest has failed, an unhandled exception was thrown: %s", exc.what()); + return 1; + } + catch (...) + { + LOGERROR("BlockStateTest has failed, an unknown exception was thrown."); + return 1; + } + + LOGD("BlockStateTest finished"); + + return 0; +} + + + + diff --git a/tests/BlockTypeRegistry/CMakeLists.txt b/tests/BlockTypeRegistry/CMakeLists.txt index 25b18c373..c95c503dd 100644 --- a/tests/BlockTypeRegistry/CMakeLists.txt +++ b/tests/BlockTypeRegistry/CMakeLists.txt @@ -11,6 +11,16 @@ add_definitions(-DTEST_GLOBALS=1) # Define individual test executables: +# BlockStateTest: Verify that the BlockState class works as intended: +add_executable(BlockStateTest + BlockStateTest.cpp + ../TestHelpers.h + ${CMAKE_SOURCE_DIR}/src/BlockState.cpp + ${CMAKE_SOURCE_DIR}/src/StringUtils.cpp + ${CMAKE_SOURCE_DIR}/src/OSSupport/CriticalSection.cpp +) +target_link_libraries(BlockStateTest fmt::fmt) + # BlockTypeRegistryTest: Verify that the BlockTypeRegistry class works as intended: add_executable(BlockTypeRegistryTest BlockTypeRegistryTest.cpp @@ -25,8 +35,10 @@ target_link_libraries(BlockTypeRegistryTest fmt::fmt) + # Define individual tests: +add_test(NAME BlockStateTest COMMAND BlockStateTest) add_test(NAME BlockTypeRegistryTest COMMAND BlockTypeRegistryTest) @@ -35,6 +47,7 @@ add_test(NAME BlockTypeRegistryTest COMMAND BlockTypeRegistryTest) # Put all the tests into a solution folder (MSVC): set_target_properties( + BlockStateTest BlockTypeRegistryTest PROPERTIES FOLDER Tests/BlockTypeRegistry ) |