From 83b856b7d42deba868608f323a3cec4ae6a17d90 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Tue, 14 Jan 2025 23:30:53 +0100 Subject: Add custom hasher & comparator for hash_set keys --- asl/tests/hash_set_tests.cpp | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'asl/tests') diff --git a/asl/tests/hash_set_tests.cpp b/asl/tests/hash_set_tests.cpp index e6a020a..9df9463 100644 --- a/asl/tests/hash_set_tests.cpp +++ b/asl/tests/hash_set_tests.cpp @@ -32,7 +32,6 @@ ASL_TEST(a_bunch_of_strings) ASL_TEST_EXPECT(set.contains("Hello, world!"_sv)); ASL_TEST_EXPECT(set.contains("Hello, puppy!"_sv)); ASL_TEST_EXPECT(!set.contains("Hello, Steven!"_sv)); - } ASL_TEST(a_bunch_of_ints) @@ -67,11 +66,31 @@ struct HashWithDestructor: public DestructorObserver { return x == other.x; } +}; + +struct CustomComparator +{ + static bool eq(const HashWithDestructor& a, const HashWithDestructor& b) + { + return a.x == b.x; + } + + static bool eq(const HashWithDestructor& a, int b) + { + return a.x == b; + } +}; - template - friend H AslHashValue(H h, const HashWithDestructor& value) +struct CustomHasher +{ + static uint64_t hash(const HashWithDestructor& b) + { + return asl::hash_value(b.x); + } + + static uint64_t hash(int x) { - return H::combine(ASL_MOVE(h), value.x); + return asl::hash_value(x); } }; @@ -81,7 +100,7 @@ ASL_TEST(destructor_and_remove) bool destroyed[kCount]{}; { - asl::hash_set set; + asl::hash_set set; for (int i = 0; i < kCount; ++i) { @@ -97,14 +116,13 @@ ASL_TEST(destructor_and_remove) for (int i = 0; i < kCount; i += 2) { - // @Todo Remove with something comparable - ASL_TEST_EXPECT(set.remove(HashWithDestructor{i, nullptr})); + ASL_TEST_EXPECT(set.remove(i)); } for (int i = 0; i < kCount; i += 2) { - ASL_TEST_EXPECT(!set.contains(HashWithDestructor{i, nullptr})); - ASL_TEST_EXPECT(set.contains(HashWithDestructor{i+1, nullptr})); + ASL_TEST_EXPECT(!set.contains(i)); + ASL_TEST_EXPECT(set.contains(i+1)); ASL_TEST_EXPECT(destroyed[i]); // NOLINT ASL_TEST_EXPECT(!destroyed[i + 1]); // NOLINT } -- cgit