diff options
author | E14 <1640391+E14@users.noreply.github.com> | 2019-09-22 22:57:54 +0200 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2019-09-22 22:57:54 +0200 |
commit | d1c95742ddd83899bb35051de9d731d38aba80a4 (patch) | |
tree | 74be95b8f98def76a47c5fc81cca59a12bee4c00 /tests/BlockTypeRegistry | |
parent | Added missing closing } in message output (diff) | |
download | cuberite-d1c95742ddd83899bb35051de9d731d38aba80a4.tar cuberite-d1c95742ddd83899bb35051de9d731d38aba80a4.tar.gz cuberite-d1c95742ddd83899bb35051de9d731d38aba80a4.tar.bz2 cuberite-d1c95742ddd83899bb35051de9d731d38aba80a4.tar.lz cuberite-d1c95742ddd83899bb35051de9d731d38aba80a4.tar.xz cuberite-d1c95742ddd83899bb35051de9d731d38aba80a4.tar.zst cuberite-d1c95742ddd83899bb35051de9d731d38aba80a4.zip |
Diffstat (limited to 'tests/BlockTypeRegistry')
-rw-r--r-- | tests/BlockTypeRegistry/BlockStateTest.cpp | 58 |
1 files changed, 58 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(); ) |