summaryrefslogtreecommitdiff
path: root/asl/tests
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2025-01-07 23:17:50 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2025-01-07 23:17:50 +0100
commitb94a42b978251c4cdb4eb0be2a2e8d9dc8949eba (patch)
tree81ac62603714fbee3db7cc2c616d170cd6b5fe2c /asl/tests
parente18b054779766269a4b9ca68729c380d24c0535d (diff)
More work on hashing
Diffstat (limited to 'asl/tests')
-rw-r--r--asl/tests/hash_tests.cpp88
-rw-r--r--asl/tests/meta_tests.cpp11
2 files changed, 97 insertions, 2 deletions
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<int*>);
+static_assert(!asl::hashable<int[]>);
+static_assert(!asl::hashable<int[9]>);
static_assert(asl::hashable<uint8_t>);
static_assert(asl::hashable<uint16_t>);
@@ -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<asl::span<const int>>);
+static_assert(asl::hashable<asl::span<asl::string_view>>);
+
+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<int>(ints1)) == asl::hash_value(asl::span<int>(ints2)));
+ ASL_TEST_EXPECT(asl::hash_value(asl::span<int>(ints1)) != asl::hash_value(asl::span<int>(ints3)));
+ ASL_TEST_EXPECT(asl::hash_value(asl::span<int>(ints1)) != asl::hash_value(asl::span<int>(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<asl::string_view>(strs1)) == asl::hash_value(asl::span<asl::string_view>(strs2)));
+ ASL_TEST_EXPECT(asl::hash_value(asl::span<asl::string_view>(strs1)) != asl::hash_value(asl::span<asl::string_view>(strs3)));
+ ASL_TEST_EXPECT(asl::hash_value(asl::span<asl::string_view>(strs1)) != asl::hash_value(asl::span<asl::string_view>(strs4)));
+}
+
+static_assert(asl::hashable<asl::buffer<int>>);
+
+ASL_TEST(buffer)
+{
+ asl::buffer<int> ints1;
+ ints1.push(1);
+ ints1.push(2);
+ ints1.push(3);
+
+ asl::buffer<int> ints2;
+ ints2.push(1);
+ ints2.push(2);
+ ints2.push(3);
+
+ asl::buffer<int> ints3;
+ ints3.push(1);
+ ints3.push(2);
+
+ asl::buffer<int> 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<asl::string_view> strs1;
+ strs1.push("Hello");
+ strs1.push("World");
+
+ asl::buffer<asl::string_view> strs2;
+ strs2.push("Hello");
+ strs2.push("World");
+
+ asl::buffer<asl::string_view> strs3;
+ strs3.push("Hello");
+ strs3.push("world");
+
+ asl::buffer<asl::string_view> 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<Enum1>);
+static_assert(asl::hashable<Enum2>);
+
// @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<C>);
static_assert(asl::uniquely_represented<int>);
static_assert(asl::uniquely_represented<uint128_t>);
static_assert(!asl::uniquely_represented<bool>);
+
+enum Enum1 {};
+enum class Enum2 {};
+
+static_assert(asl::uniquely_represented<Enum1>);
+static_assert(asl::uniquely_represented<Enum2>);
+
+static_assert(!asl::is_enum<int>);
+static_assert(asl::is_enum<Enum1>);
+static_assert(asl::is_enum<Enum2>);
+