From 50af00b4684baa5823567c634da701b21701cbef Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Thu, 20 Feb 2025 00:29:43 +0100 Subject: [PATCH] add hash properties test for interfaces --- tests/unittests/interface_types.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/unittests/interface_types.cpp b/tests/unittests/interface_types.cpp index 6d345066c..7d36c303a 100644 --- a/tests/unittests/interface_types.cpp +++ b/tests/unittests/interface_types.cpp @@ -61,6 +61,29 @@ TEST_CASE("InterfaceTypes static checks", "[interface-types][static-checks]") { STATIC_REQUIRE(TypeWithEnergy::typeName == "TypeWithEnergy"sv); } +TEST_CASE("InterfaceTypes hash", "[interface-types][hash]") { + auto hit = ExampleHit(); + auto wrapper1 = TypeWithEnergy(hit); + auto hash1 = std::hash{}(wrapper1); + + // rehashing should give the same result + auto rehash = std::hash{}(wrapper1); + REQUIRE(rehash == hash1); + + // same object should have the same hash + auto wrapper2 = wrapper1; + auto hash2 = std::hash{}(wrapper2); + REQUIRE(wrapper2 == wrapper1); + REQUIRE(hash2 == hash1); + + // different objects should have different hashes + auto different_hit = ExampleHit(); + auto different_wrapper = TypeWithEnergy(different_hit); + auto hash_different = std::hash{}(different_wrapper); + REQUIRE(different_wrapper != wrapper1); + REQUIRE(hash_different != hash1); +} + TEST_CASE("InterfaceTypes STL usage", "[interface-types][basics][hash]") { // Make sure that interface types can be used with STL map and set std::map counterMap{};