diff options
-rw-r--r-- | tests/codec.cpp | 390 |
1 files changed, 0 insertions, 390 deletions
diff --git a/tests/codec.cpp b/tests/codec.cpp deleted file mode 100644 index f3e27f5..0000000 --- a/tests/codec.cpp +++ /dev/null @@ -1,390 +0,0 @@ -#include <forstio/test/suite.h> -#include <forstio/codec/data.h> -#include <forstio/codec/simple.h> -#include <forstio/codec/interface.h> - -#include <iostream> - -namespace { -namespace schema { -using namespace saw::schema; - -using ZeroDimArray = Array<Int32,0>; -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"> ->; - -using TestUnion = Union< - Member<TwoDimArray, "two_dim_array">, - Member<UInt64, "number"> ->; - -using TestTuple = Tuple< - TwoDimArray, - UInt64 ->; - -using TestInt32Pair = Tuple< - Int32, - Int32 ->; - -using TestCalcFunction = Function<TestInt32Pair, Int32>; - -using TestInterface = Interface< - Member<TestCalcFunction, "add">, - Member<TestCalcFunction, "sub">, - Member<TestCalcFunction, "multiply"> ->; -} -SAW_TEST("One Dimensional Array") { - using namespace saw; - - data<schema::OneDimArray, encode::Native> arr{500u}; - - int bar = 0; - - for(size_t i = 0; i < arr.get_dim_size(0); ++i){ - arr.at(i).set(bar++); - } - - int sum = 0; - for(size_t i = 0; i < arr.get_dim_size(0); ++i){ - sum += arr.at(i).get(); - } - - SAW_EXPECT(sum == 124750, std::to_string(sum) + " is not 124750. Expected that data stays correct"); -} - -SAW_TEST("Two Dimensional Array") { - using namespace saw; - - data<schema::TwoDimArray, encode::Native> arr{10,50u}; - - int bar = 0; - - for(size_t i = 0; i < arr.get_dim_size(0); ++i){ - for(size_t j = 0; j < arr.get_dim_size(1); ++j){ - arr.at(i,j).set(bar++); - } - } - int sum = 0; - for(size_t i = 0; i < arr.get_dim_size(0); ++i){ - for(size_t j = 0; j < arr.get_dim_size(1); ++j){ - sum += arr.at(i,j).get(); - } - } - SAW_EXPECT(sum == 124750, std::to_string(sum) + " is not 124750. Expected that data stays correct"); -} - -SAW_TEST("Three Dimensional Array") { - using namespace saw; - - data<schema::ThreeDimArray, encode::Native> arr{10,10u,5}; - - int bar = 0; - - for(size_t i = 0; i < arr.get_dim_size(0); ++i){ - for(size_t j = 0; j < arr.get_dim_size(1); ++j){ - for(size_t k = 0; k < arr.get_dim_size(2); ++k){ - arr.at(i,j,k).set(bar++); - } - } - } - int sum = 0; - for(size_t i = 0; i < arr.get_dim_size(0); ++i){ - for(size_t j = 0; j < arr.get_dim_size(1); ++j){ - for(size_t k = 0; k < arr.get_dim_size(2); ++k){ - sum += arr.at(i,j,k).get(); - } - } - } - 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"); -} - -SAW_TEST("KelSimple Union write and read back"){ - using namespace saw; - - data<schema::TestUnion,encode::Native> native; - data<schema::TestUnion,encode::KelSimple> simple; - - native.template set<"number">(data<schema::UInt64, encode::Native>{}); - native.template get<"number">().set(410); - - codec<schema::TestUnion, 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"); - - SAW_EXPECT(native.template holds_alternative<"number">(), "Incorrect Decoding in array 0,1"); - SAW_EXPECT(native.template get<"number">().get() == 410, "Incorrect Decoding in number"); -} - -SAW_TEST("KelSimple Tuple write and read back"){ - using namespace saw; - - data<schema::TestTuple,encode::Native> native; - data<schema::TestTuple,encode::KelSimple> simple; - - auto& tda = native.template get<0>(); - tda = {1,2}; - - tda.at(0,0).set(5); - tda.at(0,1).set(3); - native.template get<1>().set(410); - - codec<schema::TestTuple, 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<0>(); - - 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<1>().get() == 410, "Incorrect Decoding in number"); -} - -SAW_TEST("KelSimple String write and read back"){ - using namespace saw; - - data<schema::String,encode::Native> native; - data<schema::String,encode::KelSimple> simple; - - std::string str = "FooBananaJoe"; - - native.set(str); - - codec<schema::String, 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"); - - SAW_EXPECT(native == str, "String should've been decoded back correctly"); -} - -SAW_TEST("Function basics"){ - using namespace saw; - - { - data<schema::TestInt32Pair, encode::Native> native; - - native.get<0>().set(5); - native.get<1>().set(40); - - auto func_add = function_factory<schema::TestCalcFunction, encode::Native>::create( - [](data<schema::TestInt32Pair, encode::Native> req){ - data<schema::Int32, encode::Native> resp; - - resp.set(req.get<0>().get() + req.get<1>().get()); - - return resp; - } - ); - - auto eov = func_add.call(std::move(native)); - SAW_EXPECT(eov.is_value(), "Returned value is an error"); - - auto& val = eov.get_value(); - SAW_EXPECT(val.get() == 45, "Sum is incorrect"); - } -} - -SAW_TEST("Interface basics"){ - using namespace saw; - - data<schema::TestInt32Pair, encode::Native> native; - - auto func_add = - [](data<schema::TestInt32Pair, encode::Native> req){ - data<schema::Int32, encode::Native> resp; - - resp.set(req.get<0>().get() + req.get<1>().get()); - - return resp; - }; - auto func_sub = - [](data<schema::TestInt32Pair, encode::Native> req){ - data<schema::Int32, encode::Native> resp; - - resp.set(req.get<0>().get() - req.get<1>().get()); - - return resp; - }; - auto func_multiply = [](data<schema::TestInt32Pair, encode::Native> req){ - data<schema::Int32, encode::Native> resp; - - resp.set(req.get<0>().get() * req.get<1>().get()); - - return resp; - }; - - auto iface = interface_factory<schema::TestInterface, encode::Native>::create(std::move(func_add), std::move(func_sub), std::move(func_multiply)); - - { - data<schema::TestInt32Pair, encode::Native> native; - - native.get<0>().set(5); - native.get<1>().set(40); - auto eov = iface.template call<"add">(std::move(native)); - SAW_EXPECT(eov.is_value(), "Returned value is an error"); - - auto& val = eov.get_value(); - SAW_EXPECT(val.get() == 45, "Sum is incorrect"); - } - { - data<schema::TestInt32Pair, encode::Native> native; - - native.get<0>().set(5); - native.get<1>().set(40); - auto eov = iface.template call<"sub">(std::move(native)); - SAW_EXPECT(eov.is_value(), "Returned value is an error"); - - auto& val = eov.get_value(); - SAW_EXPECT(val.get() == -35, "Sum is incorrect"); - } - { - data<schema::TestInt32Pair, encode::Native> native; - - native.get<0>().set(5); - native.get<1>().set(40); - auto eov = iface.template call<"multiply">(std::move(native)); - SAW_EXPECT(eov.is_value(), "Returned value is an error"); - - auto& val = eov.get_value(); - SAW_EXPECT(val.get() == 200, "Sum is incorrect"); - } -} -} |