diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-01-16 15:26:22 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-01-16 15:26:22 +0100 |
commit | b155dff8b068dc87d22eefd07150a681790247b8 (patch) | |
tree | 22e20f2593622c5ef10a43d59d257fa5bc9e5ea5 /modules/codec-json | |
parent | ca5063213282287061184263ce7e08d10f60dce9 (diff) |
codec-json: Moving to new directory structure
Diffstat (limited to 'modules/codec-json')
-rw-r--r-- | modules/codec-json/SConstruct | 9 | ||||
-rw-r--r-- | modules/codec-json/c++/SConscript (renamed from modules/codec-json/SConscript) | 8 | ||||
-rw-r--r-- | modules/codec-json/c++/json.h (renamed from modules/codec-json/json.h) | 0 | ||||
-rw-r--r-- | modules/codec-json/c++/json.tmpl.h (renamed from modules/codec-json/json.tmpl.h) | 0 | ||||
-rw-r--r-- | modules/codec-json/tests/SConscript | 31 | ||||
-rw-r--r-- | modules/codec-json/tests/codec-json.cpp | 355 |
6 files changed, 397 insertions, 6 deletions
diff --git a/modules/codec-json/SConstruct b/modules/codec-json/SConstruct index edd5f57..20dd972 100644 --- a/modules/codec-json/SConstruct +++ b/modules/codec-json/SConstruct @@ -46,7 +46,11 @@ 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-codec']) + LIBS=[ + 'forstio-core', + 'forstio-codec' + ] +); env.__class__.add_source_files = add_kel_source_files env.Tool('compilation_db'); env.cdb = env.CompilationDatabase('compile_commands.json'); @@ -57,7 +61,8 @@ env.headers = []; env.targets = []; Export('env') -SConscript('SConscript') +SConscript('c++/SConscript') +SConscript('tests/SConscript') env.Alias('cdb', env.cdb); env.Alias('all', [env.targets]); diff --git a/modules/codec-json/SConscript b/modules/codec-json/c++/SConscript index 772ac0b..868e11e 100644 --- a/modules/codec-json/SConscript +++ b/modules/codec-json/c++/SConscript @@ -21,18 +21,18 @@ env.headers += codec_json_env.headers; ## Shared lib objects_shared = [] codec_json_env.add_source_files(objects_shared, codec_json_env.sources, shared=True); -codec_json_env.library_shared = codec_json_env.SharedLibrary('#build/forstio-codec-json', [objects_shared]); +env.library_shared = codec_json_env.SharedLibrary('#build/forstio-codec-json', [objects_shared]); ## Static lib objects_static = [] codec_json_env.add_source_files(objects_static, codec_json_env.sources, shared=False); -codec_json_env.library_static = codec_json_env.StaticLibrary('#build/forstio-codec-json', [objects_static]); +env.library_static = codec_json_env.StaticLibrary('#build/forstio-codec-json', [objects_static]); # Set Alias -env.Alias('library_codec_json', [codec_json_env.library_shared, codec_json_env.library_static]); +env.Alias('library_codec_json', [env.library_shared, env.library_static]); env.targets += ['library_codec_json']; # Install -env.Install('$prefix/lib/', [codec_json_env.library_shared, codec_json_env.library_static]); +env.Install('$prefix/lib/', [env.library_shared, env.library_static]); env.Install('$prefix/include/forstio/codec/json/', [codec_json_env.headers]); diff --git a/modules/codec-json/json.h b/modules/codec-json/c++/json.h index bc60ee9..bc60ee9 100644 --- a/modules/codec-json/json.h +++ b/modules/codec-json/c++/json.h diff --git a/modules/codec-json/json.tmpl.h b/modules/codec-json/c++/json.tmpl.h index 84f0058..84f0058 100644 --- a/modules/codec-json/json.tmpl.h +++ b/modules/codec-json/c++/json.tmpl.h diff --git a/modules/codec-json/tests/SConscript b/modules/codec-json/tests/SConscript new file mode 100644 index 0000000..608c2b7 --- /dev/null +++ b/modules/codec-json/tests/SConscript @@ -0,0 +1,31 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +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")) + +env.sources += test_cases_env.sources; +env.headers += test_cases_env.headers; + +objects_static = [] +test_cases_env.add_source_files(objects_static, test_cases_env.sources, shared=False); +test_cases_env.program = test_cases_env.Program('#bin/tests', [objects_static, env.library_static]); + +# Set Alias +env.Alias('test', test_cases_env.program); +env.Alias('check', test_cases_env.program); + +env.targets += ['test','check']; diff --git a/modules/codec-json/tests/codec-json.cpp b/modules/codec-json/tests/codec-json.cpp new file mode 100644 index 0000000..83ec859 --- /dev/null +++ b/modules/codec-json/tests/codec-json.cpp @@ -0,0 +1,355 @@ +#include <forstio/test/suite.h> +#include "../c++/json.h" + +#include <iostream> + +namespace { +namespace schema { +using namespace saw::schema; + +using TestTuple = Tuple< + String, + Int32 +>; + +using TestArray = Array< + String, 1 +>; + +using TestMultiArray = Array< + String, 3 +>; + +using TestStruct = Struct< + Member<Int32, "foo">, + Member<String, "bar"> +>; +} + +SAW_TEST("UInt8 write"){ + using namespace saw; + data<schema::UInt8, encode::Native> native_int; + data<schema::UInt8, encode::Json> json_int; + + native_int.set(121); + + codec<schema::UInt8, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_int, json_int); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_v = "121"; + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT( enc_val == str_v, std::string{"Value is not being encoded correctly. Encoded: "} + enc_val ); +} + +SAW_TEST("UInt16 write"){ + using namespace saw; + data<schema::UInt16, encode::Native> native_int; + data<schema::UInt16, encode::Json> json_int; + + native_int.set(24413); + + codec<schema::UInt16, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_int, json_int); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_v = "24413"; + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT( enc_val == str_v, std::string{"Value is not being encoded correctly. Encoded: "} + enc_val ); +} + +SAW_TEST("UInt32 write"){ + using namespace saw; + data<schema::UInt32, encode::Native> native_int; + data<schema::UInt32, encode::Json> json_int; + + native_int.set(44123); + + codec<schema::UInt32, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_int, json_int); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_v = "44123"; + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT( enc_val == str_v, std::string{"Value is not being encoded correctly. Encoded: "} + enc_val ); +} + +SAW_TEST("UInt64 write"){ + using namespace saw; + data<schema::UInt64, encode::Native> native_int; + data<schema::UInt64, encode::Json> json_int; + + native_int.set(243345543); + + codec<schema::UInt64, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_int, json_int); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_v = "243345543"; + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT( enc_val == str_v, std::string{"Value is not being encoded correctly. Encoded: "} + enc_val ); +} + +SAW_TEST("Int8 write"){ + using namespace saw; + data<schema::Int8, encode::Native> native_int; + data<schema::Int8, encode::Json> json_int; + + native_int.set(-121); + + codec<schema::Int8, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_int, json_int); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_v = "-121"; + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT( enc_val == str_v, std::string{"Value is not being encoded correctly. Encoded: "} + enc_val ); +} + +SAW_TEST("Int16 write"){ + using namespace saw; + data<schema::Int16, encode::Native> native_int; + data<schema::Int16, encode::Json> json_int; + + native_int.set(-24413); + + codec<schema::Int16, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_int, json_int); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_v = "-24413"; + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT( enc_val == str_v, std::string{"Value is not being encoded correctly. Encoded: "} + enc_val ); +} + +SAW_TEST("Int32 write"){ + using namespace saw; + data<schema::Int32, encode::Native> native_int; + data<schema::Int32, encode::Json> json_int; + + native_int.set(44123); + + codec<schema::Int32, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_int, json_int); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_v = "44123"; + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT( enc_val == str_v, std::string{"Value is not being encoded correctly. Encoded: "} + enc_val ); +} + +SAW_TEST("Int64 write"){ + using namespace saw; + data<schema::Int64, encode::Native> native_int; + data<schema::Int64, encode::Json> json_int; + + native_int.set(243345543); + + codec<schema::Int64, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_int, json_int); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_v = "243345543"; + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT( enc_val == str_v, std::string{"Value is not being encoded correctly. Encoded: "} + enc_val ); +} + +SAW_TEST("String write and read"){ + using namespace saw; + data<schema::String, encode::Native> nat_str; + data<schema::String, encode::Json> json_str; + + nat_str.set("foo"); + + codec<schema::String, encode::Json> codec; + + error_or<void> eov = codec.encode(nat_str, json_str); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_view = "\"foo\""; + std::string encoded_value = convert_to_string(json_str.get_buffer()); + + SAW_EXPECT(encoded_value == str_view, "String not encoded correctly"); + + nat_str = {}; + eov = codec.decode(json_str, nat_str); + SAW_EXPECT(eov.is_value(), "Decoding error"); + + SAW_EXPECT(nat_str == "foo", "Incorrect value decoded"); +} + +SAW_TEST("Tuple read and write"){ + using namespace saw; + data<schema::TestTuple, encode::Native> native_tup; + data<schema::TestTuple, encode::Json> json_tup; + + auto& nat_zero = native_tup.template get<0>(); + auto& nat_one = native_tup.template get<1>(); + + nat_zero.set("bar"); + nat_one.set(34); + + codec<schema::TestTuple, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_tup, json_tup); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + std::string_view str_v = "[\"bar\",34]"; + std::string enc_val = convert_to_string(json_tup.get_buffer()); + + SAW_EXPECT(enc_val == str_v, std::string{"Tuple not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v}); + native_tup = {}; + + eov = json_codec.decode(json_tup, native_tup); + SAW_EXPECT(eov.is_value(), "Decoding error"); + + SAW_EXPECT(native_tup.template get<0>() == "bar", "Invalid Value 0"); + SAW_EXPECT(native_tup.template get<1>().get() == 34, "Invalid Value 1"); +} + +SAW_TEST("Array write"){ + using namespace saw; + data<schema::TestArray, encode::Native> native{3u}; + data<schema::TestArray, encode::Json> json; + + native.at(0).set("foo"); + native.at(1).set("bar"); + native.at(2).set("baz"); + + codec<schema::TestArray, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native, json); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + std::string_view str_v = "[\"foo\",\"bar\",\"baz\"]"; + std::string enc_val = convert_to_string(json.get_buffer()); + + SAW_EXPECT(enc_val == str_v, std::string{"Array not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v}); +} + +SAW_TEST("Three Dim Array write and read"){ + using namespace saw; + data<schema::TestMultiArray, encode::Native> native{2,1,2}; + data<schema::TestMultiArray, encode::Json> json; + + native.at(0,0,0).set("multi"); + native.at(0,0,1).set("baz"); + native.at(1,0,0).set("foo"); + native.at(1,0,1).set("bar"); + + codec<schema::TestMultiArray, encode::Json> codec; + + error_or<void> eov = codec.encode(native, json); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + std::string_view str_v = "[[[\"multi\",\"baz\"]],[[\"foo\",\"bar\"]]]"; + std::string enc_val = convert_to_string(json.get_buffer()); + + SAW_EXPECT(enc_val == str_v, std::string{"Array not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v}); + + native = {}; + eov = codec.decode(json, native); + SAW_EXPECT(eov.is_value(), "Decoding error"); + SAW_EXPECT(native.at(0,0,0) == "multi", "Invalid Value at 0,0,0"); + SAW_EXPECT(native.at(0,0,1) == "baz", "Invalid Value at 0,0,1"); + SAW_EXPECT(native.at(1,0,0) == "foo", "Invalid Value at 1,0,0"); + SAW_EXPECT(native.at(1,0,1) == "bar", "Invalid Value at 1,0,1"); + +} + +SAW_TEST("Struct read and write"){ + using namespace saw; + data<schema::TestStruct, encode::Native> native; + data<schema::TestStruct, encode::Json> json; + + native.get<"foo">().set(5); + native.get<"bar">().set("baz"); + + codec<schema::TestStruct, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native, json); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + std::string_view str_v = "{\"foo\":5,\"bar\":\"baz\"}"; + std::string enc_val = convert_to_string(json.get_buffer()); + + SAW_EXPECT(enc_val == str_v, std::string{"Struct not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v}); + + native = {}; + eov = json_codec.decode(json, native); + SAW_EXPECT(eov.is_value(), "Decoding error"); + SAW_EXPECT(native.get<"foo">().get() == 5, "Invalid value for foo"); + SAW_EXPECT(native.get<"bar">() == "baz", "Invalid value for bar"); +} + +SAW_TEST("Int8 read"){ + using namespace saw; + data<schema::Int8, encode::Native> native_int; + data<schema::Int8, encode::Json> json_int{"43"}; + + codec<schema::Int8, encode::Json> json_codec; + + error_or<void> eov = json_codec.decode(json_int, native_int); + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT(eov.is_value(), std::string{"Encoding error: "} + enc_val); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + typename native_data_type<schema::Int8>::type dec_val = 43; + SAW_EXPECT( dec_val == native_int.get(), std::string{"Value is not being encoded correctly. Encoded: "} + std::to_string(static_cast<int>(native_int.get()))); +} + +SAW_TEST("Int64 read"){ + using namespace saw; + data<schema::Int64, encode::Native> native_int; + data<schema::Int64, encode::Json> json_int{"-453"}; + + codec<schema::Int64, encode::Json> json_codec; + + error_or<void> eov = json_codec.decode(json_int, native_int); + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT(eov.is_value(), std::string{"Encoding error: "} + enc_val); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + typename native_data_type<schema::Int64>::type dec_val = -453; + SAW_EXPECT( dec_val == native_int.get(), std::string{"Value is not being encoded correctly. Encoded: "} + std::to_string(static_cast<int>(native_int.get()))); +} +} |