summaryrefslogtreecommitdiff
path: root/modules/codec/tests/schema.cpp
blob: 408a142e55212ac392ff1084c73263ad63324dde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <forstio/test/suite.hpp>

#include <forstio/templates.hpp>
#include "../c++/schema.hpp"
#include "../c++/schema_hash.hpp"

namespace {
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;
		constexpr 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();

		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;

using HashIface = Interface<
	Member<Function<Tuple<UInt16>, UInt32>, "foo">
>;
}

SAW_TEST("Schema Hashes"){
	using namespace saw;
	schema_hash_test_multi<
		schema_test_pair<schema::UInt8 , 958523688>,
		schema_test_pair<schema::UInt16, 3586362486>,
		schema_test_pair<schema::UInt32, 147217277>,
		schema_test_pair<schema::UInt64, 3054299356>,
		schema_test_pair<schema::Int8 , 1991167534>,
		schema_test_pair<schema::Int16, 2588715888>,
		schema_test_pair<schema::Int32, 1195998331>,
		schema_test_pair<schema::Int64, 4186165210>,
		schema_test_pair<schema::Float32, 1968068318>,
		schema_test_pair<schema::Float64, 3414484351>,
		schema_test_pair<schema::SignedInteger, 2923333106>,
		schema_test_pair<schema::UnsignedInteger, 3300532028>,
		schema_test_pair<schema::FloatingPoint, 1860828258>,
		schema_test_pair<schema::String, 1669061438>,
		schema_test_pair<schema::HashIface, 3365712860>
	>::check();
}
}