diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/codec/.nix/derivation.nix | 2 | ||||
-rw-r--r-- | modules/codec/SConstruct | 4 | ||||
-rw-r--r-- | modules/codec/c++/SConscript | 8 | ||||
-rw-r--r-- | modules/codec/c++/data.h | 12 | ||||
-rw-r--r-- | modules/codec/c++/interface.h | 2 | ||||
-rw-r--r-- | modules/codec/c++/schema.h | 4 | ||||
-rw-r--r-- | modules/codec/c++/simple.h | 4 | ||||
-rw-r--r-- | modules/codec/c++/stream_value.h | 4 | ||||
-rw-r--r-- | modules/codec/tests/SConscript | 2 | ||||
-rw-r--r-- | modules/codec/tests/codec.cpp | 390 | ||||
-rw-r--r-- | modules/core/c++/SConscript | 2 |
11 files changed, 415 insertions, 19 deletions
diff --git a/modules/codec/.nix/derivation.nix b/modules/codec/.nix/derivation.nix index bcf2be7..7111be1 100644 --- a/modules/codec/.nix/derivation.nix +++ b/modules/codec/.nix/derivation.nix @@ -24,5 +24,7 @@ in stdenv.mkDerivation { clang-tools ]; + doCheck = true; + outputs = ["out" "dev"]; } diff --git a/modules/codec/SConstruct b/modules/codec/SConstruct index def568e..364f1fe 100644 --- a/modules/codec/SConstruct +++ b/modules/codec/SConstruct @@ -46,7 +46,8 @@ env_vars.Add('prefix', env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], CPPDEFINES=['SAW_UNIX'], CXXFLAGS=['-std=c++20','-g','-Wall','-Wextra'], - LIBS=['forstio-core']) + LIBS=['forstio-core'] +) env.__class__.add_source_files = add_kel_source_files env.Tool('compilation_db'); env.cdb = env.CompilationDatabase('compile_commands.json'); @@ -58,6 +59,7 @@ env.targets = []; Export('env') SConscript('c++/SConscript') +SConscript('tests/SConscript') env.Alias('cdb', env.cdb); env.Alias('all', [env.targets]); diff --git a/modules/codec/c++/SConscript b/modules/codec/c++/SConscript index c038d42..14e7b5c 100644 --- a/modules/codec/c++/SConscript +++ b/modules/codec/c++/SConscript @@ -21,18 +21,18 @@ env.headers += codec_env.headers; ## Shared lib objects_shared = [] codec_env.add_source_files(objects_shared, codec_env.sources, shared=True); -codec_env.library_shared = codec_env.SharedLibrary('#build/forstio-codec', [objects_shared]); +env.library_shared = codec_env.SharedLibrary('#build/forstio-codec', [objects_shared]); ## Static lib objects_static = [] codec_env.add_source_files(objects_static, codec_env.sources, shared=False); -codec_env.library_static = codec_env.StaticLibrary('#build/forstio-codec', [objects_static]); +env.library_static = codec_env.StaticLibrary('#build/forstio-codec', [objects_static]); # Set Alias -env.Alias('library_codec', [codec_env.library_shared, codec_env.library_static]); +env.Alias('library_codec', [env.library_shared, env.library_static]); env.targets += ['library_codec']; # Install -env.Install('$prefix/lib/', [codec_env.library_shared, codec_env.library_static]); +env.Install('$prefix/lib/', [env.library_shared, env.library_static]); env.Install('$prefix/include/forstio/codec/', [codec_env.headers]); diff --git a/modules/codec/c++/data.h b/modules/codec/c++/data.h index 237ef5a..365405f 100644 --- a/modules/codec/c++/data.h +++ b/modules/codec/c++/data.h @@ -1,7 +1,7 @@ #pragma once -#include <forstio/core/common.h> -#include <forstio/core/templates.h> +#include <forstio/common.h> +#include <forstio/templates.h> #include <cassert> @@ -207,7 +207,7 @@ class data<schema::Array<T,Dim>, encode::Native> { template<std::integral... Dims> data(Dims... size_): - data{{size_...}} + data{{static_cast<std::size_t>(size_)...}} { static_assert(sizeof...(Dims)==Dim, "Argument size must be equal to the Dimension"); } @@ -222,12 +222,12 @@ class data<schema::Array<T,Dim>, encode::Native> { template<std::integral... Dims> data<T, encode::Native>& at(Dims... i){ - return value_.at(this->get_flat_index({i...})); + return value_.at(this->get_flat_index({static_cast<std::size_t>(i)...})); } template<std::integral... Dims> const data<T, encode::Native>& at(Dims... i) const { - return value_.at(this->get_flat_index({i...})); + return value_.at(this->get_flat_index({static_cast<std::size_t>(i)...})); } std::size_t get_dim_size(std::size_t i) const { @@ -352,7 +352,7 @@ class data<schema::Primitive<T,N>, encode::Native> { private: typename native_data_type<schema::Primitive<T,N>>::type value_; public: - data():value_{{}}{}; + data():value_{}{} SAW_DEFAULT_COPY(data); SAW_DEFAULT_MOVE(data); diff --git a/modules/codec/c++/interface.h b/modules/codec/c++/interface.h index b1422a9..89bfc26 100644 --- a/modules/codec/c++/interface.h +++ b/modules/codec/c++/interface.h @@ -1,6 +1,6 @@ #pragma once -#include <forstio/core/error.h> +#include <forstio/error.h> #include "schema.h" #include "data.h" diff --git a/modules/codec/c++/schema.h b/modules/codec/c++/schema.h index a8494fe..69233fa 100644 --- a/modules/codec/c++/schema.h +++ b/modules/codec/c++/schema.h @@ -1,7 +1,7 @@ #pragma once -#include <forstio/core/common.h> -#include <forstio/core/string_literal.h> +#include <forstio/common.h> +#include <forstio/string_literal.h> namespace saw { namespace schema { diff --git a/modules/codec/c++/simple.h b/modules/codec/c++/simple.h index 8760754..aff0626 100644 --- a/modules/codec/c++/simple.h +++ b/modules/codec/c++/simple.h @@ -3,8 +3,8 @@ #include "data.h" #include "stream_value.h" -#include <forstio/core/buffer.h> -#include <forstio/core/error.h> +#include <forstio/buffer.h> +#include <forstio/error.h> namespace saw { namespace encode { diff --git a/modules/codec/c++/stream_value.h b/modules/codec/c++/stream_value.h index 09203cb..b5a58bd 100644 --- a/modules/codec/c++/stream_value.h +++ b/modules/codec/c++/stream_value.h @@ -2,8 +2,8 @@ #include "schema.h" -#include <forstio/core/buffer.h> -#include <forstio/core/error.h> +#include <forstio/buffer.h> +#include <forstio/error.h> #include <cstdint> #include <cstring> 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"); + } +} +} diff --git a/modules/core/c++/SConscript b/modules/core/c++/SConscript index cea6b76..3f9bd92 100644 --- a/modules/core/c++/SConscript +++ b/modules/core/c++/SConscript @@ -35,7 +35,7 @@ env.targets += ['library_core']; # Install env.Install('$prefix/lib/', [env.library_shared, env.library_static]); -env.Install('$prefix/include/forstio/core/', [core_env.headers]); +env.Install('$prefix/include/forstio/', [core_env.headers]); # Test # Export('core_env'); |