summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2023-06-20 09:05:08 +0200
committerClaudius "keldu" Holeksa <mail@keldu.de>2023-06-20 09:05:08 +0200
commitf5629bf0da55ebaaddc5cf551c36ed7362dc54b5 (patch)
tree4a318cc4e4ee2ff4046958ca13d6798856f71aa2
parent777b7490470c9f1328abe8d0f83bedda8f02593d (diff)
c++: Adding tests and working around some constness problems
-rw-r--r--src/codec-json/json.h8
-rw-r--r--src/codec-json/json.tmpl.h52
-rw-r--r--tests/codec-json.cpp158
3 files changed, 217 insertions, 1 deletions
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 <typename ToEncoding>
- error_or<void> decode(const data<Schema, encode::Json>& from_decode, data<Schema, ToEncoding>& to_decode){
+ error_or<void> decode(data<Schema, encode::Json>& from_decode, data<Schema, ToEncoding>& 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<schema::Primitive<T,N>, RootSchema, FromEncode> {
return void_t{};
}
+
+ static error_or<void> decode(buffer_view& buff, data<schema::Primitive<T,N>, 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<err::not_supported>();
+ }
+ if (offset >= buff.read_composite_length()) {
+ return make_error<err::buffer_exhausted>();
+ }
+ if(buff.read(offset) >= '1' && buff.read(offset) <= '9'){
+ ++offset;
+
+ if(offset >= buff.read_composite_length()){
+ return make_error<err::buffer_exhausted>();
+ }
+
+ while(1){
+ if (buff.read(offset) >= '0' && buff.read(offset) <= '9'){
+ ++offset;
+
+ if(offset >= buff.read_composite_length()){
+ return make_error<err::buffer_exhausted>();
+ }
+ continue;
+ }
+ break;
+ }
+ }else if (buff.read(offset) == '0' ){
+ ++offset;
+ }else{
+ return make_error<err::buffer_exhausted>();
+ }
+
+ {
+ std::string_view num_view{reinterpret_cast<char*>(&buff.read()), offset};
+ typename native_data_type<schema::Primitive<T,N>>::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<err::invalid_state>();
+ }
+
+ to.set(result);
+ }
+
+ return void_t{};
+ }
};
template<typename RootSchema, typename FromEncode>
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<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;
@@ -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<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"){
using namespace saw;
data<schema::String, encode::Native> 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<schema::Int8, encode::Native> native_int;
+ data<schema::Int8, encode::Json> json_int;
+
+ codec<schema::Int8, encode::Json> json_codec;
+
+ error_or<void> 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<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())));
+}
}