From 245b2a9078c4c7d9c65b68256af0727b265660f6 Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Tue, 16 Mar 2021 10:41:22 +0100 Subject: [PATCH] Big-endian test case fixes: big-integer numerics * Fix endian assumptions in CtorByteArray tests (the byte array must always be in little-endian byte order) * Fix endian assumptions in left-/right-shift tests (byte array representing shift count must be in little-endian) --- .../tests/BigInteger/ctor.cs | 44 ++++++++++++++++++- .../tests/BigInteger/op_leftshift.cs | 8 ++++ .../tests/BigInteger/op_rightshift.cs | 8 ++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/ctor.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/ctor.cs index a980cc9b723a3a..83b1ebd8673a81 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigInteger/ctor.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/ctor.cs @@ -677,6 +677,10 @@ public static void RunCtorByteArrayTests() // ctor(byte[]): array is 1 byte tempUInt64 = (uint)s_random.Next(0, 256); tempByteArray = BitConverter.GetBytes(tempUInt64); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray); + } if (tempByteArray[0] > 127) { VerifyCtorByteArray(new byte[] { tempByteArray[0] }); @@ -719,6 +723,10 @@ public static void RunCtorByteArrayTests() { tempUInt64 = unchecked((uint)s_random.Next(int.MinValue, int.MaxValue)); tempByteArray = BitConverter.GetBytes(tempUInt64); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray); + } VerifyCtorByteArray( new byte[] { @@ -741,6 +749,10 @@ public static void RunCtorByteArrayTests() { tempUInt64 = unchecked((uint)s_random.Next(int.MinValue, int.MaxValue)); tempByteArray = BitConverter.GetBytes(tempUInt64); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray); + } if (tempUInt64 > int.MaxValue) { @@ -781,6 +793,10 @@ public static void RunCtorByteArrayTests() tempUInt64 <<= 8; tempUInt64 += (ulong)s_random.Next(0, 256); tempByteArray = BitConverter.GetBytes(tempUInt64); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray); + } if (tempUInt64 >= (ulong)0x00080000) { @@ -823,6 +839,10 @@ public static void RunCtorByteArrayTests() tempUInt64 <<= 32; tempUInt64 += unchecked((uint)s_random.Next(int.MinValue, int.MaxValue)); tempByteArray = BitConverter.GetBytes(tempUInt64); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray); + } if (tempUInt64 > long.MaxValue) { @@ -1073,11 +1093,21 @@ private static void VerifyCtorByteArray(byte[] value) tempBigInteger = tempBigInteger + (new BigInteger(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0xFF })); } + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray); + } Assert.Equal(BitConverter.ToInt64(tempByteArray, 0), (long)tempBigInteger); } else { - Assert.Equal(BitConverter.ToInt64(value, 0), (long)bigInteger); + byte[] tempByteArray = new byte[8]; + Array.Copy(value, tempByteArray, 8); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray); + } + Assert.Equal(BitConverter.ToInt64(tempByteArray, 0), (long)bigInteger); } if (IsOutOfRangeUInt64(value)) @@ -1108,11 +1138,21 @@ private static void VerifyCtorByteArray(byte[] value) } } + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray); + } Assert.Equal(BitConverter.ToUInt64(tempByteArray, 0), (ulong)tempBigInteger); } else { - Assert.Equal(BitConverter.ToUInt64(value, 0), (ulong)bigInteger); + byte[] tempByteArray = new byte[8]; + Array.Copy(value, tempByteArray, 8); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray); + } + Assert.Equal(BitConverter.ToUInt64(tempByteArray, 0), (ulong)bigInteger); } VerifyBigIntegerUsingIdentities(bigInteger, isZero); diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_leftshift.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_leftshift.cs index 3beccaa84b1c78..47b0a8ea7678cd 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_leftshift.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_leftshift.cs @@ -131,6 +131,10 @@ public static void RunLeftShiftTests() { tempByteArray1 = GetRandomPosByteArray(s_random, 100); tempByteArray2 = BitConverter.GetBytes(s_random.Next(-1000, -8 * tempByteArray1.Length)); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray2); + } VerifyLeftShiftString(Print(tempByteArray2) + Print(tempByteArray1) + "b<<"); } @@ -139,6 +143,10 @@ public static void RunLeftShiftTests() { tempByteArray1 = GetRandomNegByteArray(s_random, 100); tempByteArray2 = BitConverter.GetBytes(s_random.Next(-1000, -8 * tempByteArray1.Length)); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray2); + } VerifyLeftShiftString(Print(tempByteArray2) + Print(tempByteArray1) + "b<<"); } } diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_rightshift.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_rightshift.cs index 7939efc23043f3..5107f0ed7d7671 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_rightshift.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_rightshift.cs @@ -146,6 +146,10 @@ public static void RunRightShiftTests() { tempByteArray1 = GetRandomPosByteArray(s_random, 100); tempByteArray2 = BitConverter.GetBytes(s_random.Next(8 * tempByteArray1.Length, 1000)); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray2); + } VerifyRightShiftString(Print(tempByteArray2) + Print(tempByteArray1) + "b>>"); } @@ -154,6 +158,10 @@ public static void RunRightShiftTests() { tempByteArray1 = GetRandomNegByteArray(s_random, 100); tempByteArray2 = BitConverter.GetBytes(s_random.Next(8 * tempByteArray1.Length, 1000)); + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(tempByteArray2); + } VerifyRightShiftString(Print(tempByteArray2) + Print(tempByteArray1) + "b>>"); } }