summaryrefslogtreecommitdiff
path: root/modules/codec/tests/codec.cpp
blob: 1bec21475e71296c5389e65a5694af64cc609fd7 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <forstio/test/suite.hpp>
#include "../c++/data.hpp"
#include "../c++/id_map.hpp"
#include "../c++/simple.hpp"
#include "../c++/interface.hpp"

#include <iostream>

namespace {
namespace schema {
using namespace saw::schema;

using ZeroDimArray = Array<Int32,0>;
using OneDimArray = Array<Int32,1>;
using TwoDimArray = Array<Int32,2>;
using ThreeDimArray = Array<Int32,3>;

using TestStruct = Struct<
	Member<TwoDimArray, "two_dim_array">,
	Member<UInt64, "number">
>;

using TestUnion = Union<
	Member<TwoDimArray, "two_dim_array">,
	Member<UInt64, "number">
>;

using TestSameTypeUnion = Union<
	Member<UInt64, "one">,
	Member<UInt64, "two">
>;

using TestTuple = Tuple<
	TwoDimArray,
	UInt64
>;

using TestInt32Pair = Tuple<
	Int32,
	Int32
>;

using TestVoidReturnFunction = Function<Int32, Void>;

using TestCalcFunction = Function<TestInt32Pair, Int32>;

using TestInterface = Interface<
	Member<TestCalcFunction, "add">,
	Member<TestCalcFunction, "sub">,
	Member<TestCalcFunction, "multiply">,
	Member<TestVoidReturnFunction, "void">
>;
}
SAW_TEST("One Dimensional Array") {
	using namespace saw;

	data<schema::OneDimArray, encode::Native, storage::Default> arr{500u};

	int bar = 0;

	for(size_t i = 0; i < arr.get_dim_size(0); ++i){
		arr.at(i).set(bar++);
	}

	int sum = 0;
	for(size_t i = 0; i < arr.get_dim_size(0); ++i){
		sum += arr.at(i).get();
	}

	SAW_EXPECT(sum == 124750, std::to_string(sum) + " is not 124750. Expected that data stays correct");
}

SAW_TEST("One dim Array Default init"){
	using namespace saw;

	data<schema::OneDimArray, encode::Native, storage::Default> arr;

	SAW_EXPECT(arr.get_dim_size(0) == 0, "Dim should be size 0");
	SAW_EXPECT(arr.size() == 0, "Total size should also be zero");
}

SAW_TEST("One dimensional Array Add"){
	using namespace saw;

	data<schema::OneDimArray, encode::Native, storage::Default> arr{5u};

	int bar = 0;

	for(size_t i = 0; i < arr.get_dim_size(0); ++i){
		arr.at(i).set(bar++);
	}

	arr.add(7);

	SAW_EXPECT(arr.size() == 6u, "Array size is not 6u. Expected that data stays correct");
	SAW_EXPECT(arr.at(5u).get() == 7, "Array at 5u is not 7. Expected that data stays correct");
}

SAW_TEST("Two Dimensional Array") {
	using namespace saw;

	data<schema::TwoDimArray, encode::Native, storage::Default> arr{10,30u};

	int expected_sum = (300 * 301) / 2;

	int bar = 0;

	for(size_t i = 0; i < arr.get_dim_size(0); ++i){
		for(size_t j = 0; j < arr.get_dim_size(1); ++j){
			++bar;
			arr.at(i,j).set(bar);
		}
	}
	int sum = 0;
	for(size_t i = 0; i < arr.get_dim_size(0); ++i){
		for(size_t j = 0; j < arr.get_dim_size(1); ++j){
			sum += arr.at(i,j).get();
		}
	}
	SAW_EXPECT(sum == expected_sum, std::to_string(sum) + " is not "+ std::to_string(expected_sum) + ". Expected that data stays correct");
}

SAW_TEST("Three Dimensional Array") {
	using namespace saw;

	data<schema::ThreeDimArray, encode::Native, storage::Default> arr{10,10u,3};
	int expected_sum = (300 * 301) / 2;

	int bar = 0;

	for(size_t i = 0; i < arr.get_dim_size(0); ++i){
		for(size_t j = 0; j < arr.get_dim_size(1); ++j){
			for(size_t k = 0; k < arr.get_dim_size(2); ++k){
				++bar;
				arr.at(i,j,k).set(bar);
			}
		}
	}
	int sum = 0;
	for(size_t i = 0; i < arr.get_dim_size(0); ++i){
		for(size_t j = 0; j < arr.get_dim_size(1); ++j){
			for(size_t k = 0; k < arr.get_dim_size(2); ++k){
				sum += arr.at(i,j,k).get();
			}
		}
	}
	SAW_EXPECT(sum == expected_sum, std::to_string(sum) + " is not "+ std::to_string(expected_sum) + ". Expected that data stays correct");
}

SAW_TEST("KelSimple UInt16 write"){
	using namespace saw;
	data<schema::UInt16, encode::Native, storage::Default> native;
	data<schema::UInt16, encode::KelSimple> simple;

	codec<schema::UInt16, encode::KelSimple> codec;

	auto& buff = simple.get_buffer();

	for(uint16_t i = 0; i < 256; ++i){
		for(uint16_t j = 0; j < 256; ++j){
			native.set(i + j * 256);
			error_or<void> eov = codec.encode(native, simple);
			SAW_EXPECT(eov.is_value(), "Encoding error");

			SAW_EXPECT(buff.read_composite_length() == 2, "Written incorrect size");
			SAW_EXPECT((buff.read(0) == i && buff.read(1) == j), std::string{"Incorrect values in encoding: "} + std::to_string(static_cast<uint16_t>(buff.read(0))) + " " + std::to_string(static_cast<uint16_t>(buff.read(1))));
			buff.read_advance(2);

		}
	}
}

SAW_TEST("KelSimple UInt32 write"){
	using namespace saw;
	data<schema::UInt32, encode::Native, storage::Default> native;
	data<schema::UInt32, encode::KelSimple> simple;

	codec<schema::UInt32, encode::KelSimple> codec;

	auto& buff = simple.get_buffer();

	for(uint16_t i = 0; i < 256; ++i){
		for(uint16_t j = 0; j < 256; ++j){
			native.set(i + j * 256 * 256);
			error_or<void> eov = codec.encode(native, simple);
			SAW_EXPECT(eov.is_value(), "Encoding error");

			SAW_EXPECT(buff.read_composite_length() == 4, "Written incorrect size");
			SAW_EXPECT((buff.read(0) == i && buff.read(1) == 0 && buff.read(2) == j && buff.read(3) == 0), std::string{"Incorrect values in encoding: "} + std::to_string(static_cast<uint16_t>(buff.read(0))) + " " + std::to_string(static_cast<uint16_t>(buff.read(1))));
			buff.read_advance(4);

		}
	}
}

SAW_TEST("KelSimple Array write and read back"){
	using namespace saw;
	data<schema::TwoDimArray, encode::Native, storage::Default> native{2,3};
	data<schema::TwoDimArray, encode::KelSimple> simple;

	codec<schema::TwoDimArray, encode::KelSimple> codec;

	for(std::size_t i = 0; i < 2; ++i) {
		for(std::size_t j = 0; j < 3; ++j){
			native.at(i,j).set(i+2*j);
		}
	}

	auto eov = codec.encode(native,simple);
	SAW_EXPECT(eov.is_value(), "Encoding error");
	
	for(std::size_t i = 0; i < 2; ++i) {
		for(std::size_t j = 0; j < 3; ++j){
			native.at(i,j).set(0);
		}
	}

	eov = codec.decode(simple, native);
	SAW_EXPECT(eov.is_value(), "Decoding error");

	for(std::size_t i = 0; i < 2; ++i) {
		for(std::size_t j = 0; j < 3; ++j){
			SAW_EXPECT(native.at(i,j).get() == static_cast<int32_t>(i+2*j), "Values incorrectly decoded");
		}
	}
}

SAW_TEST("KelSimple Struct write and read back"){
	using namespace saw;

	data<schema::TestStruct,encode::Native, storage::Default> native;
	data<schema::TestStruct,encode::KelSimple> simple;

	auto& tda = native.template get<"two_dim_array">();
	tda = {1,2};

	tda.at(0,0).set(5);
	tda.at(0,1).set(3);
	native.template get<"number">().set(410);
	
	codec<schema::TestStruct, encode::KelSimple> codec;

	auto eov = codec.encode(native, simple);
	SAW_EXPECT(eov.is_value(), "Encoding error");

	// Reset values
	native = {};

	eov = codec.decode(simple, native);
	SAW_EXPECT(eov.is_value(), "Decoding error");

	auto& dec_tda = native.template get<"two_dim_array">();

	SAW_EXPECT(dec_tda.at(0,0).get() == 5, "Incorrect Decoding in array 0,0");
	SAW_EXPECT(dec_tda.at(0,1).get() == 3, "Incorrect Decoding in array 0,1");
	SAW_EXPECT(native.template get<"number">().get() == 410, "Incorrect Decoding in number");
}

SAW_TEST("Native Union same type compilation"){
	using namespace saw;

	data<schema::TestSameTypeUnion, encode::Native, storage::Default> native;

	native.template init<"two">().set(50u);

	SAW_EXPECT(!native.template holds_alternative<"one">(), "Two should be initialized");
	SAW_EXPECT(native.template holds_alternative<"two">(), "Two should be initialized");

	SAW_EXPECT(native.template get<"two">().get() == 50u, "Should be 50");
}

SAW_TEST("KelSimple Union write and read back"){
	using namespace saw;

	data<schema::TestUnion,encode::Native, storage::Default> native;
	data<schema::TestUnion,encode::KelSimple> simple;

	native.template set<"number">(data<schema::UInt64, encode::Native, storage::Default>{});
	native.template get<"number">().set(410);
	
	codec<schema::TestUnion, encode::KelSimple> codec;

	auto eov = codec.encode(native, simple);
	SAW_EXPECT(eov.is_value(), "Encoding error");

	// Reset values
	native = {};

	eov = codec.decode(simple, native);
	SAW_EXPECT(eov.is_value(), "Decoding error");

	SAW_EXPECT(native.template holds_alternative<"number">(), "Incorrect Decoding in array 0,1");
	SAW_EXPECT(native.template get<"number">().get() == 410, "Incorrect Decoding in number");
}

SAW_TEST("KelSimple Tuple write and read back"){
	using namespace saw;

	data<schema::TestTuple,encode::Native, storage::Default> native;
	data<schema::TestTuple,encode::KelSimple> simple;

	auto& tda = native.template get<0>();
	tda = {1,2};

	tda.at(0,0).set(5);
	tda.at(0,1).set(3);
	native.template get<1>().set(410);
	
	codec<schema::TestTuple, encode::KelSimple> codec;

	auto eov = codec.encode(native, simple);
	SAW_EXPECT(eov.is_value(), "Encoding error");

	// Reset values
	native = {};

	eov = codec.decode(simple, native);
	SAW_EXPECT(eov.is_value(), "Decoding error");

	auto& dec_tda = native.template get<0>();

	SAW_EXPECT(dec_tda.at(0,0).get() == 5, "Incorrect Decoding in array 0,0");
	SAW_EXPECT(dec_tda.at(0,1).get() == 3, "Incorrect Decoding in array 0,1");
	SAW_EXPECT(native.template get<1>().get() == 410, "Incorrect Decoding in number");
}

SAW_TEST("KelSimple String write and read back"){
	using namespace saw;

	data<schema::String,encode::Native, storage::Default> native;
	data<schema::String,encode::KelSimple> simple;

	std::string str = "FooBananaJoe";

	native.set(str);

	codec<schema::String, encode::KelSimple> codec;

	auto eov = codec.encode(native, simple);
	SAW_EXPECT(eov.is_value(), "Encoding error");

	// Reset values
	native = {};

	eov = codec.decode(simple, native);
	SAW_EXPECT(eov.is_value(), "Decoding error");

	SAW_EXPECT(native == str, "String should've been decoded back correctly");
}

SAW_TEST("Function basics"){
	using namespace saw;

	{
			data<schema::TestInt32Pair, encode::Native, storage::Default> native;

			native.get<0>().set(5);
			native.get<1>().set(40);

			auto func_add = function_factory<schema::TestCalcFunction, encode::Native, storage::Default>::create(
				[](data<schema::TestInt32Pair, encode::Native, storage::Default> req){
						data<schema::Int32, encode::Native, storage::Default> resp;

						resp.set(req.get<0>().get() + req.get<1>().get());

						return resp;
				}
			);

			auto eov = func_add.call(native);
			SAW_EXPECT(eov.is_value(), "Returned value is an error");

			auto& val = eov.get_value();
			SAW_EXPECT(val.get() == 45, "Sum is incorrect");
	}
}

SAW_TEST("Interface basics"){
	using namespace saw;

	data<schema::TestInt32Pair, encode::Native, storage::Default> native;

	auto func_add = 
		[](data<schema::TestInt32Pair, encode::Native, storage::Default>& req){
				data<schema::Int32, encode::Native, storage::Default> resp;

				resp.set(req.get<0>().get() + req.get<1>().get());

				return resp;
		};
	auto func_sub = 
		[](data<schema::TestInt32Pair, encode::Native, storage::Default>& req){
				data<schema::Int32, encode::Native, storage::Default> resp;

				resp.set(req.get<0>().get() - req.get<1>().get());

				return resp;
		};
	auto func_multiply = [](data<schema::TestInt32Pair, encode::Native, storage::Default>& req){
				data<schema::Int32, encode::Native, storage::Default> resp;

				resp.set(req.get<0>().get() * req.get<1>().get());

				return resp;
		};

	auto func_void = [](data<schema::Int32>& req) -> error_or<void> {
		(void) req;
		return void_t{};
	};

	auto iface = interface_factory<schema::TestInterface, encode::Native, storage::Default>::create(std::move(func_add), std::move(func_sub), std::move(func_multiply), std::move(func_void));

    {
			data<schema::TestInt32Pair, encode::Native, storage::Default> native;

			native.get<0>().set(5);
			native.get<1>().set(40);
			auto eov = iface.template call<"add">(native);
			SAW_EXPECT(eov.is_value(), "Returned value is an error");

			auto& val = eov.get_value();
			SAW_EXPECT(val.get() == 45, "Sum is incorrect");
	}
    {
			data<schema::TestInt32Pair, encode::Native, storage::Default> native;

			native.get<0>().set(5);
			native.get<1>().set(40);
			auto eov = iface.template call<"sub">(native);
			SAW_EXPECT(eov.is_value(), "Returned value is an error");

			auto& val = eov.get_value();
			SAW_EXPECT(val.get() == -35, "Sum is incorrect");
	}
    {
			data<schema::TestInt32Pair, encode::Native, storage::Default> native;

			native.get<0>().set(5);
			native.get<1>().set(40);
			auto eov = iface.template call<"multiply">(native);
			SAW_EXPECT(eov.is_value(), "Returned value is an error");

			auto& val = eov.get_value();
			SAW_EXPECT(val.get() == 200, "Sum is incorrect");
	}
}
}