Skip to content

Commit a4b748a

Browse files
Merge pull request #145 from martinfantini/sequence_lenght
Sequence length
2 parents 2627c36 + 7b691c6 commit a4b748a

5 files changed

Lines changed: 317 additions & 0 deletions

File tree

src/mfast/ext_ref.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "decimal_ref.h"
1111
#include "nested_message_ref.h"
1212

13+
#include <type_traits>
14+
1315
namespace mfast {
1416
template <int V> struct fast_operator_tag : std::integral_constant<int, V> {};
1517

@@ -147,6 +149,13 @@ class ext_cref<sequence_cref, LengthExtRef, ElementExtRef> {
147149
explicit ext_cref(const field_cref &other) : base_(other) {}
148150
cref_type get() const { return base_; }
149151
length_type get_length(value_storage &storage) const {
152+
if (std::is_same<typename LengthExtRef::operator_category, constant_operator_tag>::value)
153+
storage = base_.instruction()->length_instruction()->initial_value();
154+
else if (std::is_same<typename LengthExtRef::operator_category, copy_operator_tag>::value)
155+
storage = base_.instruction()->length_instruction()->prev_value();
156+
else if (std::is_same<typename LengthExtRef::operator_category, default_operator_tag>::value)
157+
storage = base_.instruction()->length_instruction()->initial_or_default_value();
158+
150159
uint32_mref length_mref(nullptr, &storage,
151160
base_.instruction()->length_instruction());
152161
length_mref.as(base_.size());
@@ -333,6 +342,13 @@ class ext_mref<sequence_mref, LengthExtRef, ElementExtRef> {
333342
cref_type get() const { return base_; }
334343
is_optional_type optional() const { return is_optional_type(); }
335344
length_type set_length(value_storage &storage) const {
345+
if (std::is_same<typename LengthExtRef::operator_category, constant_operator_tag>::value)
346+
storage = base_.instruction()->length_instruction()->initial_value();
347+
else if (std::is_same<typename LengthExtRef::operator_category, copy_operator_tag>::value)
348+
storage = base_.instruction()->length_instruction()->prev_value();
349+
else if (std::is_same<typename LengthExtRef::operator_category, default_operator_tag>::value)
350+
storage = base_.instruction()->length_instruction()->initial_or_default_value();
351+
336352
field_mref_base length_mref(nullptr, &storage,
337353
base_.instruction()->length_instruction());
338354
return length_type(length_mref);

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ FASTTYPEGEN_TARGET(simple_types10 simple10.xml)
2222
FASTTYPEGEN_TARGET(simple_types11 simple11.xml)
2323
FASTTYPEGEN_TARGET(simple_types12 simple12.xml)
2424
FASTTYPEGEN_TARGET(simple_types13 simple13.xml)
25+
FASTTYPEGEN_TARGET(simple_types14 simple14.xml)
2526

2627

2728
FASTTYPEGEN_TARGET(test_types1 test1.xml test2.xml)
@@ -64,13 +65,16 @@ add_executable (mfast_test
6465
${FASTTYPEGEN_simple_types11_OUTPUTS}
6566
${FASTTYPEGEN_simple_types12_OUTPUTS}
6667
${FASTTYPEGEN_simple_types13_OUTPUTS}
68+
${FASTTYPEGEN_simple_types14_OUTPUTS}
6769
fast_type_gen_test.cpp
6870
dictionary_builder_test.cpp
6971
json_test.cpp
7072
int_vector_test.cpp
7173
composite_type_test.cpp
7274
aggregate_view_test.cpp
7375
simple_coder_test.cpp
76+
sequence_encoder_decoder_v2.cpp
77+
sequence_encoder_decoder_v2.cpp
7478
scp_reset_test.cpp
7579
template_repo_base.cpp
7680
message_pmap_test.cpp

tests/sequence_encoder_decoder.cpp

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include "catch.hpp"
2+
#include <mfast.h>
3+
4+
#include "fast_test_coding_case.hpp"
5+
#include "byte_stream.h"
6+
7+
#include "simple14.h"
8+
9+
using namespace test::coding;
10+
11+
TEST_CASE("sequence optional copy operator by length encoder/decoder","[sequence_copy_operator_length_encoder_decoder]")
12+
{
13+
fast_test_coding_case<simple14::templates_description> test_case;
14+
simple14::Test_1 test_1;
15+
simple14::Test_1_mref test_1_mref = test_1.mref();
16+
auto sequence_1_mref = test_1_mref.set_sequence_1();
17+
sequence_1_mref.resize(1);
18+
19+
auto element_sequence = sequence_1_mref.front();
20+
element_sequence.set_field_1_2().as(50);
21+
22+
test_1_mref.set_field_1_3().as(10);
23+
24+
REQUIRE(test_case.encoding(test_1.cref(),"\xE0\x81\x82\xC0\xB3\x8B",true));
25+
REQUIRE(test_case.decoding("\xE0\x81\x82\xC0\xB3\x8B",test_1.cref(),true));
26+
}
27+
28+
TEST_CASE("sequence optional constant operator by length encoder/decoder","[sequence_constant_operator_length_encoder_decoder]")
29+
{
30+
fast_test_coding_case<simple14::templates_description> test_case;
31+
simple14::Test_2 test_2;
32+
simple14::Test_2 test_3;
33+
34+
{
35+
simple14::Test_2_mref test_2_mref = test_2.mref();
36+
37+
auto sequence_2_mref = test_2_mref.set_sequence_2();
38+
// First time hast to be initalized and the same value from the configuration xml
39+
sequence_2_mref.resize(1);
40+
41+
auto element_sequence = sequence_2_mref.front();
42+
element_sequence.set_field_2_2().as(50);
43+
44+
test_2_mref.set_field_2_3().as(10);
45+
46+
REQUIRE(test_case.encoding(test_2.cref(),"\xE0\x82\xC0\xB3\x8B",true));
47+
REQUIRE(test_case.decoding("\xE0\x82\xC0\xB3\x8B",test_2.cref(),true));
48+
}
49+
{
50+
simple14::Test_2_mref test_3_mref = test_3.mref();
51+
52+
auto sequence_3_mref = test_3_mref.set_sequence_2();
53+
sequence_3_mref.resize(2);
54+
55+
{
56+
auto element_sequence = sequence_3_mref.front();
57+
element_sequence.set_field_2_2().as(50);
58+
}
59+
60+
{
61+
auto element_sequence = sequence_3_mref.back();
62+
element_sequence.set_field_2_2().as(60);
63+
}
64+
65+
test_3_mref.set_field_2_3().as(10);
66+
67+
REQUIRE(test_case.encoding(test_3.cref(),"\xA0\x80\xC0\xBD\x8B"));
68+
}
69+
}
70+
71+
TEST_CASE("sequence optional default operator by length encoder/decoder","[sequence_default_operator_length_encoder_decoder]")
72+
{
73+
fast_test_coding_case<simple14::templates_description> test_case;
74+
simple14::Test_3 test_3;
75+
simple14::Test_3 test_4;
76+
77+
{
78+
simple14::Test_3_mref test_3_mref = test_3.mref();
79+
80+
auto sequence_3_mref = test_3_mref.set_sequence_3();
81+
// First time hast to be initalized and the same value from the configuration xml
82+
sequence_3_mref.resize(1);
83+
84+
auto element_sequence = sequence_3_mref.front();
85+
element_sequence.set_field_3_2().as(50);
86+
87+
test_3_mref.set_field_3_3().as(10);
88+
89+
REQUIRE(test_case.encoding(test_3.cref(),"\xC0\x83\xC0\xB3\x8B",true));
90+
REQUIRE(test_case.decoding("\xC0\x83\xC0\xB3\x8B",test_3.cref(),true));
91+
}
92+
93+
{
94+
simple14::Test_3_mref test_4_mref = test_4.mref();
95+
96+
auto sequence_4_mref = test_4_mref.set_sequence_3();
97+
sequence_4_mref.resize(2);
98+
99+
{
100+
auto element_sequence = sequence_4_mref.front();
101+
element_sequence.set_field_3_2().as(50);
102+
}
103+
104+
{
105+
auto element_sequence = sequence_4_mref.front();
106+
element_sequence.set_field_3_2().as(60);
107+
}
108+
109+
test_4_mref.set_field_3_3().as(10);
110+
111+
REQUIRE(test_case.encoding(test_4.cref(),"\xA0\x83\xC0\xBD\xC0\x80\x8B"));
112+
}
113+
}
114+
115+
TEST_CASE("sequence optional none operator by length encoder/decoder","[sequence_default_none_length_encoder_decoder]")
116+
{
117+
fast_test_coding_case<simple14::templates_description> test_case;
118+
simple14::Test_4 test_4;
119+
simple14::Test_4_mref test_4_mref = test_4.mref();
120+
121+
auto sequence_4_mref = test_4_mref.set_sequence_4();
122+
sequence_4_mref.resize(1);
123+
124+
auto element_sequence = sequence_4_mref.front();
125+
element_sequence.set_field_4_2().as(50);
126+
127+
test_4_mref.set_field_4_3().as(10);
128+
129+
REQUIRE(test_case.encoding(test_4.cref(),"\xC0\x84\x82\xC0\xB3\x8B",true));
130+
REQUIRE(test_case.decoding("\xC0\x84\x82\xC0\xB3\x8B",test_4.cref(),true));
131+
}
132+
133+
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include "catch.hpp"
2+
#include <mfast.h>
3+
4+
#include "fast_test_coding_case_v2.hpp"
5+
#include "byte_stream.h"
6+
7+
#include "simple14.h"
8+
9+
using namespace test::coding;
10+
11+
TEST_CASE("sequence optional copy operator by length encoder_V2/decoder_v2","[sequence_copy_operator_length_encoder_v2_decoder_v2]")
12+
{
13+
fast_test_coding_case_v2<simple14::templates_description> test_case;
14+
simple14::Test_1 test_1;
15+
simple14::Test_1_mref test_1_mref = test_1.mref();
16+
auto sequence_1_mref = test_1_mref.set_sequence_1();
17+
sequence_1_mref.resize(1);
18+
19+
auto element_sequence = sequence_1_mref.front();
20+
element_sequence.set_field_1_2().as(50);
21+
22+
test_1_mref.set_field_1_3().as(10);
23+
24+
REQUIRE(test_case.encoding(test_1.cref(),"\xE0\x81\x82\xC0\xB3\x8B",true));
25+
REQUIRE(test_case.decoding("\xE0\x81\x82\xC0\xB3\x8B",test_1.cref(),true));
26+
}
27+
28+
TEST_CASE("sequence optional constant operator by length encoder_V2/decoder_v2","[sequence_constant_operator_length_encoder_v2_decoder_v2]")
29+
{
30+
fast_test_coding_case_v2<simple14::templates_description> test_case;
31+
simple14::Test_2 test_2;
32+
simple14::Test_2 test_3;
33+
34+
{
35+
simple14::Test_2_mref test_2_mref = test_2.mref();
36+
37+
auto sequence_2_mref = test_2_mref.set_sequence_2();
38+
// First time hast to be initalized and the same value from the configuration xml
39+
sequence_2_mref.resize(1);
40+
41+
auto element_sequence = sequence_2_mref.front();
42+
element_sequence.set_field_2_2().as(50);
43+
44+
test_2_mref.set_field_2_3().as(10);
45+
46+
REQUIRE(test_case.encoding(test_2.cref(),"\xE0\x82\xC0\xB3\x8B",true));
47+
REQUIRE(test_case.decoding("\xE0\x82\xC0\xB3\x8B",test_2.cref(),true));
48+
}
49+
{
50+
simple14::Test_2_mref test_3_mref = test_3.mref();
51+
52+
auto sequence_3_mref = test_3_mref.set_sequence_2();
53+
sequence_3_mref.resize(2);
54+
55+
{
56+
auto element_sequence = sequence_3_mref.front();
57+
element_sequence.set_field_2_2().as(50);
58+
}
59+
60+
{
61+
auto element_sequence = sequence_3_mref.back();
62+
element_sequence.set_field_2_2().as(60);
63+
}
64+
65+
test_3_mref.set_field_2_3().as(10);
66+
67+
REQUIRE(test_case.encoding(test_3.cref(),"\xA0\x80\xC0\xBD\x8B"));
68+
}
69+
}
70+
71+
TEST_CASE("sequence optional default operator by length encoder_V2/decoder_v2","[sequence_default_operator_length_encoder_v2_decoder_v2]")
72+
{
73+
fast_test_coding_case_v2<simple14::templates_description> test_case;
74+
simple14::Test_3 test_3;
75+
simple14::Test_3 test_4;
76+
77+
{
78+
simple14::Test_3_mref test_3_mref = test_3.mref();
79+
80+
auto sequence_3_mref = test_3_mref.set_sequence_3();
81+
// First time hast to be initalized and the same value from the configuration xml
82+
sequence_3_mref.resize(1);
83+
84+
auto element_sequence = sequence_3_mref.front();
85+
element_sequence.set_field_3_2().as(50);
86+
87+
test_3_mref.set_field_3_3().as(10);
88+
89+
REQUIRE(test_case.encoding(test_3.cref(),"\xC0\x83\xC0\xB3\x8B",true));
90+
REQUIRE(test_case.decoding("\xC0\x83\xC0\xB3\x8B",test_3.cref(),true));
91+
}
92+
93+
{
94+
simple14::Test_3_mref test_4_mref = test_4.mref();
95+
96+
auto sequence_4_mref = test_4_mref.set_sequence_3();
97+
sequence_4_mref.resize(2);
98+
99+
{
100+
auto element_sequence = sequence_4_mref.front();
101+
element_sequence.set_field_3_2().as(50);
102+
}
103+
104+
{
105+
auto element_sequence = sequence_4_mref.front();
106+
element_sequence.set_field_3_2().as(60);
107+
}
108+
109+
test_4_mref.set_field_3_3().as(10);
110+
111+
REQUIRE(test_case.encoding(test_4.cref(),"\xA0\x83\xC0\xBD\xC0\x80\x8B"));
112+
}
113+
114+
// REQUIRE(test_case.decoding("\x80\x80\x8B", test_3.cref()));
115+
}
116+
117+
TEST_CASE("sequence optional none operator by length encoder_V2/decoder_v2","[sequence_default_none_length_encoder_v2_decoder_v2]")
118+
{
119+
fast_test_coding_case_v2<simple14::templates_description> test_case;
120+
simple14::Test_4 test_4;
121+
simple14::Test_4_mref test_4_mref = test_4.mref();
122+
123+
auto sequence_4_mref = test_4_mref.set_sequence_4();
124+
sequence_4_mref.resize(1);
125+
126+
auto element_sequence = sequence_4_mref.front();
127+
element_sequence.set_field_4_2().as(50);
128+
129+
test_4_mref.set_field_4_3().as(10);
130+
131+
REQUIRE(test_case.encoding(test_4.cref(),"\xC0\x84\x82\xC0\xB3\x8B",true));
132+
REQUIRE(test_case.decoding("\xC0\x84\x82\xC0\xB3\x8B",test_4.cref(),true));
133+
}

tests/simple14.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version=\" 1.0 \"?>
2+
<templates xmlns="http://www.fixprotocol.org/ns/fast/td/1.1">
3+
<template name="Test_1" id="1">
4+
<sequence name="sequence_1" presence="optional">
5+
<length name="field_1_1" id="12"><copy/></length>
6+
<uInt32 name="field_1_2" id="13" presence="optional"><copy/></uInt32>
7+
</sequence>
8+
<uInt32 name="field_1_3" id="14" presence="optional"></uInt32>
9+
</template>
10+
<template name="Test_2" id="2">
11+
<sequence name="sequence_2" presence="optional">
12+
<length name="field_2_1" id="21"><constant value="1"/></length>
13+
<uInt32 name="field_2_2" id="22" presence="optional"><copy/></uInt32>
14+
</sequence>
15+
<uInt32 name="field_2_3" id="23" presence="optional"></uInt32>
16+
</template>
17+
<template name="Test_3" id="3">
18+
<sequence name="sequence_3" presence="optional">
19+
<length name="field_3_1" id="31"><default value="1"/></length>
20+
<uInt32 name="field_3_2" id="32" presence="optional"><copy/></uInt32>
21+
</sequence>
22+
<uInt32 name="field_3_3" id="33" presence="optional"></uInt32>
23+
</template>
24+
<template name="Test_4" id="4">
25+
<sequence name="sequence_4" presence="optional">
26+
<length name="field_4_1" id="41"></length>
27+
<uInt32 name="field_4_2" id="42" presence="optional"><copy/></uInt32>
28+
</sequence>
29+
<uInt32 name="field_4_3" id="43" presence="optional"></uInt32>
30+
</template>
31+
</templates>

0 commit comments

Comments
 (0)