From b155dff8b068dc87d22eefd07150a681790247b8 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 16 Jan 2024 15:26:22 +0100 Subject: codec-json: Moving to new directory structure --- tests/codec-json.cpp | 355 --------------------------------------------------- 1 file changed, 355 deletions(-) delete mode 100644 tests/codec-json.cpp (limited to 'tests/codec-json.cpp') diff --git a/tests/codec-json.cpp b/tests/codec-json.cpp deleted file mode 100644 index e1b8b5c..0000000 --- a/tests/codec-json.cpp +++ /dev/null @@ -1,355 +0,0 @@ -#include -#include - -#include - -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, - Member ->; -} - -SAW_TEST("UInt8 write"){ - using namespace saw; - data native_int; - data json_int; - - native_int.set(121); - - codec json_codec; - - error_or 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 native_int; - data json_int; - - native_int.set(24413); - - codec json_codec; - - error_or 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 native_int; - data json_int; - - native_int.set(44123); - - codec json_codec; - - error_or 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 native_int; - data json_int; - - native_int.set(243345543); - - codec json_codec; - - error_or 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 native_int; - data json_int; - - native_int.set(-121); - - codec json_codec; - - error_or 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 native_int; - data json_int; - - native_int.set(-24413); - - codec json_codec; - - error_or 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 native_int; - data json_int; - - native_int.set(44123); - - codec json_codec; - - error_or 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 native_int; - data json_int; - - native_int.set(243345543); - - codec json_codec; - - error_or 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 nat_str; - data json_str; - - nat_str.set("foo"); - - codec codec; - - error_or 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 native_tup; - data 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 json_codec; - - error_or 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 native{3u}; - data json; - - native.at(0).set("foo"); - native.at(1).set("bar"); - native.at(2).set("baz"); - - codec json_codec; - - error_or 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 native{2,1,2}; - data 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 codec; - - error_or 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 native; - data json; - - native.get<"foo">().set(5); - native.get<"bar">().set("baz"); - - codec json_codec; - - error_or 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 native_int; - data json_int{"43"}; - - codec json_codec; - - error_or 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::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(native_int.get()))); -} - -SAW_TEST("Int64 read"){ - using namespace saw; - data native_int; - data json_int{"-453"}; - - codec json_codec; - - error_or 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::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(native_int.get()))); -} -} -- cgit v1.2.3