Просмотр исходного кода

cbor works, some tests added

cbor
Ludovic 'Archivist' Lagouardette 4 лет назад
Родитель
Сommit
3fc64d6825
2 измененных файлов: 232 добавлений и 12 удалений
  1. +7
    -12
      include/gp/enveloppe/cbor.hpp
  2. +225
    -0
      tests/cbor_test.cpp

+ 7
- 12
include/gp/enveloppe/cbor.hpp Просмотреть файл

@ -242,21 +242,21 @@ namespace gp {
case cbor_oths::word: {
if(src.size()<3) ERROR;
return {
uint16_t(*(gp::endian_wrapper<uint16_t, endian::big>*)(src.begin().data)),
uint16_t(*(src.slice_start(3).slice_end(2).cast<gp::endian_wrapper<uint16_t, endian::big>>().begin().data)),
src.begin()+3
};
}
case cbor_oths::dword: {
if(src.size()<5) ERROR;
return {
uint32_t(*(gp::endian_wrapper<uint32_t, endian::big>*)(src.begin().data)),
uint32_t(*(src.slice_start(5).slice_end(4).cast<gp::endian_wrapper<uint32_t, endian::big>>().begin().data)),
src.begin()+5
};
}
case cbor_oths::qword: {
if(src.size()<9) ERROR;
return {
uint64_t(*(gp::endian_wrapper<uint64_t, endian::big>*)(src.begin().data)),
uint64_t(*(src.slice_start(9).slice_end(8).cast<gp::endian_wrapper<uint64_t, endian::big>>().begin().data)),
src.begin()+9
};
}
@ -268,11 +268,6 @@ namespace gp {
#undef ERROR
}
template<typename U>
U& value() {
}
template<typename U>
bool is_a() {
if constexpr (
@ -432,7 +427,7 @@ namespace gp {
ERROR;
}
case cbor_type::tags: {
// TODO: add tag decoding methods
}
case cbor_type::oths: {
switch((cbor_oths)local) {
@ -476,7 +471,7 @@ namespace gp {
if(src.size()<3) ERROR;
return {
cbor_value{
cbor_floating_point{p">(ieee754_hf)(*(gp::endian_wrapper<ieee754_hf, endian::big>*)(src.begin().data))},
cbor_floating_point{n">ieee754_hf(*(src.slice_start(3).slice_end(2).cast<gp::endian_wrapper<ieee754_hf, endian::big>>().begin().data))},
alloc
},
src.begin()+3
@ -486,7 +481,7 @@ namespace gp {
if(src.size()<5) ERROR;
return {
cbor_value{
cbor_floating_point{float(*(gp::endian_wrapper<float, endian::big>*)(src.begin().data))},
cbor_floating_point{float(*(src.slice_start(5).slice_end(4).cast<gp::endian_wrapper<float, endian::big>>().begin().data))},
alloc
},
src.begin()+5
@ -496,7 +491,7 @@ namespace gp {
if(src.size()<9) ERROR;
return {
cbor_value{
cbor_floating_point{double(*(gp::endian_wrapper<double, endian::big>*)(src.begin().data))},
cbor_floating_point{double(*(src.slice_start(9).slice_end(8).cast<gp::endian_wrapper<double, endian::big>>().begin().data))},
alloc
},
src.begin()+9

+ 225
- 0
tests/cbor_test.cpp Просмотреть файл

@ -105,6 +105,139 @@ struct cbor_test : public test_scaffold {
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
}
{
gp::cbor_value data{alloc};
data = gp::cbor_number(128);
gp::array<std::byte, 2> serialized;
gp::array<std::byte, 2> serialized_manual{
std::byte(0b00011000),
std::byte(0b10000000)
};
auto ret_it = data.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
gp::fill(serialized,(std::byte)0);
auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
ret_it = decoded.first.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
}
{
gp::cbor_value data{alloc};
data = gp::cbor_number(1024);
gp::array<std::byte, 3> serialized;
gp::array<std::byte, 3> serialized_manual{
std::byte(0b00011001),
std::byte(0b00000100),
std::byte(0b00000000)
};
auto ret_it = data.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
gp::fill(serialized,(std::byte)0);
auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
ret_it = decoded.first.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
}
{
gp::cbor_value data{alloc};
data = gp::cbor_number(0xAABBCCDDu);
gp::array<std::byte, 5> serialized;
gp::array<std::byte, 5> serialized_manual{
std::byte(0b00011010),
std::byte(0xAA),
std::byte(0xBB),
std::byte(0xCC),
std::byte(0xDD)
};
auto ret_it = data.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
gp::fill(serialized,(std::byte)0);
auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
ret_it = decoded.first.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
}
{
gp::cbor_value data{alloc};
data = gp::cbor_number(false, 0xAABBCCDDEEFF0011ull);
gp::array<std::byte, 9> serialized;
gp::array<std::byte, 9> serialized_manual{
std::byte(0b00011011),
std::byte(0xAA),
std::byte(0xBB),
std::byte(0xCC),
std::byte(0xDD),
std::byte(0xEE),
std::byte(0xFF),
std::byte(0x00),
std::byte(0x11)
};
auto ret_it = data.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
gp::fill(serialized,(std::byte)0);
auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
ret_it = decoded.first.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
}
{
gp::cbor_value data{alloc};
data = gp::cbor_number(true, 0xAABBCCDDEEFF0011ull);
gp::array<std::byte, 9> serialized;
gp::array<std::byte, 9> serialized_manual{
std::byte(0b00111011),
std::byte(0xAA),
std::byte(0xBB),
std::byte(0xCC),
std::byte(0xDD),
std::byte(0xEE),
std::byte(0xFF),
std::byte(0x00),
std::byte(0x11)
};
auto ret_it = data.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
gp::fill(serialized,(std::byte)0);
auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
ret_it = decoded.first.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
}
{
gp::cbor_value data{alloc};
data = gp::cbor_floating_point(128.5f);
@ -131,6 +264,36 @@ struct cbor_test : public test_scaffold {
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
}
{
gp::cbor_value data{alloc};
data = gp::cbor_floating_point((double)128.5);
gp::array<std::byte, 9> serialized;
gp::array<std::byte, 9> serialized_manual{
std::byte(0b11111011),
std::byte(0b01000000),
std::byte(0b01100000),
std::byte(0b00010000),
std::byte(0b00000000),
std::byte(0b00000000),
std::byte(0b00000000),
std::byte(0b00000000),
std::byte(0b00000000)
};
auto ret_it = data.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
gp::fill(serialized,(std::byte)0);
auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
ret_it = decoded.first.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
}
{
gp::vector<std::byte> str{alloc};
str.reserve(5);
@ -162,6 +325,68 @@ struct cbor_test : public test_scaffold {
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
}
{
gp::vector<std::byte> str{alloc};
str.reserve(5);
for(auto a : {'h', 'e', 'l', 'l', 'o'})
str.push_back((std::byte)a);
gp::cbor_value data{alloc};
data = str;
gp::vector<gp::cbor_value> meta{alloc};
gp::repeat(5, [&](){
meta.push_back(data);
});
data = meta;
gp::array<std::byte, 31> serialized;
gp::array<std::byte, 31> serialized_manual{
std::byte(0b10000101),
std::byte(0b01000101),
std::byte('h'),
std::byte('e'),
std::byte('l'),
std::byte('l'),
std::byte('o'),
std::byte(0b01000101),
std::byte('h'),
std::byte('e'),
std::byte('l'),
std::byte('l'),
std::byte('o'),
std::byte(0b01000101),
std::byte('h'),
std::byte('e'),
std::byte('l'),
std::byte('l'),
std::byte('o'),
std::byte(0b01000101),
std::byte('h'),
std::byte('e'),
std::byte('l'),
std::byte('l'),
std::byte('o'),
std::byte(0b01000101),
std::byte('h'),
std::byte('e'),
std::byte('l'),
std::byte('l'),
std::byte('o')
};
auto ret_it = data.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
gp::fill(serialized,(std::byte)0);
auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
ret_it = decoded.first.encode(serialized.as_buffer());
gp_config::assertion(serialized.begin() != ret_it, "could not encode");
gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
}
return 0;
}

Загрузка…
Отмена
Сохранить