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
12 changes: 10 additions & 2 deletions src/fast_type_gen/inl_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,16 @@ void inl_gen::visit(const mfast::sequence_field_instruction *inst,
for (std::size_t i = 0; i < inst->subinstructions().size(); ++i) {
const field_instruction *subinst = inst->subinstructions()[i];

out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)["
<< i << "]) );\n";
if (is_group_type(subinst) && subinst->optional())
{
out_ << " {\n"
<< " " << get_ext_cref_type(subinst) << " ext_cref_group((*this)[" << i << "]);\n"
<< " ext_cref_group.set_group_present(this->field_storage(" << i << ")->is_present());\n"
<< " visitor.visit(ext_cref_group);\n"
<< " }\n";
}
else
out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)[" << i << "]) );\n";
}

out_ << "}\n\n";
Expand Down
1 change: 1 addition & 0 deletions src/mfast/coder/encoder/encoder_field_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ class delta_operator : public encoder_field_operator,
cref.exponent() - bv.of_decimal.exponent_;
delta_storage.of_decimal.mantissa_ =
cref.mantissa() - bv.of_decimal.mantissa_;
delta_storage.of_decimal.present_ = cref.present();

decimal_cref delta(&delta_storage, cref.instruction());
stream << delta;
Expand Down
4 changes: 2 additions & 2 deletions src/mfast/ext_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ class ext_cref<BaseCRef, group_type_tag, Properties>
explicit ext_cref(const field_cref &base) : base_(base) {}
explicit ext_cref(const aggregate_cref &base) : base_(base) {}
cref_type get() const { return base_; }
bool present() const { return !this->optional() || group_present_; }
bool present() const { return group_present_; }

void set_group_present(bool present) { group_present_ = present; }

private:
cref_type base_;
bool group_present_ = true;
bool group_present_ = this->optional()?false:true;
};

template <typename Properties>
Expand Down
16 changes: 16 additions & 0 deletions tests/encoder_decoder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,19 @@ TEST_CASE("sequence optional with constant length encoder/decoder","[optional_se
REQUIRE(test_case.decoding("\xF0\x87\x82\xa0\x82\x82",test_7.cref(),true));
}
}

TEST_CASE("decimal optional encoder/decoder","[decimal_optional_encoder_decoder]")
{
fast_test_coding_case<simple12::templates_description> test_case;

SECTION("encode decimal value")
{
simple12::Test_8 test_8;
simple12::Test_8_mref test_8_mref = test_8.mref();
test_8_mref.set_field_8_1().as(15, 1);
test_8_mref.set_field_8_2().as(20);

REQUIRE(test_case.encoding(test_8.cref(),"\xE0\x88\x81\x8F\x94",true));
REQUIRE(test_case.decoding("\xE0\x88\x81\x8F\x94",test_8.cref(),true));
}
}
16 changes: 16 additions & 0 deletions tests/encoder_decoder_test_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,19 @@ TEST_CASE("sequence optional with constant length encoder_v2/decoder_v2","[optio
REQUIRE(test_case.decoding("\xF0\x87\x82\xa0\x82\x82",test_7.cref(),true));
}
}

TEST_CASE("decimal optional encoder_v2/decoder_v2","[decimal_optional_encoder_v2/decoder_v2]")
{
fast_test_coding_case_v2<simple12::templates_description> test_case;

SECTION("encode decimal value")
{
simple12::Test_8 test_8;
simple12::Test_8_mref test_8_mref = test_8.mref();
test_8_mref.set_field_8_1().as(15, 1);
test_8_mref.set_field_8_2().as(20);

REQUIRE(test_case.encoding(test_8.cref(),"\xE0\x88\x81\x8F\x94",true));
REQUIRE(test_case.decoding("\xE0\x88\x81\x8F\x94",test_8.cref(),true));
}
}
6 changes: 2 additions & 4 deletions tests/fast_test_coding_case_v2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class fast_test_coding_case_v2
decoder_v2_(DESC::instance())
{}

bool
encoding(const mfast::message_cref& msg_ref, const byte_stream& result, bool reset=false)
bool encoding(const mfast::message_cref& msg_ref, const byte_stream& result, bool reset=false)
{
const int buffer_size = 128;
char buffer[buffer_size];
Expand All @@ -32,8 +31,7 @@ class fast_test_coding_case_v2
return false;
}

bool
decoding(const byte_stream& bytes, const mfast::message_cref& result, bool reset=false)
bool decoding(const byte_stream& bytes, const mfast::message_cref& result, bool reset=false)
{
const char* first = bytes.data();
mfast::message_cref msg = decoder_v2_.decode(first, first+bytes.size(), reset);
Expand Down
59 changes: 59 additions & 0 deletions tests/sequence_encoder_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,62 @@ TEST_CASE("group sequence inside sequence encoder/decoder","[group_sequence_insi
REQUIRE(test_case.decoding("\xC0\x86\x81\xD0\xB2\x82\xB2",test_6.cref(),true));
}
}

TEST_CASE("sequence with optional group encoder/decoder","[sequence_optional_group_encoder_decoder]")
{
fast_test_coding_case<simple14::templates_description> test_case;

SECTION("group not present")
{
simple14::Test_7 test_7;
simple14::Test_7_mref test_7_mref = test_7.mref();

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_3().as(50);

REQUIRE(test_case.encoding(test_7.cref(),"\xC0\x87\x81\xC0\xB2",true));
REQUIRE(test_case.decoding("\xC0\x87\x81\xC0\xB2",test_7.cref(),true));
}

SECTION("group present")
{
simple14::Test_7 test_7;
simple14::Test_7_mref test_7_mref = test_7.mref();

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_3().as(50);

auto group_7 = element_sequence.set_group_7();
group_7.set_field_7_4().as(20);

REQUIRE(test_case.encoding(test_7.cref(),"\xC0\x87\x81\xE0\xB2\x94",true));
REQUIRE(test_case.decoding("\xC0\x87\x81\xE0\xB2\x94",test_7.cref(),true));
}
}

TEST_CASE("sequence with optional decimal encoder/decoder", "[sequence_with_optional_decimal_encoder_decoder]")
{
fast_test_coding_case<simple14::templates_description> test_case;

SECTION("decimal present")
{
simple14::Test_8 test_8;
simple14::Test_8_mref test_8_mref = test_8.mref();

auto sequence_8_mref = test_8_mref.set_sequence_8();
sequence_8_mref.resize(1);

auto element_sequence = sequence_8_mref.front();
element_sequence.set_field_8_2().as(50);
element_sequence.set_field_8_3().as(10, 1);

REQUIRE(test_case.encoding(test_8.cref(),"\xC0\x88\x81\xC0\xB2\x82\x8A",true));
REQUIRE(test_case.decoding("\xC0\x88\x81\xC0\xB2\x82\x8A",test_8.cref(),true));
}
}
59 changes: 59 additions & 0 deletions tests/sequence_encoder_decoder_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,62 @@ TEST_CASE("group sequence inside sequence encoder_V2/decoder_v2","[group_sequenc
REQUIRE(test_case.decoding("\xC0\x86\x81\xD0\xB2\x82\xB2",test_6.cref(),true));
}
}

TEST_CASE("sequence with optional group encoder_V2/decoder_v2","[sequence_optional_group_encoder_v2_decoder_v2]")
{
fast_test_coding_case_v2<simple14::templates_description> test_case;

SECTION("group not present")
{
simple14::Test_7 test_7;
simple14::Test_7_mref test_7_mref = test_7.mref();

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_3().as(50);

REQUIRE(test_case.encoding(test_7.cref(),"\xC0\x87\x81\xC0\xB2",true));
REQUIRE(test_case.decoding("\xC0\x87\x81\xC0\xB2",test_7.cref(),true));
}

SECTION("group present")
{
simple14::Test_7 test_7;
simple14::Test_7_mref test_7_mref = test_7.mref();

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_3().as(50);

auto group_7 = element_sequence.set_group_7();
group_7.set_field_7_4().as(20);

REQUIRE(test_case.encoding(test_7.cref(),"\xC0\x87\x81\xE0\xB2\x94",true));
REQUIRE(test_case.decoding("\xC0\x87\x81\xE0\xB2\x94",test_7.cref(),true));
}
}

TEST_CASE("sequence with optional decimal encoder_V2/decoder_v2", "[sequence_with_optional_decimal_encoder_v2_decoder_v2]")
{
fast_test_coding_case_v2<simple14::templates_description> test_case;

SECTION("decimal present")
{
simple14::Test_8 test_8;
simple14::Test_8_mref test_8_mref = test_8.mref();

auto sequence_8_mref = test_8_mref.set_sequence_8();
sequence_8_mref.resize(1);

auto element_sequence = sequence_8_mref.front();
element_sequence.set_field_8_2().as(50);
element_sequence.set_field_8_3().as(10, 1);

REQUIRE(test_case.encoding(test_8.cref(),"\xC0\x88\x81\xC0\xB2\x82\x8A",true));
REQUIRE(test_case.decoding("\xC0\x88\x81\xC0\xB2\x82\x8A",test_8.cref(),true));
}
}
4 changes: 4 additions & 0 deletions tests/simple12.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@
</uInt32>
</sequence>
</template>
<template name="Test_8" id="8">
<decimal name="field_8_1" id="81"><delta/></decimal>
<uInt32 name="field_8_2" id="82"><copy/></uInt32>
</template>
</templates>
16 changes: 16 additions & 0 deletions tests/simple14.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@
</sequence>
</group>
</template>
<template name="Test_7" id="7">
<sequence name="sequence_7">
<length name="field_7_2" id="72"></length>
<uInt32 name="field_7_3" id="73"><copy/></uInt32>
<group name="group_7" presence="optional">
<uInt32 name="field_7_4" id="74"/>
</group>
</sequence>
</template>
<template name="Test_8" id="8">
<sequence name="sequence_8">
<length name="field_8_1" id="81"></length>
<uInt32 name="field_8_2" id="82"><copy/></uInt32>
<decimal name="field_8_3" id="84" presence="optional"><delta/></decimal>
</sequence>
</template>
</templates>