From 1da06cc4d8b64b8d63782e356acb0cb946789d20 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Wed, 25 Oct 2023 17:12:45 +0200 Subject: codec: Added basic interface and function class Preparation work for RPC functionality. --- tests/codec.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) (limited to 'tests/codec.cpp') diff --git a/tests/codec.cpp b/tests/codec.cpp index 7253ecc..439332a 100644 --- a/tests/codec.cpp +++ b/tests/codec.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -27,6 +28,19 @@ using TestTuple = Tuple< TwoDimArray, UInt64 >; + +using TestInt32Pair = Tuple< + Int32, + Int32 +>; + +using TestCalcFunction = Function; + +using TestInterface = Interface< + Member, + Member, + Member +>; } SAW_TEST("One Dimensional Array") { using namespace saw; @@ -256,6 +270,7 @@ SAW_TEST("KelSimple Tuple write and read back"){ 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; @@ -279,4 +294,97 @@ SAW_TEST("KelSimple String write and read back"){ SAW_EXPECT(native == str, "String should've been decoded back correctly"); } + +SAW_TEST("Function basics"){ + using namespace saw; + + { + data native; + + native.get<0>().set(5); + native.get<1>().set(40); + + auto func_add = function_factory::create( + [](data req){ + data 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 native; + + auto func_add = + [](data req){ + data resp; + + resp.set(req.get<0>().get() + req.get<1>().get()); + + return resp; + }; + auto func_sub = + [](data req){ + data resp; + + resp.set(req.get<0>().get() - req.get<1>().get()); + + return resp; + }; + auto func_multiply = [](data req){ + data resp; + + resp.set(req.get<0>().get() * req.get<1>().get()); + + return resp; + }; + + auto iface = interface_factory::create(std::move(func_add), std::move(func_sub), std::move(func_multiply)); + + { + data 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 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 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"); + } +} } -- cgit v1.2.3