#include #include #include #include #include "test_scaffold.h" struct cbor_test : public test_scaffold { cbor_test() { name = __FILE__ ":1"; } virtual int run() { auto store = std::make_unique>(); gp::arena alloc{&*store->begin(), store->size()}; using some_int = gp::fixed_variant; { some_int a{16}; some_int b = 12u; b = a; gp_config::assertion(b.is_a(), "b got wrong type assigned"); } { some_int a{16u}; some_int b = a; gp_config::assertion(b.is_a(), "b got wrong type assigned"); gp_config::assertion(b.value() == 16, "b got wrong value assigned"); } { some_int a{16u}; some_int b = gp::move(a); gp_config::assertion(b.is_a(), "b got wrong type assigned"); gp_config::assertion(b.value() == 16, "b got wrong value assigned"); } { some_int a{16u}; some_int b; new(&b) some_int(a); gp_config::assertion(b.is_a(), "b got wrong type assigned"); gp_config::assertion(b.value() == 16, "b got wrong value assigned"); } { some_int a{16u}; some_int b; new(&b) some_int(gp::move(a)); gp_config::assertion(b.is_a(), "b got wrong type assigned"); gp_config::assertion(b.value() == 16, "b got wrong value assigned"); } { gp::vector vec{alloc}; vec.emplace_back(12u); vec.emplace_back(-16); gp_config::assertion(vec[0].is_a(), "vec0 got wrong type assigned"); gp_config::assertion(vec[1].is_a(), "vec1 got wrong type assigned"); } return 0; } }; append_test dummy_pg5zhr8bv(new cbor_test{}); /* TODO: REWRITE THOSE BAD BOIS { gp::cbor_value data{alloc}; auto gen = data.new_object(); gen.push_back(gp::make_pair(gp::cbor_value{uint8_t(12), alloc}, gp::cbor_value{int32_t(-98), alloc})); gen.push_back(gp::make_pair(gp::cbor_value{uint8_t(13), alloc}, gp::cbor_value{uint32_t(98), alloc})); data = gen; gp::array serialized; gp::array serialized_manual{ std::byte(0b10100010), std::byte(0b00001100), std::byte(0b00111000), std::byte(98), std::byte(0b00001101), std::byte(0b00011000), std::byte(98) }; 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(1); gp::array serialized; gp::array serialized_manual{ std::byte(1) }; 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 = bool(false); gp::array serialized; gp::array serialized_manual{ std::byte(0b11100000+20) }; 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 = bool(true); gp::array serialized; gp::array serialized_manual{ std::byte(0b11100000+21) }; 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{gp::nullopt, alloc}; gp::array serialized; gp::array serialized_manual{ std::byte(0b11100000+22) }; 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}; gp::array serialized; gp::array serialized_manual{ std::byte(0b11100000+23) }; 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(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); gp::array serialized; gp::array serialized_manual{ std::byte(0b11111010), std::byte(0b01000011), std::byte(0b00000000), std::byte(0b10000000), 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_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); for(auto a : {'h', 'e', 'l', 'l', 'o'}) str.push_back((std::byte)a); gp::cbor_value data{alloc}; data = str; gp::array serialized; gp::array serialized_manual{ 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"); } { 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"); } { 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(2, [&](){ meta.push_back(gp::make_pair(data, data)); }); data = meta; gp::array serialized; gp::array serialized_manual{ std::byte(0b10100010), 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"); } */