diff --git a/include/rfl/cbor/Writer.hpp b/include/rfl/cbor/Writer.hpp index efd4ae7f..1003bcda 100644 --- a/include/rfl/cbor/Writer.hpp +++ b/include/rfl/cbor/Writer.hpp @@ -103,6 +103,8 @@ class Writer { 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 { diff --git a/tests/cbor/test_integers.cpp b/tests/cbor/test_integers.cpp new file mode 100644 index 00000000..29e0637e --- /dev/null +++ b/tests/cbor/test_integers.cpp @@ -0,0 +1,39 @@ +#include + +#include + +namespace test_integers { + +TEST(cbor, test_integers_signedness) { + + static constexpr uint64_t BIG_INT = 0xffffffffffffffff; + + struct Unsigned { + uint64_t u64; + }; + + struct Signed { + int64_t i64; + }; + + std::vector unsigned_buffer = rfl::cbor::write(Unsigned{BIG_INT}); + std::vector unsigned_expected = { + 0xA1, 0x63, 0x75, 0x36, 0x34, + 0x1B, // Per RFC 8949, Initial byte '0x1B' indicates "unsigned integer (eight-byte uint64_t follows)" + // See: https://www.rfc-editor.org/rfc/rfc8949.html#section-appendix.b + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff + }; + + EXPECT_EQ(std::vector(unsigned_expected.begin(), unsigned_expected.end()), unsigned_buffer); + + std::vector signed_buffer = rfl::cbor::write(Signed{static_cast(BIG_INT)}); + std::vector signed_expected = { + 0xA1, 0x63, 0x69, 0x36, 0x34, + 0x20 // Per RFC 8949, Initial byte '0x20' indicates "negative integer -1-0x00..-1-0x17 (-1..-24)" + // See: https://www.rfc-editor.org/rfc/rfc8949.html#section-appendix.b + }; + + EXPECT_EQ(std::vector(signed_expected.begin(), signed_expected.end()), signed_buffer); +} + +} // namespace test_integers