mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
chore: Add uint8_t
version of bin_pack
for numbers.
This was the only one missing. Also added a test for integer conversions: lossy ones are rejected, lossless ones are ok.
This commit is contained in:
parent
015305a088
commit
ee42a5ca05
|
@ -102,6 +102,11 @@ bool bin_pack_bool(Bin_Pack *bp, bool val)
|
|||
return cmp_write_bool(&bp->ctx, val);
|
||||
}
|
||||
|
||||
bool bin_pack_u08(Bin_Pack *bp, uint8_t val)
|
||||
{
|
||||
return cmp_write_uinteger(&bp->ctx, val);
|
||||
}
|
||||
|
||||
bool bin_pack_u16(Bin_Pack *bp, uint16_t val)
|
||||
{
|
||||
return cmp_write_uinteger(&bp->ctx, val);
|
||||
|
|
|
@ -82,6 +82,8 @@ bool bin_pack_array(Bin_Pack *bp, uint32_t size);
|
|||
|
||||
/** @brief Pack a MessagePack bool. */
|
||||
non_null() bool bin_pack_bool(Bin_Pack *bp, bool val);
|
||||
/** @brief Pack a `uint8_t` as MessagePack positive integer. */
|
||||
non_null() bool bin_pack_u08(Bin_Pack *bp, uint8_t val);
|
||||
/** @brief Pack a `uint16_t` as MessagePack positive integer. */
|
||||
non_null() bool bin_pack_u16(Bin_Pack *bp, uint16_t val);
|
||||
/** @brief Pack a `uint32_t` as MessagePack positive integer. */
|
||||
|
|
|
@ -44,4 +44,45 @@ TEST(BinPack, PackedUint64CanBeUnpacked)
|
|||
EXPECT_EQ(val, 1234567812345678LL);
|
||||
}
|
||||
|
||||
TEST(BinPack, MsgPackedUint8CanBeUnpackedAsUint32)
|
||||
{
|
||||
std::array<uint8_t, 2> buf;
|
||||
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bp, nullptr);
|
||||
ASSERT_TRUE(bin_pack_u08(bp.get(), 123));
|
||||
|
||||
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bu, nullptr);
|
||||
uint32_t val;
|
||||
ASSERT_TRUE(bin_unpack_u32(bu.get(), &val));
|
||||
EXPECT_EQ(val, 123);
|
||||
}
|
||||
|
||||
TEST(BinPack, MsgPackedUint32CanBeUnpackedAsUint8IfSmallEnough)
|
||||
{
|
||||
std::array<uint8_t, 2> buf;
|
||||
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bp, nullptr);
|
||||
ASSERT_TRUE(bin_pack_u32(bp.get(), 123));
|
||||
|
||||
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bu, nullptr);
|
||||
uint8_t val;
|
||||
ASSERT_TRUE(bin_unpack_u08(bu.get(), &val));
|
||||
EXPECT_EQ(val, 123);
|
||||
}
|
||||
|
||||
TEST(BinPack, LargeMsgPackedUint32CannotBeUnpackedAsUint8)
|
||||
{
|
||||
std::array<uint8_t, 5> buf;
|
||||
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bp, nullptr);
|
||||
ASSERT_TRUE(bin_pack_u32(bp.get(), 1234567));
|
||||
|
||||
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bu, nullptr);
|
||||
uint8_t val;
|
||||
EXPECT_FALSE(bin_unpack_u08(bu.get(), &val));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
Loading…
Reference in New Issue
Block a user