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 --- src/codec-json/json.h | 8 ++- src/codec-json/json.tmpl.h | 52 +++++++++++++++ tests/codec-json.cpp | 158 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+), 1 deletion(-) diff --git a/src/codec-json/json.h b/src/codec-json/json.h index f547e54..58b64aa 100644 --- a/src/codec-json/json.h +++ b/src/codec-json/json.h @@ -24,6 +24,10 @@ public: return buffer_; } + const buffer& get_buffer() const { + return buffer_; + } + error push(uint8_t val){ return buffer_.push(val); } @@ -86,7 +90,9 @@ public: } template - error_or decode(const data& from_decode, data& to_decode){ + error_or decode(data& from_decode, data& to_decode){ + buffer_view buff_v{from_decode.get_buffer()}; + return void_t {}; } }; diff --git a/src/codec-json/json.tmpl.h b/src/codec-json/json.tmpl.h index f5788e7..b8cd7aa 100644 --- a/src/codec-json/json.tmpl.h +++ b/src/codec-json/json.tmpl.h @@ -38,6 +38,58 @@ struct json_encode, RootSchema, FromEncode> { return void_t{}; } + + static error_or decode(buffer_view& buff, data, FromEncode>& to){ + assert((buff.read() >= '0' && buff.read() <= '9') || + buff.read() == '+' || buff.read() == '-'); + + std::size_t offset = 0; + + if(buff.read() == '-'){ + ++offset; + }else if(buff.read() == '+'){ + return make_error(); + } + if (offset >= buff.read_composite_length()) { + return make_error(); + } + if(buff.read(offset) >= '1' && buff.read(offset) <= '9'){ + ++offset; + + if(offset >= buff.read_composite_length()){ + return make_error(); + } + + while(1){ + if (buff.read(offset) >= '0' && buff.read(offset) <= '9'){ + ++offset; + + if(offset >= buff.read_composite_length()){ + return make_error(); + } + continue; + } + break; + } + }else if (buff.read(offset) == '0' ){ + ++offset; + }else{ + return make_error(); + } + + { + std::string_view num_view{reinterpret_cast(&buff.read()), offset}; + typename native_data_type>::type result; + auto fc_result = std::from_chars(num_view.data(), num_view.data() + num_view.size(), result); + if(fc_result.ec != std::errc{}){ + return make_error(); + } + + to.set(result); + } + + return void_t{}; + } }; template 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