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
1 change: 1 addition & 0 deletions src/mfast/aggregate_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class aggregate_cref {
this->instruction_ = other.instruction_;
this->storage_array_ = other.storage_array_;
}
bool content() const { return storage_array_->of_group.content_ != nullptr; }

class iterator
: public boost::iterator_facade<iterator, field_cref,
Expand Down
4 changes: 4 additions & 0 deletions src/mfast/coder/encoder/fast_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,12 @@ void fast_encoder_impl::visit(message_cref cref, bool force_reset) {
aggregate_cref message(cref.field_storage(0), instruction);

for (auto &&field : message)
{
if (field.present() || field.instruction()->field_operator() == operator_none)
apply_accessor(*this, field);
else if (!field.present() && field.instruction()->pmap_size())
current_pmap().set_next_bit(false);
}

pmap.commit();
}
Expand Down
17 changes: 17 additions & 0 deletions src/mfast/ext_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ class ext_cref<BaseCRef, sequence_element_tag, Properties>
aggregate_cref base_;
};

template <typename BaseCRef, typename Properties>
class ext_cref<BaseCRef, group_type_tag, Properties>
: public ext_ref_properties<group_type_tag, Properties> {
public:
typedef BaseCRef cref_type;
typedef typename cref_type::type_category type_category;
typedef group_type_tag operator_category;

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() || base_.content(); }

private:
cref_type base_;
};

template <typename Properties>
class ext_cref<nested_message_cref, group_type_tag, Properties>
: public ext_ref_properties<group_type_tag, Properties> {
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ FASTTYPEGEN_TARGET(simple_types8 simple8.xml)
FASTTYPEGEN_TARGET(simple_types9 simple9.xml)
FASTTYPEGEN_TARGET(simple_types10 simple10.xml)
FASTTYPEGEN_TARGET(simple_types11 simple11.xml)
FASTTYPEGEN_TARGET(simple_types12 simple12.xml)


FASTTYPEGEN_TARGET(test_types1 test1.xml test2.xml)
Expand All @@ -37,6 +38,8 @@ add_executable (mfast_test
decoder_operator_test.cpp
encoder_operator_test.cpp
encoder_test.cpp
encoder_decoder_test.cpp
encoder_decoder_test_v2.cpp
field_comparator_test.cpp
coder_test.cpp
value_storage_test.cpp
Expand All @@ -56,6 +59,7 @@ add_executable (mfast_test
${FASTTYPEGEN_simple_types9_OUTPUTS}
${FASTTYPEGEN_simple_types10_OUTPUTS}
${FASTTYPEGEN_simple_types11_OUTPUTS}
${FASTTYPEGEN_simple_types12_OUTPUTS}
fast_type_gen_test.cpp
dictionary_builder_test.cpp
json_test.cpp
Expand Down
269 changes: 269 additions & 0 deletions tests/encoder_decoder_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
#include "catch.hpp"
#include <mfast.h>
#include <mfast/coder/fast_encoder.h>
#include <mfast/coder/fast_decoder.h>
#include "byte_stream.h"

#include "simple12.h"

namespace
{
class fast_test_coding_case
{
public:
fast_test_coding_case()
{
encoder_.include({simple12::description()});
decoder_.include({simple12::description()});
}

bool encoding(const mfast::message_cref& msg_ref, const byte_stream& result, bool reset=false)
{
const int buffer_size = 128;
char buffer[buffer_size];

std::size_t encoded_size = encoder_.encode(msg_ref,
buffer,
buffer_size,
reset);

if (result == byte_stream(buffer, encoded_size))
return true;

std::cout << byte_stream(buffer, encoded_size);

INFO( "Got \"" << byte_stream(buffer, encoded_size) << "\" instead." );
return 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_.decode(first, first+bytes.size(), reset);

return (msg == result);
}

private:
mfast::fast_encoder encoder_;
mfast::fast_decoder decoder_;
};
}

TEST_CASE("simple field and sequence optional encoder","[field_sequence_optional_sequence_encoder]")
{
fast_test_coding_case test_case;

SECTION("encode field")
{
simple12::Test_1 test_1;
simple12::Test_1_mref test_1_mref = test_1.mref();
test_1_mref.set_field_1_1().as(1);
// \xE0: 1110 : OK
// 1 : Stop Bit
// 1 : Set Template Id.
// 1 : Set Field field_1_1
// 0 : XXX
REQUIRE(test_case.encoding(test_1.cref(),"\xE0\x81\x82\x80",true));
REQUIRE(test_case.decoding("\xE0\x81\x82\x80",test_1.cref(),true));
}

SECTION("encode sequence")
{
simple12::Test_1 test_1;
simple12::Test_1_mref test_1_mref = test_1.mref();

auto sequence_1_mref = test_1_mref.set_sequence_1();
sequence_1_mref.resize(1);

auto element_sequence = sequence_1_mref.front();
element_sequence.set_field_1_2().as(10);
element_sequence.set_field_1_3().as(50);
// \xC0: 1100 : OK
// 1 : Stop Bit
// 1 : Set Template Id.
// 0 : Set Field field_1_1
// 0 : XXX
REQUIRE(test_case.encoding(test_1.cref(),"\xC0\x81\x82\x8B\xB3",true));
REQUIRE(test_case.decoding("\xC0\x81\x82\x8B\xB3",test_1.cref(),true));
}

SECTION("encode field and sequence")
{
simple12::Test_1 test_1;
simple12::Test_1_mref test_1_mref = test_1.mref();
test_1_mref.set_field_1_1().as(1);

auto sequence_1_mref = test_1_mref.set_sequence_1();
sequence_1_mref.resize(1);

auto element_sequence = sequence_1_mref.front();
element_sequence.set_field_1_2().as(10);
element_sequence.set_field_1_3().as(50);
// \xE0: 1110 : OK
// 1 : Stop Bit
// 1 : Set Template Id.
// 1 : Set Field field_1_1
// 0 : XXX
REQUIRE(test_case.encoding(test_1.cref(),"\xE0\x81\x82\x82\x8b\xB3",true));
REQUIRE(test_case.decoding("\xE0\x81\x82\x82\x8b\xB3",test_1.cref(),true));
}
}

TEST_CASE("simple field encoder","[field_optional_encoder]")
{
fast_test_coding_case test_case;

SECTION("encode fields")
{
simple12::Test_2 test_2;
simple12::Test_2_mref test_2_mref = test_2.mref();
test_2_mref.set_field_2_1().as(1);
test_2_mref.set_field_2_2().as(2);
// \xF0: 1111 : OK
// 1 : Stop Bit
// 1 : Set Template Id.
// 1 : Set Field field_2_1
// 1 : Set Field field_2_2
REQUIRE(test_case.encoding(test_2.cref(),"\xF0\x82\x81\x82",true));
REQUIRE(test_case.decoding("\xF0\x82\x81\x82",test_2.cref(),true));
}
}

TEST_CASE("simple field and sequence encoder","[field_sequence_encoder]")
{
fast_test_coding_case test_case;

SECTION("encode fields")
{
simple12::Test_3 test_3;
simple12::Test_3_mref test_3_mref = test_3.mref();
test_3_mref.set_field_3_1().as(1);

auto sequence_3_mref = test_3_mref.set_sequence_3();
sequence_3_mref.resize(1);

auto element_sequence = sequence_3_mref.front();
element_sequence.set_field_3_2().as(10);
element_sequence.set_field_3_3().as(50);
// \xE0: 1110 : OK
// 1 : Stop Bit
// 1 : Set Template Id.
// 1 : Set Field field_3_1
// 0 : XXX
REQUIRE(test_case.encoding(test_3.cref(),"\xE0\x83\x81\x81\x8A\xB2",true));
REQUIRE(test_case.decoding("\xE0\x83\x81\x81\x8A\xB2",test_3.cref(),true));
}
}

TEST_CASE("simple field optional encoder","[field_optional_encoder]")
{
fast_test_coding_case test_case;

SECTION("encode fields")
{
simple12::Test_4 test_4;
simple12::Test_4_mref test_4_mref = test_4.mref();
test_4_mref.set_field_4_1().as(1);
test_4_mref.set_field_4_2().as(2);
// \xF0 : 1111 : OK
// 1 : Stop Bit
// 1 : Set Template Id.
// 1 : Set Field field_4_1
// 1 : Set Field field_4_2
REQUIRE(test_case.encoding(test_4.cref(),"\xF0\x84\x82\x83",true));
REQUIRE(test_case.decoding("\xF0\x84\x82\x83",test_4.cref(),true));
}

SECTION("encode first optional field")
{
simple12::Test_4 test_4;
simple12::Test_4_mref test_4_mref = test_4.mref();
test_4_mref.set_field_4_1().as(1);
// \xE0 : 1110 : OK
// 1 : Stop Bit.
// 1 : Set Template Id.
// 1 : Set Field field_4_1
// 0 : Set Field field_4_2
REQUIRE(test_case.encoding(test_4.cref(),"\xE0\x84\x82", true));
REQUIRE(test_case.decoding("\xE0\x84\x82",test_4.cref(),true));
}

SECTION("encode second optional field")
{
simple12::Test_4 test_4;
simple12::Test_4_mref test_4_mref = test_4.mref();
test_4_mref.set_field_4_2().as(2);
// \xD0 : 1110 : OK
// 1 : Stop Bit.
// 1 : Set Template Id.
// 0 : Set Field field_4_1
// 1 : Set Field field_4_2
REQUIRE(test_case.encoding(test_4.cref(),"\xD0\x84\x83",true));
REQUIRE(test_case.decoding("\xD0\x84\x83",test_4.cref(),true));
}
}

TEST_CASE("simple field group optional encoder","[field_group_optional_encoder]")
{
fast_test_coding_case test_case;

SECTION("encode field")
{
simple12::Test_5 test_5;
simple12::Test_5_mref test_5_mref = test_5.mref();
test_5_mref.set_field_5_1().as(1);
// \xE0 : 1110 : OK
// 1 : Stop Bit.
// 1 : Set Template Id.
// 1 : Set Field field_5_1
// 0 : Not Set test_5_group
REQUIRE(test_case.encoding(test_5.cref(),"\xE0\x85\x82",true));
REQUIRE(test_case.decoding("\xE0\x85\x82",test_5.cref(),true));
}

SECTION("encode group")
{
simple12::Test_5 test_5;
simple12::Test_5_mref test_5_mref = test_5.mref();
auto group_test_5_group = test_5_mref.set_test_5_group();
auto group_test_5_sequence = group_test_5_group.set_sequence_5();
group_test_5_sequence.resize(1);
{
auto element_sequence = group_test_5_sequence.front();
element_sequence.set_field_5_2().as(1);
element_sequence.set_field_5_3().as(2);
}
group_test_5_group.set_field_5_5().as(10);
// \xD0 : 1101 OK
// 1 : Stop Bit.
// 1 : Set Template Id.
// 0 : Not Set Field field_5_1
// 1 : Set Group test_5_group
REQUIRE(test_case.encoding(test_5.cref(),"\xD0\x85\xC0\x81\x81\x82\x8B",true));
REQUIRE(test_case.decoding("\xD0\x85\xC0\x81\x81\x82\x8B",test_5.cref(),true));
}

SECTION("encode field and group")
{
simple12::Test_5 test_5;
simple12::Test_5_mref test_5_mref = test_5.mref();
test_5_mref.set_field_5_1().as(1);
auto group_test_5_group = test_5_mref.set_test_5_group();
auto group_test_5_sequence = group_test_5_group.set_sequence_5();
group_test_5_sequence.resize(1);
{
auto element_sequence = group_test_5_sequence.front();
element_sequence.set_field_5_2().as(1);
element_sequence.set_field_5_3().as(2);
}
group_test_5_group.set_field_5_5().as(10);
// \xF0 : 1111 : OK
// 1 : Stop Bit.
// 1 : Set Template Id.
// 1 : Set Field field_5_1
// 1 : Set test_5_group
REQUIRE(test_case.encoding(test_5.cref(),"\xF0\x85\x82\xC0\x81\x81\x82\x8B",true));
REQUIRE(test_case.decoding("\xF0\x85\x82\xC0\x81\x81\x82\x8B",test_5.cref(),true));
}
}
Loading