From f5629bf0da55ebaaddc5cf551c36ed7362dc54b5 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 20 Jun 2023 09:05:08 +0200 Subject: c++: Adding tests and working around some constness problems --- tests/codec-json.cpp | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) (limited to 'tests/codec-json.cpp') diff --git a/tests/codec-json.cpp b/tests/codec-json.cpp index 1c77f46..030b1e4 100644 --- a/tests/codec-json.cpp +++ b/tests/codec-json.cpp @@ -25,6 +25,127 @@ using TestStruct = Struct< 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; @@ -45,6 +166,26 @@ SAW_TEST("Int32 write"){ 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"){ using namespace saw; data nat_str; @@ -147,4 +288,21 @@ SAW_TEST("Struct write"){ SAW_EXPECT(enc_val == str_v, std::string{"Struct not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v}); } + +SAW_TEST("Int8 read"){ + using namespace saw; + data native_int; + data json_int; + + codec json_codec; + + error_or eov = json_codec.decode(json_int, native_int); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * 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()))); +} } -- cgit v1.2.3