summaryrefslogtreecommitdiff
path: root/tests/codec.cpp
blob: 5186df3909423b92a03364b74ef2a465f58c6aac (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
#include <forstio/test/suite.h>
#include <forstio/codec/data.h>
#include <forstio/codec/simple.h>

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">
>;
}
SAW_TEST("One Dimensional Array") {
	using namespace saw;

	data<schema::OneDimArray, encode::Native> 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("Two Dimensional Array") {
	using namespace saw;

	data<schema::TwoDimArray, encode::Native> arr{10,50u};

	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){
			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 == 124750, std::to_string(sum) + " is not 124750. Expected that data stays correct");
}

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

	data<schema::ThreeDimArray, encode::Native> arr{10,10u,5};

	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){
				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 == 124750, std::to_string(sum) + " is not 124750. Expected that data stays correct");
}

SAW_TEST("KelSimple UInt16 write"){
	using namespace saw;
	data<schema::UInt16, encode::Native> 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> 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> 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> 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");
}
}