Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated unittests to mostly patch assumptions on big-endian. #46

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u16>(), 0x1234u16);
assert_eq!(
&bytes.as_slice()[.. 3],
Expand All @@ -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::<u16>(), 0x1234u16);
assert_eq!(
&bytes.as_slice()[.. 3],
Expand Down Expand Up @@ -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::<u16>(), 0x1234u16);
assert_eq!(
&bytes.as_slice()[.. 3],
Expand All @@ -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::<u16>(), 0x1234u16);
assert_eq!(
&bytes.as_slice()[.. 3],
Expand Down
62 changes: 50 additions & 12 deletions src/slice/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,63 @@ fn any() {
fn count_ones() {
assert_eq!(BitSlice::<Local, usize>::empty().count_ones(), 0);
assert_eq!(0xA5u8.bits::<Local>()[1 ..][.. 6].count_ones(), 2);
assert_eq!(
[0x0Fu8, !0, 0xF0].bits::<Local>()[2 ..][.. 20].count_ones(),
12
);
assert_eq!([0x0Fu8, !0].bits::<Local>()[2 ..].count_ones(), 10);
assert_eq!([!0u8, 0xF0].bits::<Local>()[.. 14].count_ones(), 10);

// BE: bits=BitSlice<Msb0, u8> [00001111, 11111111, 11110000] = 16
// LE: bits=BitSlice<Lsb0, u8> [11110000, 11111111, 00001111] = 12
let ones = [0x0Fu8, !0, 0xF0].bits::<Local>()[2 ..][.. 20].count_ones();
#[cfg(target_endian = "little")]
assert_eq!(ones, 12);
#[cfg(target_endian = "big")]
assert_eq!(ones, 16);

// BE: bits=BitSlice<Msb0, u8> [00001111, 11111111] = 12
// LE: bits=BitSlice<Lsb0, u8> [11111111, 00001111] = 10
let ones = [0x0Fu8, !0].bits::<Local>()[2 ..].count_ones();
#[cfg(target_endian = "little")]
assert_eq!(ones, 10);
#[cfg(target_endian = "big")]
assert_eq!(ones, 12);

// BE: bits=BitSlice<Msb0, u8> [11111111, 11110000] = 12
// LE: bits=BitSlice<Lsb0, u8> [11110000, 11111111] = 10
let ones = [!0u8, 0xF0].bits::<Local>()[.. 14].count_ones();
#[cfg(target_endian = "little")]
assert_eq!(ones, 10);
#[cfg(target_endian = "big")]
assert_eq!(ones, 12);

assert_eq!((!0u8).bits::<Local>().count_ones(), 8);
}

#[test]
fn count_zeros() {
assert_eq!(BitSlice::<Local, usize>::empty().count_zeros(), 0);
assert_eq!(0xA5u8.bits::<Local>()[1 ..][.. 6].count_zeros(), 4);
assert_eq!(
[0xF0u8, 0, 0x0F].bits::<Local>()[2 ..][.. 20].count_zeros(),
12
);
assert_eq!([0xF0u8, 0].bits::<Local>()[2 ..].count_zeros(), 10);
assert_eq!([0u8, 0x0F].bits::<Local>()[.. 14].count_zeros(), 10);

// BE: bits=BitSlice<Lsb0, u8> [11110000, 00000000, 00001111] = 16
// LE: bits=BitSlice<Msb0, u8> [00001111, 00000000, 11110000] = 11
let zeros = [0xF0u8, 0, 0x0F].bits::<Local>()[2 ..][.. 20].count_zeros();
#[cfg(target_endian = "little")]
assert_eq!(zeros, 12);
#[cfg(target_endian = "big")]
assert_eq!(zeros, 16);

// BE: bits=BitSlice<Msb0, u8> [11110000, 00000000] = 12
// LE: bits=BitSlice<Lsb0, u8> [00000000, 11110000] = 10
let zeros = [0xF0u8, 0].bits::<Local>()[2 ..].count_zeros();
#[cfg(target_endian = "little")]
assert_eq!(zeros, 10);
#[cfg(target_endian = "big")]
assert_eq!(zeros, 12);

// BE: bits=BitSlice<Msb0, u8> [00000000, 00001111] = 12
// LE: bits=BitSlice<Lsb0, u8> [00001111, 00000000] = 10
let zeros = [0u8, 0x0F].bits::<Local>()[.. 14].count_zeros();
#[cfg(target_endian = "little")]
assert_eq!(zeros, 10);
#[cfg(target_endian = "big")]
assert_eq!(zeros, 12);

assert_eq!(0u8.bits::<Local>().count_zeros(), 8);
}

Expand Down