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

Fix as_bytes() test for big endian targets #34

Merged
merged 1 commit into from
Oct 9, 2022
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
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