diff options
Diffstat (limited to 'tests/codec.cpp')
-rw-r--r-- | tests/codec.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
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 <forstio/test/suite.h> #include <forstio/codec/data.h> +#include <forstio/codec/simple.h> namespace { namespace schema { @@ -10,6 +11,10 @@ using OneDimArray = Array<Int32,1>; using TwoDimArray = Array<Int32,2>; using ThreeDimArray = Array<Int32,3>; +using TestStruct = Struct< + Member<TwoDimArray, "two_dim_array">, + Member<UInt64, "number"> +>; } 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<schema::UInt16, encode::Native> native; + data<schema::UInt16, encode::KelSimple> simple; + + codec<schema::UInt16, encode::KelSimple> 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<void> 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<uint16_t>(buff.read(0))) + " " + std::to_string(static_cast<uint16_t>(buff.read(1)))); + buff.read_advance(2); + + } + } +} + +SAW_TEST("KelSimple UInt32 write"){ + using namespace saw; + data<schema::UInt32, encode::Native> native; + data<schema::UInt32, encode::KelSimple> simple; + + codec<schema::UInt32, encode::KelSimple> 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<void> 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<uint16_t>(buff.read(0))) + " " + std::to_string(static_cast<uint16_t>(buff.read(1)))); + buff.read_advance(4); + + } + } +} + +SAW_TEST("KelSimple Array write and read back"){ + using namespace saw; + data<schema::TwoDimArray, encode::Native> native{2,3}; + data<schema::TwoDimArray, encode::KelSimple> simple; + + codec<schema::TwoDimArray, encode::KelSimple> 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<int32_t>(i+2*j), "Values incorrectly decoded"); + } + } +} + +SAW_TEST("KelSimple Struct write and read back"){ + using namespace saw; + + data<schema::TestStruct,encode::Native> native; + data<schema::TestStruct,encode::KelSimple> 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<schema::TestStruct, encode::KelSimple> 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"); +} } |