diff --git a/src/fields.rs b/src/fields.rs index a58a5f21..97911ab2 100644 --- a/src/fields.rs +++ b/src/fields.rs @@ -994,8 +994,7 @@ mod tests { // expected byte pattern: 0x34 0x12 // bits: 0011_0100 __01_0010 // idx: 7654 3210 fedc ba98 - let u16b = u16::from_ne_bytes(0x1234u16.to_le_bytes()); - bytes[5 ..][.. 14].store_le(u16b); + bytes[5 ..][.. 14].store_le(0x1234u16); assert_eq!(bytes[5 ..][.. 14].load_le::(), 0x1234u16); assert_eq!( &bytes.as_slice()[.. 3], @@ -1004,7 +1003,7 @@ mod tests { ); // the load/store orderings only affect the order of elements, not of // bits within the element. - bytes[5 ..][.. 14].store_be(u16b); + bytes[5 ..][.. 14].store_be(0x1234u16); assert_eq!(bytes[5 ..][.. 14].load_be::(), 0x1234u16); assert_eq!( &bytes.as_slice()[.. 3], @@ -1068,8 +1067,7 @@ mod tests { // expected byte pattern: 0x34 0x12 // bits: 0011_0100 __01_0010 // idx: 7654 3210 fedc ba98 - let u16b = u16::from_ne_bytes(0x1234u16.to_le_bytes()); - bytes[5 ..][.. 14].store_le(u16b); + bytes[5 ..][.. 14].store_le(0x1234u16); assert_eq!(bytes[5 ..][.. 14].load_le::(), 0x1234u16); assert_eq!( &bytes.as_slice()[.. 3], @@ -1078,7 +1076,7 @@ mod tests { ); // the load/store orderings only affect the order of elements, not of // bits within the element. - bytes[5 ..][.. 14].store_be(u16b); + bytes[5 ..][.. 14].store_be(0x1234u16); assert_eq!(bytes[5 ..][.. 14].load_be::(), 0x1234u16); assert_eq!( &bytes.as_slice()[.. 3], diff --git a/src/slice/tests.rs b/src/slice/tests.rs index 68f8700e..f030251c 100644 --- a/src/slice/tests.rs +++ b/src/slice/tests.rs @@ -72,12 +72,31 @@ fn any() { fn count_ones() { assert_eq!(BitSlice::::empty().count_ones(), 0); assert_eq!(0xA5u8.bits::()[1 ..][.. 6].count_ones(), 2); - assert_eq!( - [0x0Fu8, !0, 0xF0].bits::()[2 ..][.. 20].count_ones(), - 12 - ); - assert_eq!([0x0Fu8, !0].bits::()[2 ..].count_ones(), 10); - assert_eq!([!0u8, 0xF0].bits::()[.. 14].count_ones(), 10); + + // BE: bits=BitSlice [00001111, 11111111, 11110000] = 16 + // LE: bits=BitSlice [11110000, 11111111, 00001111] = 12 + let ones = [0x0Fu8, !0, 0xF0].bits::()[2 ..][.. 20].count_ones(); + #[cfg(target_endian = "little")] + assert_eq!(ones, 12); + #[cfg(target_endian = "big")] + assert_eq!(ones, 16); + + // BE: bits=BitSlice [00001111, 11111111] = 12 + // LE: bits=BitSlice [11111111, 00001111] = 10 + let ones = [0x0Fu8, !0].bits::()[2 ..].count_ones(); + #[cfg(target_endian = "little")] + assert_eq!(ones, 10); + #[cfg(target_endian = "big")] + assert_eq!(ones, 12); + + // BE: bits=BitSlice [11111111, 11110000] = 12 + // LE: bits=BitSlice [11110000, 11111111] = 10 + let ones = [!0u8, 0xF0].bits::()[.. 14].count_ones(); + #[cfg(target_endian = "little")] + assert_eq!(ones, 10); + #[cfg(target_endian = "big")] + assert_eq!(ones, 12); + assert_eq!((!0u8).bits::().count_ones(), 8); } @@ -85,12 +104,31 @@ fn count_ones() { fn count_zeros() { assert_eq!(BitSlice::::empty().count_zeros(), 0); assert_eq!(0xA5u8.bits::()[1 ..][.. 6].count_zeros(), 4); - assert_eq!( - [0xF0u8, 0, 0x0F].bits::()[2 ..][.. 20].count_zeros(), - 12 - ); - assert_eq!([0xF0u8, 0].bits::()[2 ..].count_zeros(), 10); - assert_eq!([0u8, 0x0F].bits::()[.. 14].count_zeros(), 10); + + // BE: bits=BitSlice [11110000, 00000000, 00001111] = 16 + // LE: bits=BitSlice [00001111, 00000000, 11110000] = 11 + let zeros = [0xF0u8, 0, 0x0F].bits::()[2 ..][.. 20].count_zeros(); + #[cfg(target_endian = "little")] + assert_eq!(zeros, 12); + #[cfg(target_endian = "big")] + assert_eq!(zeros, 16); + + // BE: bits=BitSlice [11110000, 00000000] = 12 + // LE: bits=BitSlice [00000000, 11110000] = 10 + let zeros = [0xF0u8, 0].bits::()[2 ..].count_zeros(); + #[cfg(target_endian = "little")] + assert_eq!(zeros, 10); + #[cfg(target_endian = "big")] + assert_eq!(zeros, 12); + + // BE: bits=BitSlice [00000000, 00001111] = 12 + // LE: bits=BitSlice [00001111, 00000000] = 10 + let zeros = [0u8, 0x0F].bits::()[.. 14].count_zeros(); + #[cfg(target_endian = "little")] + assert_eq!(zeros, 10); + #[cfg(target_endian = "big")] + assert_eq!(zeros, 12); + assert_eq!(0u8.bits::().count_zeros(), 8); }