From 49fc9ff2f1cc2e428be29023850f28994c9a8cf1 Mon Sep 17 00:00:00 2001 From: frazar <914950+frazar@users.noreply.github.com> Date: Sun, 9 Oct 2022 23:08:37 +0200 Subject: [PATCH] Fix as_bytes() test for big endian targets (#34) 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 --- .github/workflows/ci.yml | 3 +-- src/lib.rs | 36 +++++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 00f83a0d59..1d2f3d734c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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. # diff --git a/src/lib.rs b/src/lib.rs index ac8f6e241d..e7ca2c9a73 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2872,13 +2872,21 @@ 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 = 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. @@ -2886,17 +2894,27 @@ mod tests { 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) }, ] ); }