Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/mfast/coder/encoder/fast_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ inline void fast_encoder_impl::visit(group_cref cref, int) {

inline void fast_encoder_impl::visit(sequence_cref cref, int) {

if (cref.instruction()->optional() && !cref.present()) {
if (cref.instruction()->length_instruction()->pmap_size() > 0)
this->current_->set_next_bit(false);
if (cref.instruction()->length_instruction()->field_operator() != operator_constant &&
cref.instruction()->length_instruction()->field_operator() != operator_default &&
cref.instruction()->length_instruction()->field_operator() != operator_copy)
strm_.encode_null();
return;
}

value_storage storage;

uint32_mref length_mref(nullptr, &storage,
Expand Down
11 changes: 11 additions & 0 deletions src/mfast/coder/encoder_v2/fast_encoder_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ inline void fast_encoder_core::encode_field(const T &ext_ref, group_type_tag) {
template <typename T>
inline void fast_encoder_core::encode_field(const T &ext_ref,
sequence_type_tag) {

if (ext_ref.get().instruction()->optional() && !ext_ref.present()) {
if (T::length_type::has_pmap_type::value)
this->current_->set_next_bit(false);
if(!std::is_same<typename T::length_type::operator_category, constant_operator_tag>::value &&
!std::is_same<typename T::length_type::operator_category, copy_operator_tag>::value &&
!std::is_same<typename T::length_type::operator_category, default_operator_tag>::value )
strm_.encode_null();
return;
}

value_storage storage;

typename T::length_type length = ext_ref.get_length(storage);
Expand Down
2 changes: 2 additions & 0 deletions src/mfast/ext_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ class ext_cref<sequence_cref, LengthExtRef, ElementExtRef> {
}
std::size_t size() const { return base_.size(); }

bool present() const { return base_.present(); }

private:
sequence_cref base_;
};
Expand Down
35 changes: 35 additions & 0 deletions tests/encoder_decoder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,38 @@ TEST_CASE("simple optional field with default value encoder/decoder","[optional_
REQUIRE(test_case.decoding("\xD0\x86\x82",test_6.cref(),true));
}
}

TEST_CASE("sequence optional with constant length encoder/decoder","[optional_sequence_with_constant_length_encoder_decoder]")
{
fast_test_coding_case<simple12::templates_description> test_case;

SECTION("encode fields without sequence")
{
simple12::Test_7 test_7;
simple12::Test_7_mref test_7_mref = test_7.mref();
test_7_mref.set_field_7_1().as(1);
REQUIRE(test_case.encoding(test_7.cref(),"\xE0\x87\x82",true));
REQUIRE(test_case.decoding("\xE0\x87\x82",test_7.cref(),true));
}

SECTION("encode fields with sequence")
{
simple12::Test_7 test_7;
simple12::Test_7_mref test_7_mref = test_7.mref();
test_7_mref.set_field_7_1().as(1);

{
auto sequence_7_mref = test_7_mref.set_sequence_7();
sequence_7_mref.resize(1);

{
auto element_sequence = sequence_7_mref.front();
element_sequence.set_field_7_4().as(1);
element_sequence.set_field_7_5().as(1);
}
}

REQUIRE(test_case.encoding(test_7.cref(),"\xF0\x87\x82\xa0\x82\x82",true));
REQUIRE(test_case.decoding("\xF0\x87\x82\xa0\x82\x82",test_7.cref(),true));
}
}
37 changes: 36 additions & 1 deletion tests/encoder_decoder_test_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST_CASE("simple field and sequence optional encoder_v2/decoder_v2","[field_seq
// 1 : Set Template Id.
// 1 : Set Field field_1_1
// 0 : XXX
REQUIRE(test_case.encoding(test_1.cref(),"\xE0\x81\x82\x81",true));
REQUIRE(test_case.encoding(test_1.cref(),"\xE0\x81\x82\x80",true));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here was an error of encoding and i got different value from the encoding v1.

REQUIRE(test_case.decoding("\xE0\x81\x82\x80",test_1.cref(),true));
}

Expand Down Expand Up @@ -246,3 +246,38 @@ TEST_CASE("simple optional field with default value encoder_v2/decoder_v2","[opt
REQUIRE(test_case.decoding("\xD0\x86\x82",test_6.cref(),true));
}
}

TEST_CASE("sequence optional with constant length encoder_v2/decoder_v2","[optional_sequence_with_constant_length_encoder_v2/decoder_v2]")
{
fast_test_coding_case_v2<simple12::templates_description> test_case;

SECTION("encode fields without sequence")
{
simple12::Test_7 test_7;
simple12::Test_7_mref test_7_mref = test_7.mref();
test_7_mref.set_field_7_1().as(1);
REQUIRE(test_case.encoding(test_7.cref(),"\xE0\x87\x82",true));
REQUIRE(test_case.decoding("\xE0\x87\x82",test_7.cref(),true));
}

SECTION("encode fields with sequence")
{
simple12::Test_7 test_7;
simple12::Test_7_mref test_7_mref = test_7.mref();
test_7_mref.set_field_7_1().as(1);

{
auto sequence_7_mref = test_7_mref.set_sequence_7();
sequence_7_mref.resize(1);

{
auto element_sequence = sequence_7_mref.front();
element_sequence.set_field_7_4().as(1);
element_sequence.set_field_7_5().as(1);
}
}

REQUIRE(test_case.encoding(test_7.cref(),"\xF0\x87\x82\xa0\x82\x82",true));
REQUIRE(test_case.decoding("\xF0\x87\x82\xa0\x82\x82",test_7.cref(),true));
}
}
36 changes: 29 additions & 7 deletions tests/simple12.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<template name="Test_1" id="1">
<uInt32 name="field_1_1" id="11" presence="optional"><copy/></uInt32>
<sequence name="sequence_1" presence="optional">
<uInt32 name="field_1_2" id="12" presence="optional"></uInt32>
<uInt32 name="field_1_3" id="13" presence="optional"></uInt32>
<length name="num_elements" id="12"/>
<uInt32 name="field_1_2" id="13" presence="optional"></uInt32>
<uInt32 name="field_1_3" id="14" presence="optional"></uInt32>
</sequence>
</template>
<template name="Test_2" id="2">
Expand All @@ -14,8 +15,9 @@
<template name="Test_3" id="3">
<uInt32 name="field_3_1" id="31"><copy/></uInt32>
<sequence name="sequence_3">
<uInt32 name="field_3_2" id="32"></uInt32>
<uInt32 name="field_3_3" id="33"></uInt32>
<length name="num_elements" id="32"/>
<uInt32 name="field_3_2" id="33"></uInt32>
<uInt32 name="field_3_3" id="34"></uInt32>
</sequence>
</template>
<template name="Test_4" id="4">
Expand All @@ -26,10 +28,11 @@
<uInt32 name="field_5_1" id="51" presence="optional"><copy/></uInt32>
<group name="test_5_group" presence="optional">
<sequence name="sequence_5">
<uInt32 name="field_5_2" id="52"></uInt32>
<uInt32 name="field_5_3" id="53"></uInt32>
<length name="num_elements" id="52"/>
<uInt32 name="field_5_2" id="53"></uInt32>
<uInt32 name="field_5_3" id="54"></uInt32>
</sequence>
<uInt32 name="field_5_5" id="54" presence="optional"><copy/></uInt32>
<uInt32 name="field_5_5" id="55" presence="optional"><copy/></uInt32>
</group>
</template>
<template name="Test_6" id="6">
Expand All @@ -43,4 +46,23 @@
<default value="1"/>
</uInt32>
</template>
<template name="Test_7" id="7">
<uInt32 name="field_7_1" id="71" presence="optional">
<copy/>
</uInt32>
<sequence name="sequence_7" presence="optional">
<length name="num_elements" id="72">
<constant value="1"/>
</length>
<uInt32 name="field_7_3" id="73">
<default value="0"/>
</uInt32>
<uInt32 name="field_7_4" id="74" presence="optional">
<delta/>
</uInt32>
<uInt32 name="field_7_5" id="75" presence="optional">
<copy/>
</uInt32>
</sequence>
</template>
</templates>