From 64a45d6a226fe7963cc5aae4318a409882205a24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Capek?= Date: Mon, 17 Sep 2018 12:17:05 +0200 Subject: [PATCH 1/2] Add test case demonstrating broken bit positions for integers > 1 byte There are 2 issues with this: - the BitIntsLsb struct is serialized in Msb-first order - val1 and val2 serialization is also broken --- .../tests/packing_bit_positioning.rs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packed_struct_tests/tests/packing_bit_positioning.rs b/packed_struct_tests/tests/packing_bit_positioning.rs index 766cf82..06f3b7f 100644 --- a/packed_struct_tests/tests/packing_bit_positioning.rs +++ b/packed_struct_tests/tests/packing_bit_positioning.rs @@ -60,6 +60,32 @@ fn test_packing_bit_positions_lsb() { } +#[derive(PackedStruct, PartialEq, Debug)] +#[packed_struct(size_bytes="4", bit_numbering="lsb0", endian="lsb")] +pub struct BigIntsLsb { + #[packed_field(bits="2:0")] + pub val1: Integer, + #[packed_field(bits="6")] + pub val2: bool, + #[packed_field(bits="31:16")] + pub val3: Integer, +} + +#[test] +fn test_packing_bit_positions_bigints_lsb() { + let a = BigIntsLsb { + val1: 7.into(), + val2: true, + val3: 0xbeef.into(), + }; + + let packed = a.pack(); + assert_eq!(&[0x07u8, 0x04, 0xef, 0xbe], &packed); + + let unpacked = BigIntsLsb::unpack(&packed).unwrap(); + assert_eq!(a, unpacked); +} + #[test] fn test_packing_byte_position() { From 46f5fa1b645bcbc2be487bbd3fd5af1039ebbe97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Capek?= Date: Thu, 27 Sep 2018 21:04:49 +0200 Subject: [PATCH 2/2] Change lsb0 bit allocations to make it fit the desired LSB endianness. --- packed_struct_tests/tests/packing_bit_positioning.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packed_struct_tests/tests/packing_bit_positioning.rs b/packed_struct_tests/tests/packing_bit_positioning.rs index 06f3b7f..8245943 100644 --- a/packed_struct_tests/tests/packing_bit_positioning.rs +++ b/packed_struct_tests/tests/packing_bit_positioning.rs @@ -63,15 +63,16 @@ fn test_packing_bit_positions_lsb() { #[derive(PackedStruct, PartialEq, Debug)] #[packed_struct(size_bytes="4", bit_numbering="lsb0", endian="lsb")] pub struct BigIntsLsb { - #[packed_field(bits="2:0")] + #[packed_field(bits="26:24")] pub val1: Integer, - #[packed_field(bits="6")] + #[packed_field(bits="18")] pub val2: bool, - #[packed_field(bits="31:16")] + #[packed_field(bits="15:0")] pub val3: Integer, } #[test] +/// This test should verify the packing/unpacking a hypothetical 32-bit register that contains 0xbeef0407 fn test_packing_bit_positions_bigints_lsb() { let a = BigIntsLsb { val1: 7.into(), @@ -80,7 +81,7 @@ fn test_packing_bit_positions_bigints_lsb() { }; let packed = a.pack(); - assert_eq!(&[0x07u8, 0x04, 0xef, 0xbe], &packed); + assert_eq!(&[0x07u8, 0x04, 0xef, 0xbe], &packed, "mismatch received: {}", a); let unpacked = BigIntsLsb::unpack(&packed).unwrap(); assert_eq!(a, unpacked);