Skip to content

More unit tests #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 16, 2025
Merged
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
@@ -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,
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
@@ -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);
2 changes: 2 additions & 0 deletions src/mfast/ext_ref.h
Original file line number Diff line number Diff line change
@@ -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_;
};
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ add_executable (mfast_test
aggregate_view_test.cpp
simple_coder_test.cpp
sequence_encoder_decoder_v2.cpp
sequence_encoder_decoder_v2.cpp
sequence_encoder_decoder.cpp
scp_reset_test.cpp
template_repo_base.cpp
message_pmap_test.cpp
35 changes: 35 additions & 0 deletions tests/encoder_decoder_test.cpp
Original file line number Diff line number Diff line change
@@ -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
@@ -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<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));
}
}
49 changes: 49 additions & 0 deletions tests/group_encoder_decoder.cpp
Original file line number Diff line number Diff line change
@@ -39,4 +39,53 @@ TEST_CASE("field with group encoder/decoder","[field_without_group_encoder_decod
REQUIRE(test_case.decoding("\xE0\x81\x9F\xA8\x80\x8B",test_1.cref(),true));
}

TEST_CASE("optional group encoder/decoder","[optional_group_encoder_decoder]")
{
fast_test_coding_case<simple13::templates_description> test_case;

SECTION("No group")
{
simple13::Test_4 test_4;

REQUIRE(test_case.encoding(test_4.cref(),"\xC0\x84",true));
REQUIRE(test_case.decoding("\xC0\x84",test_4.cref(),true));
}

SECTION("With group")
{
simple13::Test_4 test_4;
simple13::Test_4_mref test_4_mref = test_4.mref();

// set group
{
auto group_1 = test_4_mref.set_group_4_1();
auto sequence_4_mref = group_1.set_sequence_4();
sequence_4_mref.resize(1);
auto element_sequence = sequence_4_mref.front();
element_sequence.set_field_4_3().as(1);
}

REQUIRE(test_case.encoding(test_4.cref(),"\xE0\x84\x80\xC0\x81",true));
REQUIRE(test_case.decoding("\xE0\x84\x80\xC0\x81",test_4.cref(),true));
}

SECTION("With group sequence and field")
{
simple13::Test_4 test_4;
simple13::Test_4_mref test_4_mref = test_4.mref();

// set group
{
auto group_1 = test_4_mref.set_group_4_1();
auto sequence_4_mref = group_1.set_sequence_4();
sequence_4_mref.resize(1);
auto element_sequence = sequence_4_mref.front();
element_sequence.set_field_4_3().as(1);

group_1.set_field_4_4().as(10);
}

REQUIRE(test_case.encoding(test_4.cref(),"\xE0\x84\xC0\xC0\x81\x8B",true));
REQUIRE(test_case.decoding("\xE0\x84\xC0\xC0\x81\x8B",test_4.cref(),true));
}
}
49 changes: 49 additions & 0 deletions tests/group_encoder_decoder_v2.cpp
Original file line number Diff line number Diff line change
@@ -39,4 +39,53 @@ TEST_CASE("field with group encoder_v2/decoder_v2","[field_without_group_encoder
REQUIRE(test_case.decoding("\xE0\x81\x9F\xA8\x80\x8B",test_1.cref(),true));
}

TEST_CASE("optional group encoder_v2/decoder_v2","[optional_group_encoder_v2_decoder_v2]")
{
fast_test_coding_case_v2<simple13::templates_description> test_case;

SECTION("No group")
{
simple13::Test_4 test_4;

REQUIRE(test_case.encoding(test_4.cref(),"\xC0\x84",true));
REQUIRE(test_case.decoding("\xC0\x84",test_4.cref(),true));
}

SECTION("With group")
{
simple13::Test_4 test_4;
simple13::Test_4_mref test_4_mref = test_4.mref();

// set group
{
auto group_1 = test_4_mref.set_group_4_1();
auto sequence_4_mref = group_1.set_sequence_4();
sequence_4_mref.resize(1);
auto element_sequence = sequence_4_mref.front();
element_sequence.set_field_4_3().as(1);
}

REQUIRE(test_case.encoding(test_4.cref(),"\xE0\x84\x80\xC0\x81",true));
REQUIRE(test_case.decoding("\xE0\x84\x80\xC0\x81",test_4.cref(),true));
}

SECTION("With group sequence and field")
{
simple13::Test_4 test_4;
simple13::Test_4_mref test_4_mref = test_4.mref();

// set group
{
auto group_1 = test_4_mref.set_group_4_1();
auto sequence_4_mref = group_1.set_sequence_4();
sequence_4_mref.resize(1);
auto element_sequence = sequence_4_mref.front();
element_sequence.set_field_4_3().as(1);

group_1.set_field_4_4().as(10);
}

REQUIRE(test_case.encoding(test_4.cref(),"\xE0\x84\xC0\xC0\x81\x8B",true));
REQUIRE(test_case.decoding("\xE0\x84\xC0\xC0\x81\x8B",test_4.cref(),true));
}
}
106 changes: 106 additions & 0 deletions tests/sequence_encoder_decoder.cpp
Original file line number Diff line number Diff line change
@@ -130,4 +130,110 @@ TEST_CASE("sequence optional none operator by length encoder/decoder","[sequence
REQUIRE(test_case.decoding("\xC0\x84\x82\xC0\xB3\x8B",test_4.cref(),true));
}

TEST_CASE("sequence inside sequence encoder/decoder","[sequence_inside_sequence_encoder_decoder]")
{
fast_test_coding_case<simple14::templates_description> test_case;

SECTION("outside sequence")
{
simple14::Test_5 test_5;
simple14::Test_5_mref test_5_mref = test_5.mref();

test_5_mref.set_field_5_2().as(1);

auto sequence_5_1_mref = test_5_mref.set_sequence_5_1();
sequence_5_1_mref.resize(1);

{
auto element_sequence = sequence_5_1_mref.front();
element_sequence.set_field_5_4().as(50);
}

REQUIRE(test_case.encoding(test_5.cref(),"\xD0\x85\x81\x81\xC0\xB3",true));
REQUIRE(test_case.decoding("\xD0\x85\x81\x81\xC0\xB3",test_5.cref(),true));
}

SECTION("inside sequence")
{
simple14::Test_5 test_5;
simple14::Test_5_mref test_5_mref = test_5.mref();

test_5_mref.set_field_5_2().as(1);

auto sequence_5_1_mref = test_5_mref.set_sequence_5_1();
sequence_5_1_mref.resize(1);

{
auto element_sequence = sequence_5_1_mref.front();
element_sequence.set_field_5_4().as(50);

auto sequence_5_2_mref = element_sequence.set_sequence_5_2();
sequence_5_2_mref.resize(1);

{
auto element_sequence_inside = sequence_5_2_mref.front();
element_sequence_inside.set_field_5_6().as(50);
}
}

REQUIRE(test_case.encoding(test_5.cref(),"\xD0\x85\x81\x81\xE0\xB3\x82\xB2",true));
REQUIRE(test_case.decoding("\xD0\x85\x81\x81\xE0\xB3\x82\xB2",test_5.cref(),true));
}
}

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

SECTION("outside sequence")
{
simple14::Test_6 test_6;
simple14::Test_6_mref test_6_mref = test_6.mref();

// set group
{
auto group_6 = test_6_mref.set_group_6();
auto sequence_6_1_mref = group_6.set_sequence_6_1();
sequence_6_1_mref.resize(1);

//set sequence
{
auto element_sequence = sequence_6_1_mref.front();
element_sequence.set_field_6_3().as(50);
}
}

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

SECTION("inside sequence")
{
simple14::Test_6 test_6;
simple14::Test_6_mref test_6_mref = test_6.mref();

// set group
{
auto group_6 = test_6_mref.set_group_6();
auto sequence_6_1_mref = group_6.set_sequence_6_1();
sequence_6_1_mref.resize(1);

//set sequence
{
auto element_sequence = sequence_6_1_mref.front();
element_sequence.set_field_6_3().as(50);

auto sequence_6_2_mref = element_sequence.set_sequence_6_2();
sequence_6_2_mref.resize(1);

{
auto element_sequence_inside = sequence_6_2_mref.front();
element_sequence_inside.set_field_6_6().as(50);
}
}
}

REQUIRE(test_case.encoding(test_6.cref(),"\xC0\x86\x81\xD0\xB2\x82\xB2",true));
REQUIRE(test_case.decoding("\xC0\x86\x81\xD0\xB2\x82\xB2",test_6.cref(),true));
}
}
108 changes: 108 additions & 0 deletions tests/sequence_encoder_decoder_v2.cpp
Original file line number Diff line number Diff line change
@@ -131,3 +131,111 @@ TEST_CASE("sequence optional none operator by length encoder_V2/decoder_v2","[se
REQUIRE(test_case.encoding(test_4.cref(),"\xC0\x84\x82\xC0\xB3\x8B",true));
REQUIRE(test_case.decoding("\xC0\x84\x82\xC0\xB3\x8B",test_4.cref(),true));
}

TEST_CASE("sequence inside sequence encoder_V2/decoder_v2","[sequence_inside_sequence_encoder_v2_decoder_v2]")
{
fast_test_coding_case_v2<simple14::templates_description> test_case;

SECTION("outside sequence")
{
simple14::Test_5 test_5;
simple14::Test_5_mref test_5_mref = test_5.mref();

test_5_mref.set_field_5_2().as(1);

auto sequence_5_1_mref = test_5_mref.set_sequence_5_1();
sequence_5_1_mref.resize(1);

{
auto element_sequence = sequence_5_1_mref.front();
element_sequence.set_field_5_4().as(50);
}

REQUIRE(test_case.encoding(test_5.cref(),"\xD0\x85\x81\x81\xC0\xB3",true));
REQUIRE(test_case.decoding("\xD0\x85\x81\x81\xC0\xB3",test_5.cref(),true));
}

SECTION("inside sequence")
{
simple14::Test_5 test_5;
simple14::Test_5_mref test_5_mref = test_5.mref();

test_5_mref.set_field_5_2().as(1);

auto sequence_5_1_mref = test_5_mref.set_sequence_5_1();
sequence_5_1_mref.resize(1);

{
auto element_sequence = sequence_5_1_mref.front();
element_sequence.set_field_5_4().as(50);

auto sequence_5_2_mref = element_sequence.set_sequence_5_2();
sequence_5_2_mref.resize(1);

{
auto element_sequence_inside = sequence_5_2_mref.front();
element_sequence_inside.set_field_5_6().as(50);
}
}

REQUIRE(test_case.encoding(test_5.cref(),"\xD0\x85\x81\x81\xE0\xB3\x82\xB2",true));
REQUIRE(test_case.decoding("\xD0\x85\x81\x81\xE0\xB3\x82\xB2",test_5.cref(),true));
}
}

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

SECTION("outside sequence")
{
simple14::Test_6 test_6;
simple14::Test_6_mref test_6_mref = test_6.mref();

// set group
{
auto group_6 = test_6_mref.set_group_6();
auto sequence_6_1_mref = group_6.set_sequence_6_1();
sequence_6_1_mref.resize(1);

//set sequence
{
auto element_sequence = sequence_6_1_mref.front();
element_sequence.set_field_6_3().as(50);
}
}

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

SECTION("inside sequence")
{
simple14::Test_6 test_6;
simple14::Test_6_mref test_6_mref = test_6.mref();

// set group
{
auto group_6 = test_6_mref.set_group_6();
auto sequence_6_1_mref = group_6.set_sequence_6_1();
sequence_6_1_mref.resize(1);

//set sequence
{
auto element_sequence = sequence_6_1_mref.front();
element_sequence.set_field_6_3().as(50);

auto sequence_6_2_mref = element_sequence.set_sequence_6_2();
sequence_6_2_mref.resize(1);

{
auto element_sequence_inside = sequence_6_2_mref.front();
element_sequence_inside.set_field_6_6().as(50);
}
}
}

REQUIRE(test_case.encoding(test_6.cref(),"\xC0\x86\x81\xD0\xB2\x82\xB2",true));
REQUIRE(test_case.decoding("\xC0\x86\x81\xD0\xB2\x82\xB2",test_6.cref(),true));
}
}
36 changes: 29 additions & 7 deletions tests/simple12.xml
Original file line number Diff line number Diff line change
@@ -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">
@@ -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">
@@ -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">
@@ -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>
16 changes: 16 additions & 0 deletions tests/simple13.xml
Original file line number Diff line number Diff line change
@@ -29,4 +29,20 @@
</group>
</group>
</template>
<template name="Test_4" id="4">
<group name="group_4_1" presence="optional">
<sequence name="sequence_4">
<length name="field_4_1" id="41">
<constant value="1"/>
</length>
<uInt32 name="field_4_2" id="42">
<constant value="0"/>
</uInt32>
<uInt32 name="field_4_3" id="43">
<copy/>
</uInt32>
</sequence>
<uInt32 name="field_4_4" id="44" presence="optional"><copy/></uInt32>
</group>
</template>
</templates>
26 changes: 26 additions & 0 deletions tests/simple14.xml
Original file line number Diff line number Diff line change
@@ -28,4 +28,30 @@
</sequence>
<uInt32 name="field_4_3" id="43" presence="optional"></uInt32>
</template>
<template name="Test_5" id="5">
<uInt32 name="field_5_1" id="51" presence="optional"><copy/></uInt32>
<uInt32 name="field_5_2" id="52"><copy/></uInt32>
<sequence name="sequence_5_1">
<length name="field_5_3" id="53"></length>
<uInt32 name="field_5_4" id="54" presence="optional"><copy/></uInt32>
<sequence name="sequence_5_2" presence="optional">
<length name="field_5_5" id="55"><copy/></length>
<uInt32 name="field_5_6" id="56"/>
</sequence>
</sequence>
</template>
<template name="Test_6" id="6">
<uInt32 name="field_6_1" id="61" presence="optional"><copy/></uInt32>
<group name="group_6">
<sequence name="sequence_6_1">
<length name="field_6_2" id="62"></length>
<uInt32 name="field_6_3" id="63"><copy/></uInt32>
<uInt32 name="field_6_4" id="64" presence="optional"><copy/></uInt32>
<sequence name="sequence_6_2" presence="optional">
<length name="field_6_5" id="65"><copy/></length>
<uInt32 name="field_6_6" id="66"/>
</sequence>
</sequence>
</group>
</template>
</templates>