From aa65ac264ee6e851a6aa26b1d9b7bd32fabe02e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:45:56 +0100 Subject: [PATCH] Add missing test case for BitCast BitCast lacked a test for the case of 2 structs with target requiring higher alignment than source, which could be problematic on platforms without unaligned loads. --- .../UnsafeTests.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libraries/System.Runtime/tests/System.Runtime.CompilerServices.Unsafe.Tests/UnsafeTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.CompilerServices.Unsafe.Tests/UnsafeTests.cs index 13488161781df5..c37b714aa6df69 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.CompilerServices.Unsafe.Tests/UnsafeTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.CompilerServices.Unsafe.Tests/UnsafeTests.cs @@ -1260,10 +1260,14 @@ public static unsafe void BitCast() Half h = Unsafe.ReadUnaligned(ref Unsafe.As(ref s2)); float s = Unsafe.ReadUnaligned(ref Unsafe.As(ref s4)); double d = Unsafe.ReadUnaligned(ref Unsafe.As(ref s8)); + (float, float) tf = Unsafe.ReadUnaligned<(float, float)>(ref Unsafe.As(ref s8)); + (uint, uint) ti = Unsafe.ReadUnaligned<(uint, uint)>(ref Unsafe.As(ref s8)); Assert.Equal(h, Unsafe.BitCast(s2)); Assert.Equal(s, Unsafe.BitCast(s4)); Assert.Equal(d, Unsafe.BitCast(s8)); + Assert.Equal(tf, Unsafe.BitCast(s8)); + Assert.Equal(ti, Unsafe.BitCast(s8)); *(S2*)misalignedPtr = s2; Assert.Equal(h, Unsafe.BitCast(*(S2*)misalignedPtr)); @@ -1271,6 +1275,8 @@ public static unsafe void BitCast() Assert.Equal(s, Unsafe.BitCast(*(S4*)misalignedPtr)); *(S8*)misalignedPtr = s8; Assert.Equal(d, Unsafe.BitCast(*(S8*)misalignedPtr)); + Assert.Equal(tf, Unsafe.BitCast(*(S8*)misalignedPtr)); + Assert.Equal(ti, Unsafe.BitCast(*(S8*)misalignedPtr)); NativeMemory.AlignedFree(misalignedPtr - 1); }