From b94a42b978251c4cdb4eb0be2a2e8d9dc8949eba Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Tue, 7 Jan 2025 23:17:50 +0100 Subject: More work on hashing --- asl/tests/hash_tests.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++-- asl/tests/meta_tests.cpp | 11 ++++++ 2 files changed, 97 insertions(+), 2 deletions(-) (limited to 'asl/tests') diff --git a/asl/tests/hash_tests.cpp b/asl/tests/hash_tests.cpp index d0df77a..1ef051e 100644 --- a/asl/tests/hash_tests.cpp +++ b/asl/tests/hash_tests.cpp @@ -2,6 +2,11 @@ #include "asl/hash.hpp" #include "asl/string_view.hpp" #include "asl/string.hpp" +#include "asl/buffer.hpp" + +static_assert(!asl::hashable); +static_assert(!asl::hashable); +static_assert(!asl::hashable); static_assert(asl::hashable); static_assert(asl::hashable); @@ -51,8 +56,87 @@ ASL_TEST(strings) ASL_TEST_EXPECT(asl::hash_value("hello"_sv) == asl::hash_value(asl::string("hello"_sv))); } -// @Todo span, buffer (add combine_contiguous (optimize uniquely_represented)) -// @Todo enum classes +static_assert(asl::hashable>); +static_assert(asl::hashable>); + +ASL_TEST(span) +{ + int ints1[] = {1, 2, 3}; + int ints2[] = {1, 2, 3}; + int ints3[] = {1, 2}; + int ints4[] = {3, 2, 1}; + + ASL_TEST_EXPECT(asl::hash_value(asl::span(ints1)) == asl::hash_value(asl::span(ints2))); + ASL_TEST_EXPECT(asl::hash_value(asl::span(ints1)) != asl::hash_value(asl::span(ints3))); + ASL_TEST_EXPECT(asl::hash_value(asl::span(ints1)) != asl::hash_value(asl::span(ints4))); + + asl::string_view strs1[] = {"a", "abc", "hello"}; + asl::string_view strs2[] = {"a", "abc", "hello"}; + asl::string_view strs3[] = {"a", "abc"}; + asl::string_view strs4[] = {"a", "abc", "hello", "what"}; + + ASL_TEST_EXPECT(asl::hash_value(asl::span(strs1)) == asl::hash_value(asl::span(strs2))); + ASL_TEST_EXPECT(asl::hash_value(asl::span(strs1)) != asl::hash_value(asl::span(strs3))); + ASL_TEST_EXPECT(asl::hash_value(asl::span(strs1)) != asl::hash_value(asl::span(strs4))); +} + +static_assert(asl::hashable>); + +ASL_TEST(buffer) +{ + asl::buffer ints1; + ints1.push(1); + ints1.push(2); + ints1.push(3); + + asl::buffer ints2; + ints2.push(1); + ints2.push(2); + ints2.push(3); + + asl::buffer ints3; + ints3.push(1); + ints3.push(2); + + asl::buffer ints4; + ints4.push(1); + ints4.push(2); + ints4.push(4); + + ASL_TEST_EXPECT(asl::hash_value(ints1) == asl::hash_value(ints2)); + ASL_TEST_EXPECT(asl::hash_value(ints1) != asl::hash_value(ints3)); + ASL_TEST_EXPECT(asl::hash_value(ints1) != asl::hash_value(ints4)); + ASL_TEST_EXPECT(asl::hash_value(ints1) == asl::hash_value(ints1.as_span())); + + asl::buffer strs1; + strs1.push("Hello"); + strs1.push("World"); + + asl::buffer strs2; + strs2.push("Hello"); + strs2.push("World"); + + asl::buffer strs3; + strs3.push("Hello"); + strs3.push("world"); + + asl::buffer strs4; + strs4.push("Hello"); + strs4.push("World"); + strs4.push("World"); + + ASL_TEST_EXPECT(asl::hash_value(strs1) == asl::hash_value(strs2)); + ASL_TEST_EXPECT(asl::hash_value(strs1) != asl::hash_value(strs3)); + ASL_TEST_EXPECT(asl::hash_value(strs1) != asl::hash_value(strs4)); + ASL_TEST_EXPECT(asl::hash_value(strs1) == asl::hash_value(strs1.as_span())); +} + +enum Enum1 {}; +enum class Enum2 {}; + +static_assert(asl::hashable); +static_assert(asl::hashable); + // @Todo option (optimize uniquely_represented + has_niche) // @Todo status, status_or // @Todo box diff --git a/asl/tests/meta_tests.cpp b/asl/tests/meta_tests.cpp index 4fedd71..c393631 100644 --- a/asl/tests/meta_tests.cpp +++ b/asl/tests/meta_tests.cpp @@ -236,3 +236,14 @@ static_assert(!asl::is_floating_point); static_assert(asl::uniquely_represented); static_assert(asl::uniquely_represented); static_assert(!asl::uniquely_represented); + +enum Enum1 {}; +enum class Enum2 {}; + +static_assert(asl::uniquely_represented); +static_assert(asl::uniquely_represented); + +static_assert(!asl::is_enum); +static_assert(asl::is_enum); +static_assert(asl::is_enum); + -- cgit