From f4480c74187a218a999de3929d00ad58d7b65adf Mon Sep 17 00:00:00 2001 From: Roberto Martin Fantini Date: Sat, 8 Mar 2025 13:48:29 +0100 Subject: [PATCH 1/4] Add check if the sequence is optional, if it is not persent encode null 0x80 and false in the presence map. --- src/mfast/coder/encoder/fast_encoder.cpp | 10 ++++++++++ src/mfast/coder/encoder_v2/fast_encoder_core.h | 11 +++++++++++ src/mfast/ext_ref.h | 2 ++ 3 files changed, 23 insertions(+) diff --git a/src/mfast/coder/encoder/fast_encoder.cpp b/src/mfast/coder/encoder/fast_encoder.cpp index 27257ac5..ca36ba86 100644 --- a/src/mfast/coder/encoder/fast_encoder.cpp +++ b/src/mfast/coder/encoder/fast_encoder.cpp @@ -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, diff --git a/src/mfast/coder/encoder_v2/fast_encoder_core.h b/src/mfast/coder/encoder_v2/fast_encoder_core.h index 345627ae..6f70a282 100644 --- a/src/mfast/coder/encoder_v2/fast_encoder_core.h +++ b/src/mfast/coder/encoder_v2/fast_encoder_core.h @@ -259,6 +259,17 @@ inline void fast_encoder_core::encode_field(const T &ext_ref, group_type_tag) { template 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::value && + !std::is_same::value && + !std::is_same::value ) + strm_.encode_null(); + return; + } + value_storage storage; typename T::length_type length = ext_ref.get_length(storage); diff --git a/src/mfast/ext_ref.h b/src/mfast/ext_ref.h index 33edb1d6..60bf81aa 100644 --- a/src/mfast/ext_ref.h +++ b/src/mfast/ext_ref.h @@ -167,6 +167,8 @@ class ext_cref { } std::size_t size() const { return base_.size(); } + bool present() const { return base_.present(); } + private: sequence_cref base_; }; From 6171f6f1746291d52fb08bbe85e80f84c3b16cf6 Mon Sep 17 00:00:00 2001 From: Roberto Martin Fantini Date: Sat, 8 Mar 2025 13:51:36 +0100 Subject: [PATCH 2/4] add unit test for the sequence case --- tests/encoder_decoder_test.cpp | 35 +++++++++++++++++++++++++++++ tests/encoder_decoder_test_v2.cpp | 37 ++++++++++++++++++++++++++++++- tests/simple12.xml | 36 ++++++++++++++++++++++++------ 3 files changed, 100 insertions(+), 8 deletions(-) diff --git a/tests/encoder_decoder_test.cpp b/tests/encoder_decoder_test.cpp index 96271e48..768b663d 100644 --- a/tests/encoder_decoder_test.cpp +++ b/tests/encoder_decoder_test.cpp @@ -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 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)); + } +} diff --git a/tests/encoder_decoder_test_v2.cpp b/tests/encoder_decoder_test_v2.cpp index 4846e84a..0aabce9d 100644 --- a/tests/encoder_decoder_test_v2.cpp +++ b/tests/encoder_decoder_test_v2.cpp @@ -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)); REQUIRE(test_case.decoding("\xE0\x81\x82\x80",test_1.cref(),true)); } @@ -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 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)); + } +} diff --git a/tests/simple12.xml b/tests/simple12.xml index 2465c03d..db69f6e7 100644 --- a/tests/simple12.xml +++ b/tests/simple12.xml @@ -3,8 +3,9 @@ + diff --git a/tests/simple14.xml b/tests/simple14.xml index 97390a38..e7f9b916 100644 --- a/tests/simple14.xml +++ b/tests/simple14.xml @@ -28,4 +28,30 @@ + +