summaryrefslogtreecommitdiff
path: root/modules/codec/tests/schema.cpp
blob: 77d369d76a02822fcb6deaece7efe76c7c5299e6 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <forstio/test/suite.hpp>

#include <forstio/templates.hpp>
#include "../c++/schema.hpp"
#include "../c++/schema_hash.hpp"
#include "../c++/schema_factory.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();
}

SAW_TEST("Schema Factory Compiles"){
	using namespace saw;

	auto factory = build_schema<schema::Struct<>>();

	auto str = factory
		.add<schema::UInt32, "foo">()
		.add<schema::Int16, "bar">()
		.add_maybe<schema::Int16, "bar">()
		.add_maybe<schema::String, "baz">()
	;

	using FactorySchema = decltype(str)::Schema;

	using Schema = schema::Struct<
		schema::Member<schema::UInt32, "foo">,
		schema::Member<schema::Int16, "bar">,
		schema::Member<schema::String, "baz">
	>;
	
	SAW_EXPECT(is_struct<FactorySchema>::value, "Expected a struct");
	
	schema_hash_test_multi<
		schema_test_pair<FactorySchema, 86402530>,
		schema_test_pair<Schema, 86402530>
	>::check();
}

template<typename Schema>
auto ensure_types_one(saw::schema_factory<Schema> builder){
	return builder
		.template add_maybe<schema::Array<schema::Int32>, "foo">()
		.template add_maybe<schema::Int32, "bar">()
	;
}

template<typename Schema>
auto ensure_types_two(saw::schema_factory<Schema> builder){
	return builder
		.template add_maybe<schema::Array<schema::Int32>, "foo">()
		.template add_maybe<schema::UInt64, "baz">()
	;
}

SAW_TEST("Schema Factory Multimethod"){
	using namespace saw;
	auto builder = build_schema<schema::Struct<>>();

	/**
	 * This would basically be multiple components
	 */
	auto b2 = ensure_types_one(builder);
	auto b3 = ensure_types_two(b2);

	using Schema = schema::Struct<
		schema::Member<schema::Array<schema::Int32>, "foo">,
		schema::Member<schema::Int32, "bar">,
		schema::Member<schema::UInt64, "baz">
	>;

	schema_hash_test_multi<
		schema_test_pair<decltype(b3)::Schema, 2324362429>,
		schema_test_pair<Schema, 2324362429>
	>::check();

	
}
}