From 44b97c0c13c3cb05a5fed70326285b45bc7b37a6 Mon Sep 17 00:00:00 2001 From: Claudius Holeksa Date: Mon, 26 Jun 2023 15:25:29 +0200 Subject: c++,codec: Added kelsimple array and struct decoding / encoding --- tests/codec.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) (limited to 'tests/codec.cpp') diff --git a/tests/codec.cpp b/tests/codec.cpp index 09e7228..5186df3 100644 --- a/tests/codec.cpp +++ b/tests/codec.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace { namespace schema { @@ -10,6 +11,10 @@ using OneDimArray = Array; using TwoDimArray = Array; using ThreeDimArray = Array; +using TestStruct = Struct< + Member, + Member +>; } SAW_TEST("One Dimensional Array") { using namespace saw; @@ -75,4 +80,113 @@ SAW_TEST("Three Dimensional Array") { } SAW_EXPECT(sum == 124750, std::to_string(sum) + " is not 124750. Expected that data stays correct"); } + +SAW_TEST("KelSimple UInt16 write"){ + using namespace saw; + data native; + data simple; + + codec codec; + + auto& buff = simple.get_buffer(); + + for(uint16_t i = 0; i < 256; ++i){ + for(uint16_t j = 0; j < 256; ++j){ + native.set(i + j * 256); + error_or eov = codec.encode(native, simple); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + SAW_EXPECT(buff.read_composite_length() == 2, "Written incorrect size"); + SAW_EXPECT((buff.read(0) == i && buff.read(1) == j), std::string{"Incorrect values in encoding: "} + std::to_string(static_cast(buff.read(0))) + " " + std::to_string(static_cast(buff.read(1)))); + buff.read_advance(2); + + } + } +} + +SAW_TEST("KelSimple UInt32 write"){ + using namespace saw; + data native; + data simple; + + codec codec; + + auto& buff = simple.get_buffer(); + + for(uint16_t i = 0; i < 256; ++i){ + for(uint16_t j = 0; j < 256; ++j){ + native.set(i + j * 256 * 256); + error_or eov = codec.encode(native, simple); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + SAW_EXPECT(buff.read_composite_length() == 4, "Written incorrect size"); + SAW_EXPECT((buff.read(0) == i && buff.read(1) == 0 && buff.read(2) == j && buff.read(3) == 0), std::string{"Incorrect values in encoding: "} + std::to_string(static_cast(buff.read(0))) + " " + std::to_string(static_cast(buff.read(1)))); + buff.read_advance(4); + + } + } +} + +SAW_TEST("KelSimple Array write and read back"){ + using namespace saw; + data native{2,3}; + data simple; + + codec codec; + + for(std::size_t i = 0; i < 2; ++i) { + for(std::size_t j = 0; j < 3; ++j){ + native.at(i,j).set(i+2*j); + } + } + + auto eov = codec.encode(native,simple); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + for(std::size_t i = 0; i < 2; ++i) { + for(std::size_t j = 0; j < 3; ++j){ + native.at(i,j).set(0); + } + } + + eov = codec.decode(simple, native); + SAW_EXPECT(eov.is_value(), "Decoding error"); + + for(std::size_t i = 0; i < 2; ++i) { + for(std::size_t j = 0; j < 3; ++j){ + SAW_EXPECT(native.at(i,j).get() == static_cast(i+2*j), "Values incorrectly decoded"); + } + } +} + +SAW_TEST("KelSimple Struct write and read back"){ + using namespace saw; + + data native; + data simple; + + auto& tda = native.template get<"two_dim_array">(); + tda = {1,2}; + + tda.at(0,0).set(5); + tda.at(0,1).set(3); + native.template get<"number">().set(410); + + codec codec; + + auto eov = codec.encode(native, simple); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + // Reset values + native = {}; + + eov = codec.decode(simple, native); + SAW_EXPECT(eov.is_value(), "Decoding error"); + + auto& dec_tda = native.template get<"two_dim_array">(); + + SAW_EXPECT(dec_tda.at(0,0).get() == 5, "Incorrect Decoding in array 0,0"); + SAW_EXPECT(dec_tda.at(0,1).get() == 3, "Incorrect Decoding in array 0,1"); + SAW_EXPECT(native.template get<"number">().get() == 410, "Incorrect Decoding in number"); +} } -- cgit v1.2.3