diff --git a/include/rfl/avro/Writer.hpp b/include/rfl/avro/Writer.hpp index 3c22c973f..02389e1be 100644 --- a/include/rfl/avro/Writer.hpp +++ b/include/rfl/avro/Writer.hpp @@ -348,13 +348,21 @@ class RFL_API Writer { } } else if constexpr (std::is_same_v) { - int result = avro_value_set_float(_val, static_cast(_var)); + int result = avro_value_set_float(_val, _var); if (result != 0) { throw std::runtime_error( "Error setting float value: " + std::to_string(result) + ": " + avro_strerror()); } + } else if constexpr (std::is_same_v) { + int result = avro_value_set_double(_val, _var); + if (result != 0) { + throw std::runtime_error( + "Error setting double value: " + std::to_string(result) + ": " + + avro_strerror()); + } + } else if constexpr (std::is_floating_point_v) { int result = avro_value_set_double(_val, static_cast(_var)); if (result != 0) { diff --git a/include/rfl/boost_serialization/Reader.hpp b/include/rfl/boost_serialization/Reader.hpp index d4dc30b48..ec15eb0ae 100644 --- a/include/rfl/boost_serialization/Reader.hpp +++ b/include/rfl/boost_serialization/Reader.hpp @@ -48,46 +48,30 @@ struct Reader { static constexpr bool has_custom_constructor = false; /// Checks if the given Boost archive value is empty. - /// Always returns false for Boost.Serialization as it doesn't have a null concept. + /// Always returns false for Boost.Serialization as it doesn't have a null + /// concept. /// @param _var The archive variable (unused) /// @return Always false bool is_empty(const InputVarType& /*_var*/) const noexcept { return false; } /// Converts a value from the Boost archive to a basic C++ type. - /// Supports strings, booleans, floating-point numbers, integers, and literal types. + /// Supports strings, booleans, floating-point numbers, integers, and literal + /// types. /// @tparam T The target C++ type to convert to /// @param _var The archive variable containing the value /// @return A Result containing the converted value or an error template rfl::Result to_basic_type(const InputVarType& _var) const noexcept { try { - if constexpr (std::is_same, std::string>()) { - std::string val; - *_var.ar >> val; - return val; - } else if constexpr (std::is_same, bool>()) { - bool val = false; - *_var.ar >> val; - return val; - } else if constexpr (std::is_floating_point>()) { - double val = 0.0; - *_var.ar >> val; - return static_cast(val); - } else if constexpr (std::is_unsigned>()) { - std::uint64_t val = 0; - *_var.ar >> val; - return static_cast(val); - } else if constexpr (std::is_integral>()) { - std::int64_t val = 0; - *_var.ar >> val; - return static_cast(val); - } else if constexpr (internal::is_literal_v) { + if constexpr (internal::is_literal_v) { std::int64_t val = 0; *_var.ar >> val; return T::from_value( static_cast::ValueType>(val)); } else { - static_assert(rfl::always_false_v, "Unsupported type."); + T val; + *_var.ar >> val; + return val; } } catch (std::exception& e) { return error(e.what()); @@ -147,7 +131,8 @@ struct Reader { } /// Reads all elements from an array using the provided array reader. - /// @tparam ArrayReader The type of reader that processes individual array elements + /// @tparam ArrayReader The type of reader that processes individual array + /// elements /// @param _array_reader The reader object that processes each element /// @param _arr The array structure containing size information /// @return std::nullopt on success, or an Error if reading fails @@ -188,7 +173,8 @@ struct Reader { /// Reads all fields from an object using the provided object reader. /// Fields are accessed by their index. - /// @tparam ObjectReader The type of reader that processes individual object fields + /// @tparam ObjectReader The type of reader that processes individual object + /// fields /// @param _object_reader The reader object that processes each field /// @param _obj The object structure containing size information /// @return std::nullopt on success, or an Error if reading fails @@ -202,8 +188,9 @@ struct Reader { return std::nullopt; } - /// Reads a union (variant) value and converts it to the appropriate variant type. - /// Reads the discriminant first to determine which alternative is active. + /// Reads a union (variant) value and converts it to the appropriate variant + /// type. Reads the discriminant first to determine which alternative is + /// active. /// @tparam VariantType The C++ variant type to construct /// @tparam UnionReaderType The type of reader that handles union alternatives /// @param _union The union structure diff --git a/include/rfl/boost_serialization/Writer.hpp b/include/rfl/boost_serialization/Writer.hpp index 8f77628da..ccf1e74f9 100644 --- a/include/rfl/boost_serialization/Writer.hpp +++ b/include/rfl/boost_serialization/Writer.hpp @@ -270,20 +270,10 @@ class Writer { template void new_value(const T& _var) const { using Type = std::remove_cvref_t; - if constexpr (std::is_same()) { - *ar_ << _var; - } else if constexpr (std::is_same()) { - *ar_ << _var; - } else if constexpr (std::is_floating_point()) { - *ar_ << static_cast(_var); - } else if constexpr (std::is_unsigned()) { - *ar_ << static_cast(_var); - } else if constexpr (std::is_integral()) { - *ar_ << static_cast(_var); - } else if constexpr (internal::is_literal_v) { + if constexpr (internal::is_literal_v) { *ar_ << static_cast(_var.value()); } else { - static_assert(rfl::always_false_v, "Unsupported type."); + *ar_ << _var; } } diff --git a/include/rfl/bson/Writer.hpp b/include/rfl/bson/Writer.hpp index 29b1fdb25..ab95ebcc6 100644 --- a/include/rfl/bson/Writer.hpp +++ b/include/rfl/bson/Writer.hpp @@ -117,13 +117,20 @@ class RFL_API Writer { throw std::runtime_error("Could not append bool to array."); } - } else if constexpr (std::is_floating_point>()) { + } else if constexpr (std::is_same, float>()) { const bool ok = bson_array_builder_append_double( - _parent->val_, static_cast(_var)); + _parent->val_, _var); if (!ok) { throw std::runtime_error("Could not append float to array."); } + } else if constexpr (std::is_same, double>()) { + const bool ok = bson_array_builder_append_double( + _parent->val_, _var); + if (!ok) { + throw std::runtime_error("Could not append double to array."); + } + } else if constexpr (std::is_integral>()) { const bool ok = bson_array_builder_append_int64( _parent->val_, static_cast(_var)); @@ -175,12 +182,21 @@ class RFL_API Writer { std::string(_name) + "' to object."); } - } else if constexpr (std::is_floating_point>()) { + } else if constexpr (std::is_same, float>()) { + const bool ok = bson_append_double(_parent->val_, _name.data(), + static_cast(_name.size()), + _var); + if (!ok) { + throw std::runtime_error("Could not float field '" + + std::string(_name) + "' to object."); + } + + } else if constexpr (std::is_same, double>()) { const bool ok = bson_append_double(_parent->val_, _name.data(), - static_cast(_name.size()), - static_cast(_var)); + static_cast(_name.size()), + _var); if (!ok) { - throw std::runtime_error("Could not floating point field '" + + throw std::runtime_error("Could not double field '" + std::string(_name) + "' to object."); } @@ -195,7 +211,7 @@ class RFL_API Writer { } else if constexpr (std::is_same, bson_oid_t>()) { const bool ok = bson_append_oid(_parent->val_, _name.data(), - static_cast(_name.size()), &_var); + static_cast(_name.size()), &_var); if (!ok) { throw std::runtime_error("Could not oid field '" + std::string(_name) + "' to object."); diff --git a/include/rfl/capnproto/Writer.hpp b/include/rfl/capnproto/Writer.hpp index 3767fb0cb..f61967714 100644 --- a/include/rfl/capnproto/Writer.hpp +++ b/include/rfl/capnproto/Writer.hpp @@ -167,10 +167,18 @@ class RFL_API Writer { internal::ptr_cast(_var.data()), _var.size()); _parent->val_.set(_parent->ix_++, capnp::Data::Reader(array_ptr)); - } else if constexpr (std::is_floating_point>() || - std::is_same, bool>()) { + } else if constexpr (std::is_same, bool>()) { _parent->val_.set(_parent->ix_++, _var); + } else if constexpr (std::is_same, float>()) { + _parent->val_.set(_parent->ix_++, _var); + + } else if constexpr (std::is_same, double>()) { + _parent->val_.set(_parent->ix_++, _var); + + } else if constexpr (std::is_floating_point>()) { + _parent->val_.set(_parent->ix_++, static_cast(_var)); + } else if constexpr (std::is_unsigned>()) { _parent->val_.set(_parent->ix_++, static_cast(_var)); @@ -211,10 +219,18 @@ class RFL_API Writer { _parent->val_.set(to_kj_string_ptr(_name), capnp::Data::Reader(array_ptr)); - } else if constexpr (std::is_floating_point>() || - std::is_same, bool>()) { + } else if constexpr (std::is_same, bool>()) { + _parent->val_.set(to_kj_string_ptr(_name), _var); + + } else if constexpr (std::is_same, float>()) { _parent->val_.set(to_kj_string_ptr(_name), _var); + } else if constexpr (std::is_same, double>()) { + _parent->val_.set(to_kj_string_ptr(_name), _var); + + } else if constexpr (std::is_floating_point>()) { + _parent->val_.set(to_kj_string_ptr(_name), static_cast(_var)); + } else if constexpr (std::is_unsigned>()) { _parent->val_.set(to_kj_string_ptr(_name), static_cast(_var)); @@ -246,10 +262,18 @@ class RFL_API Writer { internal::ptr_cast(_var.data()), _var.size()); _parent->val_.set(field, capnp::Data::Reader(array_ptr)); - } else if constexpr (std::is_floating_point>() || - std::is_same, bool>()) { + } else if constexpr (std::is_same, bool>()) { + _parent->val_.set(field, _var); + + } else if constexpr (std::is_same, float>()) { _parent->val_.set(field, _var); + } else if constexpr (std::is_same, double>()) { + _parent->val_.set(field, _var); + + } else if constexpr (std::is_floating_point>()) { + _parent->val_.set(field, static_cast(_var)); + } else if constexpr (std::is_unsigned>()) { _parent->val_.set(field, static_cast(_var)); diff --git a/include/rfl/cbor/Writer.hpp b/include/rfl/cbor/Writer.hpp index a934497e8..bf4e8d4ff 100644 --- a/include/rfl/cbor/Writer.hpp +++ b/include/rfl/cbor/Writer.hpp @@ -14,23 +14,27 @@ namespace rfl::cbor { -/// Writer class for serializing C++ objects to CBOR (Concise Binary Object Representation) format. -/// This class provides the interface for converting C++ objects into compact binary CBOR data. -/// CBOR is a binary data serialization format designed to be smaller and faster than JSON. +/// Writer class for serializing C++ objects to CBOR (Concise Binary Object +/// Representation) format. This class provides the interface for converting C++ +/// objects into compact binary CBOR data. CBOR is a binary data serialization +/// format designed to be smaller and faster than JSON. class RFL_API Writer { using Encoder = jsoncons::cbor::cbor_bytes_encoder; public: /// Represents a CBOR array being constructed during serialization. - /// This is a placeholder type as the actual array data is managed by the encoder. + /// This is a placeholder type as the actual array data is managed by the + /// encoder. struct CBOROutputArray {}; /// Represents a CBOR object being constructed during serialization. - /// This is a placeholder type as the actual object data is managed by the encoder. + /// This is a placeholder type as the actual object data is managed by the + /// encoder. struct CBOROutputObject {}; /// Represents a CBOR value being constructed during serialization. - /// This is a placeholder type as the actual value data is managed by the encoder. + /// This is a placeholder type as the actual value data is managed by the + /// encoder. struct CBOROutputVar {}; using OutputArrayType = CBOROutputArray; @@ -38,7 +42,8 @@ class RFL_API Writer { using OutputVarType = CBOROutputVar; /// Constructs a CBOR Writer with the specified encoder. - /// @param _encoder Pointer to the CBOR encoder that will write the binary output + /// @param _encoder Pointer to the CBOR encoder that will write the binary + /// output Writer(Encoder* _encoder); ~Writer(); @@ -49,7 +54,8 @@ class RFL_API Writer { OutputArrayType array_as_root(const size_t _size) const; /// Creates a CBOR object as the root element of the output. - /// @param The expected number of fields (unused, reserved for future optimization) + /// @param The expected number of fields (unused, reserved for future + /// optimization) /// @return An output object that can be populated with key-value pairs OutputObjectType object_as_root(const size_t) const; @@ -84,7 +90,8 @@ class RFL_API Writer { OutputObjectType* _parent) const; /// Adds a nested object to a parent array. - /// @param The expected number of fields (unused, reserved for future optimization) + /// @param The expected number of fields (unused, reserved for future + /// optimization) /// @param _parent Pointer to the parent array to add to /// @return An output object that can be populated with key-value pairs OutputObjectType add_object_to_array(const size_t, @@ -92,7 +99,8 @@ class RFL_API Writer { /// Adds a nested object to a parent object with the specified field name. /// @param _name The name of the field in the parent object - /// @param The expected number of fields (unused, reserved for future optimization) + /// @param The expected number of fields (unused, reserved for future + /// optimization) /// @param _parent Pointer to the parent object to add to /// @return An output object that can be populated with key-value pairs OutputObjectType add_object_to_object(const std::string_view& _name, @@ -103,7 +111,8 @@ class RFL_API Writer { /// Supports basic types like strings, numbers, booleans, and byte strings. /// @tparam T The type of the value to add /// @param _var The value to add to the array - /// @param _parent Pointer to the parent array (unused as encoder tracks context) + /// @param _parent Pointer to the parent array (unused as encoder tracks + /// context) /// @return An output variable representing the added value template OutputVarType add_value_to_array(const T& _var, @@ -116,7 +125,8 @@ class RFL_API Writer { /// @tparam T The type of the value to add /// @param _name The name of the field in the parent object /// @param _var The value to add to the object - /// @param _parent Pointer to the parent object (unused as encoder tracks context) + /// @param _parent Pointer to the parent object (unused as encoder tracks + /// context) /// @return An output variable representing the added value template OutputVarType add_value_to_object(const std::string_view& _name, @@ -155,19 +165,25 @@ class RFL_API Writer { OutputVarType new_value(const T& _var) const { if constexpr (std::is_same, std::string>()) { encoder_->string_value(_var); + } else if constexpr (std::is_same, rfl::Bytestring>() || std::is_same, rfl::Vectorstring>()) { encoder_->byte_string_value(_var); + } else if constexpr (std::is_same, bool>()) { encoder_->bool_value(_var); + } else if constexpr (std::is_floating_point>()) { encoder_->double_value(static_cast(_var)); + } else if constexpr (std::is_unsigned>()) { encoder_->uint64_value(static_cast(_var)); + } else if constexpr (std::is_integral>()) { encoder_->int64_value(static_cast(_var)); + } else { static_assert(rfl::always_false_v, "Unsupported type."); } diff --git a/include/rfl/cereal/Writer.hpp b/include/rfl/cereal/Writer.hpp index ee6e6ebb6..ec20478d5 100644 --- a/include/rfl/cereal/Writer.hpp +++ b/include/rfl/cereal/Writer.hpp @@ -19,7 +19,8 @@ namespace rfl::cereal { /// Writer for Cereal serialization library integration. -/// Writes data to Cereal's portable binary archive format for cross-platform serialization. +/// Writes data to Cereal's portable binary archive format for cross-platform +/// serialization. class Writer { public: using CerealArchive = ::cereal::PortableBinaryOutputArchive; diff --git a/include/rfl/flexbuf/Writer.hpp b/include/rfl/flexbuf/Writer.hpp index 47ba42b8b..36cbef141 100644 --- a/include/rfl/flexbuf/Writer.hpp +++ b/include/rfl/flexbuf/Writer.hpp @@ -14,8 +14,7 @@ #include "../always_false.hpp" #include "../common.hpp" -namespace rfl { -namespace flexbuf { +namespace rfl::flexbuf { struct RFL_API Writer { struct OutputArray { @@ -89,17 +88,28 @@ struct RFL_API Writer { const T& _var) const { if constexpr (std::is_same, std::string>()) { fbb_->String(_name.data(), _var); + } else if constexpr (std::is_same, rfl::Bytestring>() || std::is_same, rfl::Vectorstring>()) { fbb_->Blob(_name.data(), _var.data(), _var.size()); + } else if constexpr (std::is_same, bool>()) { fbb_->Bool(_name.data(), _var); + + } else if constexpr (std::is_same, float>()) { + fbb_->Float(_name.data(), _var); + + } else if constexpr (std::is_same, double>()) { + fbb_->Double(_name.data(), _var); + } else if constexpr (std::is_floating_point>()) { fbb_->Double(_name.data(), _var); + } else if constexpr (std::is_integral>()) { fbb_->Int(_name.data(), _var); + } else { static_assert(always_false_v, "Unsupported type"); } @@ -110,15 +120,26 @@ struct RFL_API Writer { OutputVarType insert_value(const T& _var) const { if constexpr (std::is_same, std::string>()) { fbb_->String(_var); + } else if constexpr (std::is_same, rfl::Bytestring>()) { fbb_->Blob(_var.data(), _var.size()); + } else if constexpr (std::is_same, bool>()) { fbb_->Bool(_var); + + } else if constexpr (std::is_same, float>()) { + fbb_->Float(_var); + + } else if constexpr (std::is_same, double>()) { + fbb_->Double(_var); + } else if constexpr (std::is_floating_point>()) { fbb_->Double(_var); + } else if constexpr (std::is_integral>()) { fbb_->Int(_var); + } else { static_assert(always_false_v, "Unsupported type"); } @@ -137,7 +158,6 @@ struct RFL_API Writer { Ref fbb_; }; -} // namespace flexbuf -} // namespace rfl +} // namespace rfl::flexbuf #endif diff --git a/include/rfl/ubjson/Writer.hpp b/include/rfl/ubjson/Writer.hpp index 0ef7eba3f..5f0714b84 100644 --- a/include/rfl/ubjson/Writer.hpp +++ b/include/rfl/ubjson/Writer.hpp @@ -95,17 +95,22 @@ class RFL_API Writer { OutputVarType new_value(const T& _var) const { if constexpr (std::is_same, std::string>()) { encoder_->string_value(_var); + } else if constexpr (std::is_same, rfl::Bytestring>() || std::is_same, rfl::Vectorstring>()) { encoder_->byte_string_value(_var); + } else if constexpr (std::is_same, bool>()) { encoder_->bool_value(_var); + } else if constexpr (std::is_floating_point>()) { encoder_->double_value(static_cast(_var)); + } else if constexpr (std::is_integral>()) { encoder_->int64_value(static_cast(_var)); + } else { static_assert(rfl::always_false_v, "Unsupported type."); } diff --git a/tests/avro/test_floating_point_types.cpp b/tests/avro/test_floating_point_types.cpp new file mode 100644 index 000000000..51127166b --- /dev/null +++ b/tests/avro/test_floating_point_types.cpp @@ -0,0 +1,25 @@ +#include + +#include +#include + +namespace test_floating_point_types { + +struct FloatAndDoubleStruct { + float f; + double d; +}; + +TEST(avro, writes_float_and_double) { + const FloatAndDoubleStruct s{1.0f, 2.0}; + + const auto bytes = rfl::avro::write(s); + const auto res = rfl::avro::read(bytes); + + EXPECT_TRUE(res && true) << "Error: " << res.error().what(); + + EXPECT_EQ(res.value().f, s.f); + EXPECT_EQ(res.value().d, s.d); +} + +} // namespace test_floating_point_types diff --git a/tests/boost_serialization/test_floating_point_types.cpp b/tests/boost_serialization/test_floating_point_types.cpp new file mode 100644 index 000000000..dc7921a5f --- /dev/null +++ b/tests/boost_serialization/test_floating_point_types.cpp @@ -0,0 +1,24 @@ +#include + +#include + +namespace test_floating_point_types { + +struct FloatAndDoubleStruct { + float f; + double d; +}; + +TEST(boost_serialization, writes_float_and_double) { + const FloatAndDoubleStruct s{1.0f, 2.0}; + + const auto bytes = rfl::boost_serialization::write(s); + const auto res = rfl::boost_serialization::read(bytes); + + EXPECT_TRUE(res && true) << "Error: " << res.error().what(); + + EXPECT_EQ(res.value().f, s.f); + EXPECT_EQ(res.value().d, s.d); +} + +} // namespace test_floating_point_types diff --git a/tests/bson/test_floating_point_types.cpp b/tests/bson/test_floating_point_types.cpp new file mode 100644 index 000000000..e071a88c1 --- /dev/null +++ b/tests/bson/test_floating_point_types.cpp @@ -0,0 +1,24 @@ +#include + +#include + +namespace test_floating_point_types { + +struct FloatAndDoubleStruct { + float f; + double d; +}; + +TEST(bson, writes_float_and_double) { + const FloatAndDoubleStruct s{1.0f, 2.0}; + + const auto bytes = rfl::bson::write(s); + const auto res = rfl::bson::read(bytes); + + EXPECT_TRUE(res && true) << "Error: " << res.error().what(); + + EXPECT_EQ(res.value().f, s.f); + EXPECT_EQ(res.value().d, s.d); +} + +} // namespace test_floating_point_types diff --git a/tests/capnproto/test_floating_point_types.cpp b/tests/capnproto/test_floating_point_types.cpp new file mode 100644 index 000000000..21c074765 --- /dev/null +++ b/tests/capnproto/test_floating_point_types.cpp @@ -0,0 +1,24 @@ +#include + +#include + +namespace test_floating_point_types { + +struct FloatAndDoubleStruct { + float f; + double d; +}; + +TEST(capnproto, writes_float_and_double) { + const FloatAndDoubleStruct s{1.0f, 2.0}; + + const auto bytes = rfl::capnproto::write(s); + const auto res = rfl::capnproto::read(bytes); + + EXPECT_TRUE(res && true) << "Error: " << res.error().what(); + + EXPECT_EQ(res.value().f, s.f); + EXPECT_EQ(res.value().d, s.d); +} + +} // namespace test_floating_point_types diff --git a/tests/cbor/test_floating_point_types.cpp b/tests/cbor/test_floating_point_types.cpp new file mode 100644 index 000000000..baee9ef03 --- /dev/null +++ b/tests/cbor/test_floating_point_types.cpp @@ -0,0 +1,24 @@ +#include + +#include + +namespace test_floating_point_types { + +struct FloatAndDoubleStruct { + float f; + double d; +}; + +TEST(cbor, writes_float_and_double) { + const FloatAndDoubleStruct s{1.0f, 2.0}; + + const auto bytes = rfl::cbor::write(s); + const auto res = rfl::cbor::read(bytes); + + EXPECT_TRUE(res && true) << "Error: " << res.error().what(); + + EXPECT_EQ(res.value().f, s.f); + EXPECT_EQ(res.value().d, s.d); +} + +} // namespace test_floating_point_types diff --git a/tests/cereal/test_floating_point_types.cpp b/tests/cereal/test_floating_point_types.cpp new file mode 100644 index 000000000..fd37cc5af --- /dev/null +++ b/tests/cereal/test_floating_point_types.cpp @@ -0,0 +1,24 @@ +#include + +#include + +namespace test_floating_point_types { + +struct FloatAndDoubleStruct { + float f; + double d; +}; + +TEST(cereal, writes_float_and_double) { + const FloatAndDoubleStruct s{1.0f, 2.0}; + + const auto bytes = rfl::cereal::write(s); + const auto res = rfl::cereal::read(bytes); + + EXPECT_TRUE(res && true) << "Error: " << res.error().what(); + + EXPECT_EQ(res.value().f, s.f); + EXPECT_EQ(res.value().d, s.d); +} + +} // namespace test_floating_point_types diff --git a/tests/flexbuffers/test_floating_point_types.cpp b/tests/flexbuffers/test_floating_point_types.cpp new file mode 100644 index 000000000..ad86f11ac --- /dev/null +++ b/tests/flexbuffers/test_floating_point_types.cpp @@ -0,0 +1,24 @@ +#include + +#include + +namespace test_floating_point_types { + +struct FloatAndDoubleStruct { + float f; + double d; +}; + +TEST(flexbuf, writes_float_and_double) { + const FloatAndDoubleStruct s{1.0f, 2.0}; + + const auto bytes = rfl::flexbuf::write(s); + const auto res = rfl::flexbuf::read(bytes); + + EXPECT_TRUE(res && true) << "Error: " << res.error().what(); + + EXPECT_EQ(res.value().f, s.f); + EXPECT_EQ(res.value().d, s.d); +} + +} // namespace test_floating_point_types diff --git a/tests/ubjson/test_floating_point_types.cpp b/tests/ubjson/test_floating_point_types.cpp new file mode 100644 index 000000000..98b2791d1 --- /dev/null +++ b/tests/ubjson/test_floating_point_types.cpp @@ -0,0 +1,24 @@ +#include + +#include + +namespace test_floating_point_types { + +struct FloatAndDoubleStruct { + float f; + double d; +}; + +TEST(ubjson, writes_float_and_double) { + const FloatAndDoubleStruct s{1.0f, 2.0}; + + const auto bytes = rfl::ubjson::write(s); + const auto res = rfl::ubjson::read(bytes); + + EXPECT_TRUE(res && true) << "Error: " << res.error().what(); + + EXPECT_EQ(res.value().f, s.f); + EXPECT_EQ(res.value().d, s.d); +} + +} // namespace test_floating_point_types diff --git a/tests/yas/test_floating_point_types.cpp b/tests/yas/test_floating_point_types.cpp new file mode 100644 index 000000000..b8264c499 --- /dev/null +++ b/tests/yas/test_floating_point_types.cpp @@ -0,0 +1,24 @@ +#include + +#include + +namespace test_floating_point_types { + +struct FloatAndDoubleStruct { + float f; + double d; +}; + +TEST(yas, writes_float_and_double) { + const FloatAndDoubleStruct s{1.0f, 2.0}; + + const auto bytes = rfl::yas::write(s); + const auto res = rfl::yas::read(bytes); + + EXPECT_TRUE(res && true) << "Error: " << res.error().what(); + + EXPECT_EQ(res.value().f, s.f); + EXPECT_EQ(res.value().d, s.d); +} + +} // namespace test_floating_point_types