From d1c95742ddd83899bb35051de9d731d38aba80a4 Mon Sep 17 00:00:00 2001 From: E14 <1640391+E14@users.noreply.github.com> Date: Sun, 22 Sep 2019 22:57:54 +0200 Subject: Add ProtocolBlockTypePalette (#4391) --- tests/BlockTypeRegistry/BlockStateTest.cpp | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'tests/BlockTypeRegistry') 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 v; + std::map map1; + std::map map2; + + for (int i = 0; i < 128; ++i) + { + v.push_back(BlockState({{std::string(1, static_cast(0x1F)), std::string(1, static_cast(i))}})); + v.push_back(BlockState({{std::string(1, static_cast(0x10)), std::string(1, static_cast(i | 0x80))}, + {std::string(1, static_cast(0x0F)), std::string(1, static_cast(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(); ) -- cgit v1.2.3