summaryrefslogtreecommitdiffstats
path: root/tests/BlockTypeRegistry/BlockStateTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/BlockTypeRegistry/BlockStateTest.cpp')
-rw-r--r--tests/BlockTypeRegistry/BlockStateTest.cpp58
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();
)