From 3fc64d6825096fadf3a3aab2c562ef64e9c8242d Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Tue, 20 Oct 2020 16:01:45 +0200 Subject: [PATCH] cbor works, some tests added --- include/gp/enveloppe/cbor.hpp | 19 ++- tests/cbor_test.cpp | 225 ++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+), 12 deletions(-) diff --git a/include/gp/enveloppe/cbor.hpp b/include/gp/enveloppe/cbor.hpp index 61e9f34..55d3a62 100644 --- a/include/gp/enveloppe/cbor.hpp +++ b/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*)(src.begin().data)), + uint16_t(*(src.slice_start(3).slice_end(2).cast>().begin().data)), src.begin()+3 }; } case cbor_oths::dword: { if(src.size()<5) ERROR; return { - uint32_t(*(gp::endian_wrapper*)(src.begin().data)), + uint32_t(*(src.slice_start(5).slice_end(4).cast>().begin().data)), src.begin()+5 }; } case cbor_oths::qword: { if(src.size()<9) ERROR; return { - uint64_t(*(gp::endian_wrapper*)(src.begin().data)), + uint64_t(*(src.slice_start(9).slice_end(8).cast>().begin().data)), src.begin()+9 }; } @@ -268,11 +268,6 @@ namespace gp { #undef ERROR } - template - U& value() { - - } - template 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{(ieee754_hf)(*(gp::endian_wrapper*)(src.begin().data))}, + cbor_floating_point{ieee754_hf(*(src.slice_start(3).slice_end(2).cast>().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*)(src.begin().data))}, + cbor_floating_point{float(*(src.slice_start(5).slice_end(4).cast>().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*)(src.begin().data))}, + cbor_floating_point{double(*(src.slice_start(9).slice_end(8).cast>().begin().data))}, alloc }, src.begin()+9 diff --git a/tests/cbor_test.cpp b/tests/cbor_test.cpp index b82ea84..ef1640e 100644 --- a/tests/cbor_test.cpp +++ b/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 serialized; + gp::array 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 serialized; + gp::array 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 serialized; + gp::array 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 serialized; + gp::array 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 serialized; + gp::array 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 serialized; + gp::array 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 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 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 meta{alloc}; + gp::repeat(5, [&](){ + meta.push_back(data); + }); + data = meta; + + + gp::array serialized; + gp::array 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; }