Skip to content

Commit

Permalink
Fix as_bytes() test for big endian targets (#34)
Browse files Browse the repository at this point in the history
The tests for the `as_bytes` method assumed that bytes are stored in
little endian order, which is not the case for PowerPC. Fix the alignment
tests so that they work on both little and big endian targets.

Fixes #23

Co-authored-by: Francesco Zardi <frazar@users.noreply.github.com>
  • Loading branch information
frazar and frazar authored Oct 9, 2022
1 parent 94676c6 commit 49fc9ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ jobs:
# Miri (and wouldn't want to do anyway).
#
# TODO(#21): Fix `test_new_error` on i686 and re-enable it here.
# TODO(#23): Fix `test_as_bytes_methods` on powerpc and re-enable it here.
run: cargo +${{ matrix.channel }} miri test --target ${{ matrix.target }} --features "${{ matrix.features }}" -- --skip ui ${{ contains(matrix.target, 'i686') && '--skip test_new_error' || '' }} ${{ contains(matrix.target, 'powerpc') && '--skip test_as_bytes_methods' || '' }}
run: cargo +${{ matrix.channel }} miri test --target ${{ matrix.target }} --features "${{ matrix.features }}" -- --skip ui ${{ contains(matrix.target, 'i686') && '--skip test_new_error' || '' }}
# Only nightly has a working Miri, so we skip installing on all other
# toolchains.
#
Expand Down
36 changes: 27 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2872,31 +2872,49 @@ mod tests {
}

let mut foo = Foo { a: 1, b: Wrapping(2), c: None };

// Test that we can access the underlying bytes, and that we get the
// right bytes and the right number of bytes.
assert_eq!(foo.as_bytes(), [1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]);
let expected: Vec<u8> = if cfg!(target_endian = "little") {
vec![1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]
} else {
vec![0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0]
};
assert_eq!(foo.as_bytes(), expected.as_bytes());

// Test that changes to the underlying byte slices are reflected in the
// original object.
foo.as_bytes_mut()[0] = 3;
assert_eq!(foo, Foo { a: 3, b: Wrapping(2), c: None });
let expected_a = if cfg!(target_endian = "little") { 0x00_00_00_03 } else { 0x03_00_00_01 };
assert_eq!(foo, Foo { a: expected_a, b: Wrapping(2), c: None });

// Do the same tests for a slice, which ensures that this logic works
// for unsized types as well.
let foo = &mut [
Foo { a: 1, b: Wrapping(2), c: None },
Foo { a: 3, b: Wrapping(4), c: NonZeroU32::new(1) },
];
assert_eq!(
foo.as_bytes(),
[1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0]
);
let expected = if cfg!(target_endian = "little") {
vec![1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0]
} else {
vec![0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 1]
};
assert_eq!(foo.as_bytes(), expected);

foo.as_bytes_mut()[8] = 5;
foo.as_bytes_mut()[16] = 6;
let expected_c_1 = NonZeroU32::new(if cfg!(target_endian = "little") {
0x00_00_00_05
} else {
0x05_00_00_00
});
let expected_b_2 =
Wrapping(if cfg!(target_endian = "little") { 0x00_00_00_06 } else { 0x06_00_00_04 });
assert_eq!(
foo,
&mut [
Foo { a: 1, b: Wrapping(2), c: NonZeroU32::new(5) },
Foo { a: 3, b: Wrapping(6), c: NonZeroU32::new(1) }
&[
Foo { a: 1, b: Wrapping(2), c: expected_c_1 },
Foo { a: 3, b: expected_b_2, c: NonZeroU32::new(1) },
]
);
}
Expand Down

0 comments on commit 49fc9ff

Please sign in to comment.