summaryrefslogtreecommitdiff
path: root/modules/codec
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2024-01-16 15:26:46 +0100
committerClaudius "keldu" Holeksa <mail@keldu.de>2024-01-16 15:26:46 +0100
commit634f55ceed45197e743c2d1c3df0c97c6f8888ae (patch)
treed1e9663c53fd719ae53745a18b0628ac43314300 /modules/codec
parentb155dff8b068dc87d22eefd07150a681790247b8 (diff)
codec: Adding schema hashes and testing
Diffstat (limited to 'modules/codec')
-rw-r--r--modules/codec/c++/schema_hash.h60
-rw-r--r--modules/codec/tests/schema.cpp66
2 files changed, 111 insertions, 15 deletions
diff --git a/modules/codec/c++/schema_hash.h b/modules/codec/c++/schema_hash.h
index a7f482f..8f66fbf 100644
--- a/modules/codec/c++/schema_hash.h
+++ b/modules/codec/c++/schema_hash.h
@@ -2,7 +2,6 @@
#include <forstio/string_literal.h>
#include "schema.h"
-#include "data.h"
namespace saw {
struct schema_hash_combine {
@@ -12,7 +11,7 @@ struct schema_hash_combine {
};
template<string_literal lit>
-struct schema_hash_literal {
+struct hash_literal {
static constexpr uint64_t apply(uint64_t seed){
constexpr std::string_view view = lit.view();
for(uint64_t i = 0; i < view.size(); ++i){
@@ -23,21 +22,64 @@ struct schema_hash_literal {
};
template<typename Schema>
-struct schema_hash {
+struct schema_hash_seed {
static_assert(always_false<Schema>, "Not schema_hashable");
};
template<>
-struct schema_hash<schema::String> {
+struct schema_hash_seed<schema::SignedInteger> {
+ using Schema = schema::SignedInteger;
+
+ static constexpr uint64_t apply(uint64_t seed){
+ return hash_literal<Schema::name>::apply(seed);
+ }
+};
+
+template<>
+struct schema_hash_seed<schema::UnsignedInteger> {
+ using Schema = schema::UnsignedInteger;
+
+ static constexpr uint64_t apply(uint64_t seed){
+ return hash_literal<Schema::name>::apply(seed);
+ }
+};
+
+template<>
+struct schema_hash_seed<schema::FloatingPoint> {
+ using Schema = schema::FloatingPoint;
+
+ static constexpr uint64_t apply(uint64_t seed){
+ return hash_literal<Schema::name>::apply(seed);
+ }
+};
+
+template<>
+struct schema_hash_seed<schema::String> {
using Schema = schema::String;
- static constexpr uint64_t apply(uint64_t seed, const data<Schema>& val){
- seed = schema_hash_literal<Schema::name>::apply(seed);
- for(size_t i = 0; i < val.size(); ++i){
- seed = schema_hash_combine::apply(seed, static_cast<uint64_t>(val.at(i)));
- }
+ static constexpr uint64_t apply(uint64_t seed){
+ return hash_literal<Schema::name>::apply(seed);
+ }
+};
+
+template<typename P, uint64_t N>
+struct schema_hash_seed<schema::Primitive<P,N>> {
+ using Schema = schema::Primitive<P,N>;
+
+ static constexpr uint64_t apply(uint64_t seed){
+ seed = hash_literal<Schema::name>::apply(seed);
+ seed = schema_hash_seed<P>::apply(seed);
+ seed = schema_hash_combine::apply(seed, N);
return seed;
}
};
+template<typename Schema>
+struct schema_hash {
+ static constexpr uint64_t apply() {
+ constexpr uint64_t seed = 0;
+ return schema_hash_seed<Schema>::apply(seed);
+ }
+};
+
}
diff --git a/modules/codec/tests/schema.cpp b/modules/codec/tests/schema.cpp
index df90eb1..871feb7 100644
--- a/modules/codec/tests/schema.cpp
+++ b/modules/codec/tests/schema.cpp
@@ -1,14 +1,68 @@
#include <forstio/test/suite.h>
+
+#include <forstio/templates.h>
#include "../c++/schema.h"
#include "../c++/schema_hash.h"
namespace {
-SAW_TEST("Schema String Hash"){
- using namespace saw;
- data<schema::String> str{"foo"};
- uint64_t hash = schema_hash<schema::String>::apply(0, str);
- uint64_t expected_hash = 1524024712123765130u;
+template<typename T>
+struct schema_hash_test {
+ static_assert(saw::always_false<T>, "Needs the test pair");
+};
+
+template<typename T, uint64_t expected>
+struct schema_test_pair{};
+
+template<typename T, uint64_t expected>
+struct schema_hash_test<schema_test_pair<T,expected>> {
+ static void check(){
+ using namespace saw;
+ uint64_t hash = schema_hash<T>::apply();
+ SAW_EXPECT( hash == expected, std::string{"Hash for "} + std::string{T::name.view()} + " is " + std::to_string(hash) +", but should be "+ std::to_string(expected) + ".");
+ }
+};
+
+template<typename... T>
+struct schema_hash_test_multi {
+ template<uint64_t i>
+ static void check_ele(){
+ using type = typename saw::parameter_pack_type<i,T...>::type;
+
+ schema_hash_test<type>::check();
- SAW_EXPECT( hash == expected_hash, std::string{"Hash is "} + std::to_string(hash) +", but should be 0.");
+ if constexpr ( (i+1) < sizeof...(T)){
+ check_ele<i+1>();
+ }
+ }
+
+ static void check(){
+ if constexpr (sizeof...(T) > 0){
+ check_ele<0>();
+ }
+ }
+};
+
+namespace schema {
+using namespace saw::schema;
+}
+
+SAW_TEST("Schema Hashes"){
+ using namespace saw;
+ schema_hash_test_multi<
+ schema_test_pair<schema::UInt8 , 17448981058701187403u>,
+ schema_test_pair<schema::UInt16, 17448981058701187400u>,
+ schema_test_pair<schema::UInt32, 17448981058701187398u>,
+ schema_test_pair<schema::UInt64, 17448981058701187394u>,
+ schema_test_pair<schema::Int8 , 856492552766770219u>,
+ schema_test_pair<schema::Int16, 856492552766770216u>,
+ schema_test_pair<schema::Int32, 856492552766770214u>,
+ schema_test_pair<schema::Int64, 856492552766770210u>,
+ schema_test_pair<schema::Float32, 14974566471626763725u>,
+ schema_test_pair<schema::Float64, 14974566471626763729u>,
+ schema_test_pair<schema::SignedInteger, 17576067307413535420u>,
+ schema_test_pair<schema::UnsignedInteger, 15327325027193222152u>,
+ schema_test_pair<schema::FloatingPoint, 18201377588738489119u>,
+ schema_test_pair<schema::String, 3005399793615731798u>
+ >::check();
}
}