Skip to content

Commit a74af23

Browse files
Merge pull request #147 from martinfantini/default_optional
Default optional
2 parents a4b748a + 313371f commit a74af23

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

src/mfast/coder/encoder_v2/fast_encoder_core.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ void fast_encoder_core::encode_field(const T &ext_ref, default_operator_tag,
470470
return;
471471
}
472472

473+
if (ext_ref.optional() && !ext_ref.present()) {
474+
pmap.set_next_bit(false);
475+
return;
476+
}
477+
473478
pmap.set_next_bit(true);
474479
if (!ext_ref.present()) {
475480
// A NULL indicates that the value is absent and the state of the previous

tests/encoder_decoder_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,23 @@ TEST_CASE("simple field group optional encoder/decoder","[field_group_optional_e
225225
REQUIRE(test_case.decoding("\xF0\x85\x82\xC0\x81\x81\x82\x8B",test_5.cref(),true));
226226
}
227227
}
228+
229+
TEST_CASE("simple optional field with default value encoder/decoder","[optional_field_with_default_value_encoder_decoder]")
230+
{
231+
fast_test_coding_case<simple12::templates_description> test_case;
232+
233+
SECTION("encode fields")
234+
{
235+
simple12::Test_6 test_6;
236+
simple12::Test_6_mref test_6_mref = test_6.mref();
237+
test_6_mref.set_field_6_2().as(1);
238+
// \xD0 : 1110 : OK
239+
// 1 : Stop Bit.
240+
// 1 : Set Template Id.
241+
// 0 : Not Set Field field_6_1
242+
// 1 : Set field_6_2
243+
REQUIRE(test_case.encoding(test_6.cref(),"\xD0\x86\x82",true));
244+
test_6_mref.set_field_6_1().as(1);
245+
REQUIRE(test_case.decoding("\xD0\x86\x82",test_6.cref(),true));
246+
}
247+
}

tests/encoder_decoder_test_v2.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,23 @@ TEST_CASE("simple field group optional encoder_v2/decoder_v2","[field_group_opti
226226
REQUIRE(test_case.decoding("\xF0\x85\x82\xC0\x81\x81\x82\x8B",test_5.cref(),true));
227227
}
228228
}
229+
230+
TEST_CASE("simple optional field with default value encoder_v2/decoder_v2","[optional_field_with_default_value_encoder_v2/decoder_v2]")
231+
{
232+
fast_test_coding_case_v2<simple12::templates_description> test_case;
233+
234+
SECTION("encode field")
235+
{
236+
simple12::Test_6 test_6;
237+
simple12::Test_6_mref test_6_mref = test_6.mref();
238+
test_6_mref.set_field_6_2().as(1);
239+
// \xD0 : 1110 : OK
240+
// 1 : Stop Bit.
241+
// 1 : Set Template Id.
242+
// 0 : Not Set Field field_6_1
243+
// 1 : Set field_6_2
244+
REQUIRE(test_case.encoding(test_6.cref(),"\xD0\x86\x82",true));
245+
test_6_mref.set_field_6_1().as(1);
246+
REQUIRE(test_case.decoding("\xD0\x86\x82",test_6.cref(),true));
247+
}
248+
}

tests/encoder_operator_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ TEST_CASE("test the encoding of fast operator default","[operator_default_encode
405405

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

408-
REQUIRE( encode_ext_cref("\xC0\x80",
408+
REQUIRE( encode_ext_cref("\x80",
409409
ext_cref<uint64_cref, default_operator_tag, optional_with_initial_value_tag>(result),
410410
PRESERVE_PREVIOUS_VALUE, &allocator ) );
411411
}

tests/simple12.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,15 @@
3232
<uInt32 name="field_5_5" id="54" presence="optional"><copy/></uInt32>
3333
</group>
3434
</template>
35+
<template name="Test_6" id="6">
36+
<uInt32 name="field_6_1" id="61" presence="optional">
37+
<default value="1"/>
38+
</uInt32>
39+
<uInt32 name="field_6_2" id="62" presence="optional">
40+
<copy/>
41+
</uInt32>
42+
<uInt32 name="field_6_3" id="63">
43+
<default value="1"/>
44+
</uInt32>
45+
</template>
3546
</templates>

0 commit comments

Comments
 (0)