Browse Source

cbor current tests work

channel
Ludovic 'Archivist' Lagouardette 3 years ago
parent
commit
460ff45767
2 changed files with 32 additions and 8 deletions
  1. +27
    -5
      include/gp/ipc/envelope/cbor.hpp
  2. +5
    -3
      tests/cbor_test.cpp

+ 27
- 5
include/gp/ipc/envelope/cbor.hpp View File

@ -8,7 +8,6 @@
#include <gp/containers/vector.hpp> #include <gp/containers/vector.hpp>
#include <concepts> #include <concepts>
#include <string>
// TODO: Rewrite in a more ORM-like way // TODO: Rewrite in a more ORM-like way
// TODO: Implement some bignum for support // TODO: Implement some bignum for support
@ -352,13 +351,12 @@ namespace gp {
if(!state.size()) return {nullopt, state}; if(!state.size()) return {nullopt, state};
auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5); auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5);
switch(type) { switch(type) {
case cbor_type::oths: case cbor_type::oths:
{ {
const auto[value, new_state] = pull_arbitrary_integer_from_cbor(state); const auto[value, new_state] = pull_arbitrary_integer_from_cbor(state);
if(!value.has_value()) break; if(!value.has_value()) break;
if(new_state.size()<value.value()) break;
gp::vector<char> return_value{alloc};
if(value.value() == 22) if(value.value() == 22)
{ {
return {optional(nullptr), parsing_state(new_state.begin()+1, new_state.end())}; return {optional(nullptr), parsing_state(new_state.begin()+1, new_state.end())};
@ -370,6 +368,32 @@ namespace gp {
return {nullopt, state}; return {nullopt, state};
} }
template<>
inline gp::pair<gp::optional<bool>, parsing_state> read_cbor<bool>(parsing_state state, gp::allocator& alloc) {
if(!state.size()) return {nullopt, state};
auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5);
switch(type) {
case cbor_type::oths:
{
const auto[value, new_state] = pull_arbitrary_integer_from_cbor(state);
if(!value.has_value()) break;
if(value.value() == 20)
{
return {false, parsing_state(new_state.begin()+1, new_state.end())};
}
else if(value.value() == 21)
{
return {true, parsing_state(new_state.begin()+1, new_state.end())};
}
break;
}
default: break;
}
return {nullopt, state};
}
template<> template<>
inline gp::pair<gp::optional<cbor_undefined>, parsing_state> read_cbor<cbor_undefined>(parsing_state state, gp::allocator& alloc) { inline gp::pair<gp::optional<cbor_undefined>, parsing_state> read_cbor<cbor_undefined>(parsing_state state, gp::allocator& alloc) {
if(!state.size()) return {nullopt, state}; if(!state.size()) return {nullopt, state};
@ -380,8 +404,6 @@ namespace gp {
{ {
const auto[value, new_state] = pull_arbitrary_integer_from_cbor(state); const auto[value, new_state] = pull_arbitrary_integer_from_cbor(state);
if(!value.has_value()) break; if(!value.has_value()) break;
if(new_state.size()<value.value()) break;
gp::vector<char> return_value{alloc};
if(value.value() == 23) if(value.value() == 23)
{ {
return {optional(cbor_undefined{}), parsing_state(new_state.begin()+1, new_state.end())}; return {optional(cbor_undefined{}), parsing_state(new_state.begin()+1, new_state.end())};

+ 5
- 3
tests/cbor_test.cpp View File

@ -384,7 +384,7 @@ struct cbor_test12 : public generic_cbor_test {
auto [value, state] = gp::read_cbor<std::nullptr_t>(serialized.as_buffer(), alloc); auto [value, state] = gp::read_cbor<std::nullptr_t>(serialized.as_buffer(), alloc);
gp_config::assertion(value.has_value(), "could not encode");
gp_config::assertion(value.has_value(), "could not decode");
gp_config::assertion(value.value() == nullptr, "data did not decode correctly"); gp_config::assertion(value.value() == nullptr, "data did not decode correctly");
} }
@ -461,12 +461,14 @@ struct cbor_test15 : public generic_cbor_test {
{ {
gp::vector<char> serialized(alloc); gp::vector<char> serialized(alloc);
gp::array<char, 1> serialized_manual{ gp::array<char, 1> serialized_manual{
char(0b11100000+24)
char(0b11100000+23)
}; };
gp::push_as_cbor(serialized, gp::cbor_undefined{}); gp::push_as_cbor(serialized, gp::cbor_undefined{});
gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode"); gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
log_segment("value", std::to_string((uint8_t)serialized[0]).c_str());
gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly"); gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly");
auto [value, state] = gp::read_cbor<gp::cbor_undefined>(serialized.as_buffer(), alloc); auto [value, state] = gp::read_cbor<gp::cbor_undefined>(serialized.as_buffer(), alloc);
@ -505,7 +507,7 @@ struct cbor_test16 : public generic_cbor_test {
gp::push_as_cbor(serialized, str.as_buffer()); gp::push_as_cbor(serialized, str.as_buffer());
gp_config::assertion(serialized.size() != serialized_manual.size(), "could not encode");
gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
for(size_t idx = 0; idx < serialized_manual.size(); idx++) { for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly"); gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
} }

Loading…
Cancel
Save