summaryrefslogtreecommitdiff
path: root/modules/codec/tests
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2023-12-05 14:41:24 +0100
committerClaudius "keldu" Holeksa <mail@keldu.de>2023-12-05 14:41:24 +0100
commita8900f0b26271278b8c3b574349189f90ff9f2aa (patch)
treed71d0a2d996980fb491c6715050d2a2bf61cde89 /modules/codec/tests
parent2db0d6a52b91a6a05301332369152f7d6e055ee9 (diff)
codec: Fixed tests for codec
Diffstat (limited to 'modules/codec/tests')
-rw-r--r--modules/codec/tests/SConscript2
-rw-r--r--modules/codec/tests/codec.cpp390
2 files changed, 392 insertions, 0 deletions
diff --git a/modules/codec/tests/SConscript b/modules/codec/tests/SConscript
index ba09372..608c2b7 100644
--- a/modules/codec/tests/SConscript
+++ b/modules/codec/tests/SConscript
@@ -12,6 +12,8 @@ dir_path = Dir('.').abspath
# Environment for base library
test_cases_env = env.Clone();
+test_cases_env.Append(LIBS=['forstio-test']);
+
test_cases_env.sources = sorted(glob.glob(dir_path + "/*.cpp"))
test_cases_env.headers = sorted(glob.glob(dir_path + "/*.h"))
diff --git a/modules/codec/tests/codec.cpp b/modules/codec/tests/codec.cpp
new file mode 100644
index 0000000..2cf4587
--- /dev/null
+++ b/modules/codec/tests/codec.cpp
@@ -0,0 +1,390 @@
+#include <forstio/test/suite.h>
+#include "../c++/data.h"
+#include "../c++/simple.h"
+#include "../c++/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");
+ }
+}
+}