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
5 changes: 5 additions & 0 deletions src/mfast/coder/encoder_v2/fast_encoder_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ void fast_encoder_core::encode_field(const T &ext_ref, default_operator_tag,
return;
}

if (ext_ref.optional() && !ext_ref.present()) {
pmap.set_next_bit(false);
return;
}

pmap.set_next_bit(true);
if (!ext_ref.present()) {
// A NULL indicates that the value is absent and the state of the previous
Expand Down
20 changes: 20 additions & 0 deletions tests/encoder_decoder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,23 @@ TEST_CASE("simple field group optional encoder/decoder","[field_group_optional_e
REQUIRE(test_case.decoding("\xF0\x85\x82\xC0\x81\x81\x82\x8B",test_5.cref(),true));
}
}

TEST_CASE("simple optional field with default value encoder/decoder","[optional_field_with_default_value_encoder_decoder]")
{
fast_test_coding_case<simple12::templates_description> test_case;

SECTION("encode fields")
{
simple12::Test_6 test_6;
simple12::Test_6_mref test_6_mref = test_6.mref();
test_6_mref.set_field_6_2().as(1);
// \xD0 : 1110 : OK
// 1 : Stop Bit.
// 1 : Set Template Id.
// 0 : Not Set Field field_6_1
// 1 : Set field_6_2
REQUIRE(test_case.encoding(test_6.cref(),"\xD0\x86\x82",true));
test_6_mref.set_field_6_1().as(1);
REQUIRE(test_case.decoding("\xD0\x86\x82",test_6.cref(),true));
}
}
20 changes: 20 additions & 0 deletions tests/encoder_decoder_test_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,23 @@ TEST_CASE("simple field group optional encoder_v2/decoder_v2","[field_group_opti
REQUIRE(test_case.decoding("\xF0\x85\x82\xC0\x81\x81\x82\x8B",test_5.cref(),true));
}
}

TEST_CASE("simple optional field with default value encoder_v2/decoder_v2","[optional_field_with_default_value_encoder_v2/decoder_v2]")
{
fast_test_coding_case_v2<simple12::templates_description> test_case;

SECTION("encode field")
{
simple12::Test_6 test_6;
simple12::Test_6_mref test_6_mref = test_6.mref();
test_6_mref.set_field_6_2().as(1);
// \xD0 : 1110 : OK
// 1 : Stop Bit.
// 1 : Set Template Id.
// 0 : Not Set Field field_6_1
// 1 : Set field_6_2
REQUIRE(test_case.encoding(test_6.cref(),"\xD0\x86\x82",true));
test_6_mref.set_field_6_1().as(1);
REQUIRE(test_case.decoding("\xD0\x86\x82",test_6.cref(),true));
}
}
2 changes: 1 addition & 1 deletion tests/encoder_operator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ TEST_CASE("test the encoding of fast operator default","[operator_default_encode

inst.prev_value().defined(false); // reset the previous value to undefined again

REQUIRE( encode_ext_cref("\xC0\x80",
REQUIRE( encode_ext_cref("\x80",
Copy link
Contributor Author

@martinfantini martinfantini Mar 7, 2025

Choose a reason for hiding this comment

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

Here the unit test is checking a optional field with the default value which is not present in the value storage. And the old value encoded in the stream was \x80 (\xC0**\x80** ), which makes no sense, send a empty value.

Now it is sending only the \x80, which is the pmap only.

ext_cref<uint64_cref, default_operator_tag, optional_with_initial_value_tag>(result),
PRESERVE_PREVIOUS_VALUE, &allocator ) );
}
Expand Down
11 changes: 11 additions & 0 deletions tests/simple12.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@
<uInt32 name="field_5_5" id="54" presence="optional"><copy/></uInt32>
</group>
</template>
<template name="Test_6" id="6">
<uInt32 name="field_6_1" id="61" presence="optional">
<default value="1"/>
</uInt32>
<uInt32 name="field_6_2" id="62" presence="optional">
<copy/>
</uInt32>
<uInt32 name="field_6_3" id="63">
<default value="1"/>
</uInt32>
</template>
</templates>