-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathsimple_coder_test.cpp
More file actions
325 lines (247 loc) · 9.93 KB
/
simple_coder_test.cpp
File metadata and controls
325 lines (247 loc) · 9.93 KB
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
// Copyright (c) 2013, 2014, Huang-Ming Huang, Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
//
// mFAST is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mFAST is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with mFast. If not, see <http://www.gnu.org/licenses/>.
//
#include <catch2/catch_test_macros.hpp>
#include <mfast.h>
#include <mfast/field_comparator.h>
#include <mfast/coder/fast_encoder_v2.h>
#include <mfast/coder/fast_decoder_v2.h>
#include <cstring>
#include <stdexcept>
#include "simple1.h"
#include "simple2.h"
#include "simple3.h"
#include "simple4.h"
#include "simple5.h"
#include "simple6.h"
#include "simple7.h"
#include "simple8.h"
#include "simple9.h"
#include "simple10.h"
#include "byte_stream.h"
#include "debug_allocator.h"
using namespace mfast;
template <typename DESC>
class fast_coding_test_case
{
public:
fast_coding_test_case()
: encoder_(&alloc_, DESC::instance())
, decoder_(&alloc_, DESC::instance())
{
}
bool
encoding(const message_cref& msg_ref, const byte_stream& result, bool reset=false)
{
const int buffer_size = 128;
char buffer[buffer_size];
std::size_t encoded_size = encoder_.encode(msg_ref,
buffer,
buffer_size,
reset);
if (result == byte_stream(buffer, encoded_size))
return true;
INFO("Got \"" << byte_stream(buffer, encoded_size) << "\" instead.");
return false;
}
bool
decoding(const byte_stream& bytes, const message_cref& result, bool reset=false)
{
const char* first = bytes.data();
message_cref msg = decoder_.decode(first, first+bytes.size(), reset);
if (msg == result)
return true;
return false;
}
private:
debug_allocator alloc_;
mfast::fast_encoder_v2 encoder_;
mfast::fast_decoder_v2<0> decoder_;
};
TEST_CASE("test fast coder v2 for a simple template","[simple_coder_test]")
{
fast_coding_test_case<simple1::templates_description> test_case;
debug_allocator alloc;
simple1::Test msg(&alloc);
simple1::Test_mref msg_ref = msg.mref();
msg_ref.set_field1().as(1);
msg_ref.set_field2().as(2);
msg_ref.set_field3().as(3);
REQUIRE(test_case.encoding(msg_ref,"\xB8\x81\x82\x83"));
REQUIRE(test_case.decoding("\xB8\x81\x82\x83", msg_ref));
}
TEST_CASE("test fast coder v2 for a template with group","[group_coder_test]")
{
fast_coding_test_case<simple2::templates_description> test_case;
debug_allocator alloc;
simple2::Test msg(&alloc);
simple2::Test_mref msg_ref = msg.mref();
msg_ref.set_field1().as(1);
msg_ref.set_group1().set_field2().as(2);
msg_ref.set_group1().set_field3().as(3);
REQUIRE(test_case.encoding(msg_ref, "\xB0\x81\xE0\x82\x83"));
REQUIRE(test_case.decoding("\xB0\x81\xE0\x82\x83", msg_ref));
// mfast::fast_decoder_v2< boost::mpl::vector<simple2::templates_description*> > decoder;
// const char* strm = "\xB0\x81\xE0\x82\x83";
//
// simple2::Test_cref ref(decoder.decode(strm, strm +5));
// REQUIRE_EQUAL(ref.get_field1().value(), 1);
// REQUIRE(ref.get_group1().present());
// REQUIRE_EQUAL(ref.get_group1().get_field2().value(), 2);
// REQUIRE_EQUAL(ref.get_group1().get_field3().value(), 3);
}
TEST_CASE("test fast coder v2 for a template with sequence","[sequence_coder_test]")
{
fast_coding_test_case<simple3::templates_description> test_case;
debug_allocator alloc;
simple3::Test msg(&alloc);
simple3::Test_mref msg_ref = msg.mref();
msg_ref.set_field1().as(1);
simple3::Test_mref::sequence1_mref seq(msg_ref.set_sequence1());
seq.resize(2);
seq[0].set_field2().as(2);
seq[0].set_field3().as(3);
seq[1].set_field2().as(0);
seq[1].set_field3().as(1);
REQUIRE(test_case.encoding(msg_ref,
// pmap | f1 | s1 len| elem1 pmap | f2 | f3 | elem2 pmap | f2 | f3 |
// A0 81 83 E0 82 83 E0 80 81
"\xA0\x81\x83\xE0\x82\x83\xE0\x80\x81"
));
REQUIRE(test_case.decoding("\xA0\x81\x83\xE0\x82\x83\xE0\x80\x81", msg_ref));
}
TEST_CASE("test fast coder v2 for a template with static templateref","[static_templateref_coder_test]")
{
fast_coding_test_case<simple4::templates_description> test_case;
debug_allocator alloc;
simple4::Test msg(&alloc);
simple4::Test_mref msg_ref = msg.mref();
msg_ref.set_field1().as(1);
msg_ref.set_field2().as(2);
msg_ref.set_field3().as(3);
// pmap | template id | field1 | field2 | field 3 |
// F8 82 81 82 83
REQUIRE(test_case.encoding(msg_ref,"\xF8\x82\x81\x82\x83"));
REQUIRE(test_case.decoding("\xF8\x82\x81\x82\x83", msg_ref));
}
TEST_CASE("test fast coder v2 for a template with dynamic templateref","[dynamic_templateref_coder_test]")
{
fast_coding_test_case<simple5::templates_description> test_case;
debug_allocator alloc;
simple5::Test msg(&alloc);
simple5::Test_mref msg_ref = msg.mref();
msg_ref.set_field1().as(1);
nested_message_mref nested(msg_ref.set_nested());
simple5::Nested_mref target = nested.as<simple5::Nested>();
target.set_field2().as(2);
target.set_field3().as(3);
REQUIRE(test_case.encoding(msg_ref,"\xE0\x82\x81\xF0\x81\x82\x83"));
REQUIRE(test_case.decoding("\xE0\x82\x81\xF0\x81\x82\x83", msg_ref));
}
TEST_CASE("test fast coder v2 for a template with manual reset","[manual_reset_test]")
{
fast_coding_test_case<simple6::templates_description> test_case;
debug_allocator alloc;
simple6::Test msg(&alloc);
simple6::Test_mref msg_ref = msg.mref();
msg_ref.set_field1().as(1);
msg_ref.set_field2().as(2);
msg_ref.set_field3().as(3);
REQUIRE(test_case.encoding(msg_ref, "\xB8\x81\x82\x83", false));
REQUIRE(test_case.decoding("\xB8\x81\x82\x83", msg_ref, false));
// message not changed
REQUIRE(test_case.encoding(msg_ref, "\x80", false));
REQUIRE(test_case.decoding("\x80", msg_ref, false));
msg_ref.set_field1().as(11);
msg_ref.set_field2().as(12);
msg_ref.set_field3().as(13);
REQUIRE(!msg_ref.get_field1().absent());
REQUIRE(msg_ref.get_field1().is_initial_value());
REQUIRE(msg_ref.get_field2().is_initial_value());
REQUIRE(msg_ref.get_field3().is_initial_value());
// encoding with reset, all values are initial
REQUIRE(test_case.encoding(msg_ref, "\x80", true));
REQUIRE(test_case.decoding("\x80", msg_ref, true));
}
TEST_CASE("test fast coder v2 for a template with auto rest","[auto_reset_coder_test]")
{
fast_coding_test_case<simple7::templates_description> test_case;
debug_allocator alloc;
simple7::Test msg(&alloc);
simple7::Test_mref msg_ref = msg.mref();
msg_ref.set_field1().as(1);
msg_ref.set_field2().as(2);
msg_ref.set_field3().as(3);
// REQUIRE(test_case.encoding(msg_ref, "\xB8\x81\x82\x83"));
REQUIRE(test_case.decoding("\xB8\x81\x82\x83", msg_ref));
msg_ref.set_field1().as(11);
msg_ref.set_field2().as(12);
msg_ref.set_field3().as(13);
// encoding with reset, all values are initial
REQUIRE(test_case.encoding(msg_ref, "\x80"));
REQUIRE(test_case.decoding("\x80", msg_ref));
REQUIRE(test_case.encoding(msg_ref, "\x80"));
REQUIRE(test_case.decoding("\x80", msg_ref));
}
TEST_CASE("test fast coder v2 for a template with zero segment size", "[segment_pmap_size_zero_coder_test]")
{
fast_coding_test_case<simple8::templates_description> test_case;
debug_allocator alloc;
simple8::Test msg(&alloc);
simple8::Test_mref msg_ref = msg.mref();
msg_ref.set_field1().as(1);
msg_ref.set_field2().as(2);
REQUIRE(test_case.encoding(msg_ref,"\x80\x81\x82"));
REQUIRE(test_case.decoding("\x80\x81\x82", msg_ref));
}
TEST_CASE("test fast coder v2 for a simple template with absent optional fields in a sequence", "[absent_optional_fields_coder_test]")
{
fast_coding_test_case<simple9::templates_description> test_case;
debug_allocator alloc;
simple9::Test msg(&alloc);
simple9::Test_mref msg_ref = msg.mref();
msg_ref.set_field1().as(1);
auto sequence1 = msg_ref.set_sequence1();
sequence1.resize(2);
sequence1[0].set_field3().as(3);
sequence1[1].set_field2().as(1);
REQUIRE(test_case.encoding(msg_ref,"\xA0\x81\x83\x80\x84\x82\x80"));
REQUIRE(test_case.decoding("\xA0\x81\x83\x80\x84\x82\x80", msg_ref));
}
TEST_CASE("test fast decoder for a simple template with constant len in a sequence", "[constant_sequence_length_test]")
{
fast_coding_test_case<simple10::templates_description> test_case;
debug_allocator alloc;
simple10::Test msg(&alloc);
simple10::Test_mref msg_ref = msg.mref();
simple10::Test_mref::sequence1_mref seq(msg_ref.set_sequence1());
seq.resize(1);
seq[0].set_field1().as(10);
// The value of a constant field is never transferred.
// ...
// A field will not occupy any bit in the presence map if it is mandatory and has the constant operator.
// ...
// The default, copy, and increment operators have the following presence map and NULL utilization:
// - Mandatory integer, decimal, string and byte vector fields – one bit. If set, the value appears in the stream.
REQUIRE(test_case.encoding(msg_ref,
// pmap | elem1 pmap | f1 |
// 80 C0 8A
"\x80\xC0\x8A"
));
REQUIRE(test_case.decoding("\x80\xC0\x8A", msg_ref, true));
}