Skip to content

Test case fixes for big-endian systems #48395

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

Closed
wants to merge 1 commit into from
Closed
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: 0 additions & 3 deletions src/libraries/Microsoft.VisualBasic.Core/tests/VBMathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ public void Randomize_SetsExpectedState()
{
ResetSeed();

if (!BitConverter.IsLittleEndian)
throw new NotImplementedException("big endian tests");

VBMath.Randomize(-2E30);
Assert.Equal(-0.0297851562f, VBMath.Rnd(0.0f));
VBMath.Randomize(-0.003356);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,13 @@ public void BinaryWriter_WriteSpan()

char testChar;

testChar = BitConverter.ToChar(new byte[] { (byte)baseStream.ReadByte(), (byte)baseStream.ReadByte() }, 0);
testChar = (char)((ushort)baseStream.ReadByte() + ((ushort)baseStream.ReadByte() << 8));
Assert.Equal('a', testChar);

testChar = BitConverter.ToChar(new byte[] { (byte)baseStream.ReadByte(), (byte)baseStream.ReadByte() }, 0);
testChar = (char)((ushort)baseStream.ReadByte() + ((ushort)baseStream.ReadByte() << 8));
Assert.Equal('7', testChar);

testChar = BitConverter.ToChar(new byte[] { (byte)baseStream.ReadByte(), (byte)baseStream.ReadByte() }, 0);
testChar = (char)((ushort)baseStream.ReadByte() + ((ushort)baseStream.ReadByte() << 8));
Assert.Equal(char.MaxValue, testChar);
}
}
Expand Down
29 changes: 18 additions & 11 deletions src/libraries/System.Memory/tests/Binary/BinaryReaderUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ public class BinaryReaderUnitTests
[Fact]
public void SpanRead()
{
Assert.True(BitConverter.IsLittleEndian);

ulong value = 0x8877665544332211; // [11 22 33 44 55 66 77 88]
ulong value; // [11 22 33 44 55 66 77 88]
if (BitConverter.IsLittleEndian)
{
value = 0x8877665544332211;
}
else
{
value = 0x1122334455667788;
}
Span<byte> span;
unsafe
{
Expand Down Expand Up @@ -113,9 +119,15 @@ public void SpanRead()
[Fact]
public void ReadOnlySpanRead()
{
Assert.True(BitConverter.IsLittleEndian);

ulong value = 0x8877665544332211; // [11 22 33 44 55 66 77 88]
ulong value; // [11 22 33 44 55 66 77 88]
if (BitConverter.IsLittleEndian)
{
value = 0x8877665544332211;
}
else
{
value = 0x1122334455667788;
}
ReadOnlySpan<byte> span;
unsafe
{
Expand Down Expand Up @@ -282,8 +294,6 @@ public void ReadOnlySpanReadFail()
[Fact]
public void SpanWriteAndReadBigEndianHeterogeneousStruct()
{
Assert.True(BitConverter.IsLittleEndian);

Span<byte> spanBE = new byte[Unsafe.SizeOf<TestStruct>()];

WriteInt16BigEndian(spanBE, s_testStruct.S0);
Expand Down Expand Up @@ -358,8 +368,6 @@ public void SpanWriteAndReadBigEndianHeterogeneousStruct()
[Fact]
public void SpanWriteAndReadLittleEndianHeterogeneousStruct()
{
Assert.True(BitConverter.IsLittleEndian);

Span<byte> spanLE = new byte[Unsafe.SizeOf<TestStruct>()];

WriteInt16LittleEndian(spanLE, s_testStruct.S0);
Expand Down Expand Up @@ -434,7 +442,6 @@ public void SpanWriteAndReadLittleEndianHeterogeneousStruct()
[Fact]
public void ReadingStructFieldByFieldOrReadAndReverseEndianness()
{
Assert.True(BitConverter.IsLittleEndian);
Span<byte> spanBE = new byte[Unsafe.SizeOf<TestHelpers.TestStructExplicit>()];

var testExplicitStruct = new TestHelpers.TestStructExplicit
Expand Down
11 changes: 0 additions & 11 deletions src/libraries/System.Memory/tests/Binary/BinaryWriterUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public class BinaryWriterUnitTests
[Fact]
public void SpanWrite()
{
Assert.True(BitConverter.IsLittleEndian);

Span<byte> span = new byte[8];

byte byteValue = 0x11;
Expand Down Expand Up @@ -92,7 +90,6 @@ public void SpanWrite()
[InlineData(0x00FF)]
public void SpanWriteInt16(short value)
{
Assert.True(BitConverter.IsLittleEndian);
var span = new Span<byte>(new byte[2]);
WriteInt16BigEndian(span, value);
short read = ReadInt16BigEndian(span);
Expand Down Expand Up @@ -121,7 +118,6 @@ public void SpanWriteInt16(short value)
[InlineData(0x00FF)]
public void SpanWriteUInt16(ushort value)
{
Assert.True(BitConverter.IsLittleEndian);
var span = new Span<byte>(new byte[2]);
WriteUInt16BigEndian(span, value);
ushort read = ReadUInt16BigEndian(span);
Expand Down Expand Up @@ -152,7 +148,6 @@ public void SpanWriteUInt16(ushort value)
[InlineData(0x000000FF)]
public void SpanWriteInt32(int value)
{
Assert.True(BitConverter.IsLittleEndian);
var span = new Span<byte>(new byte[4]);
WriteInt32BigEndian(span, value);
int read = ReadInt32BigEndian(span);
Expand Down Expand Up @@ -183,7 +178,6 @@ public void SpanWriteInt32(int value)
[InlineData(0x000000FF)]
public void SpanWriteUInt32(uint value)
{
Assert.True(BitConverter.IsLittleEndian);
var span = new Span<byte>(new byte[4]);
WriteUInt32BigEndian(span, value);
uint read = ReadUInt32BigEndian(span);
Expand Down Expand Up @@ -218,7 +212,6 @@ public void SpanWriteUInt32(uint value)
[InlineData(0x00000000000000FF)]
public void SpanWriteInt64(long value)
{
Assert.True(BitConverter.IsLittleEndian);
var span = new Span<byte>(new byte[8]);
WriteInt64BigEndian(span, value);
long read = ReadInt64BigEndian(span);
Expand Down Expand Up @@ -253,7 +246,6 @@ public void SpanWriteInt64(long value)
[InlineData(0x00000000000000FF)]
public void SpanWriteUInt64(ulong value)
{
Assert.True(BitConverter.IsLittleEndian);
var span = new Span<byte>(new byte[8]);
WriteUInt64BigEndian(span, value);
ulong read = ReadUInt64BigEndian(span);
Expand Down Expand Up @@ -290,7 +282,6 @@ public static IEnumerable<object[]> SpanWriteHalf_TestData()
[MemberData(nameof(SpanWriteHalf_TestData))]
public void SpanWriteHalf(Half value)
{
Assert.True(BitConverter.IsLittleEndian);
var span = new Span<byte>(new byte[4]);
WriteHalfBigEndian(span, value);
Half read = ReadHalfBigEndian(span);
Expand Down Expand Up @@ -321,7 +312,6 @@ public void SpanWriteHalf(Half value)
[InlineData(float.NaN)]
public void SpanWriteSingle(float value)
{
Assert.True(BitConverter.IsLittleEndian);
var span = new Span<byte>(new byte[4]);
WriteSingleBigEndian(span, value);
float read = ReadSingleBigEndian(span);
Expand Down Expand Up @@ -352,7 +342,6 @@ public void SpanWriteSingle(float value)
[InlineData(double.NaN)]
public void SpanWriteDouble(double value)
{
Assert.True(BitConverter.IsLittleEndian);
var span = new Span<byte>(new byte[8]);
WriteDoubleBigEndian(span, value);
double read = ReadDoubleBigEndian(span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ public static partial class MemoryMarshalTests
[Fact]
public static void ReadOnlySpan_AsBytesUIntToByte()
{
uint[] a = { 0x44332211, 0x88776655 };
uint[] a;
if (BitConverter.IsLittleEndian)
{
a = new uint[] { 0x44332211, 0x88776655 };
}
else
{
a = new uint[] { 0x11223344, 0x55667788 };
}
ReadOnlySpan<uint> span = new ReadOnlySpan<uint>(a);
ReadOnlySpan<byte> asBytes = MemoryMarshal.AsBytes<uint>(span);

Expand Down
10 changes: 9 additions & 1 deletion src/libraries/System.Memory/tests/MemoryMarshal/AsBytesSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ public static partial class MemoryMarshalTests
[Fact]
public static void Span_AsBytesUIntToByte()
{
uint[] a = { 0x44332211, 0x88776655 };
uint[] a;
if (BitConverter.IsLittleEndian)
{
a = new uint[] { 0x44332211, 0x88776655 };
}
else
{
a = new uint[] { 0x11223344, 0x55667788 };
}
Span<uint> span = new Span<uint>(a);
Span<byte> asBytes = MemoryMarshal.AsBytes<uint>(span);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ public static partial class MemoryMarshalTests
[Fact]
public static void CastReadOnlySpanUIntToUShort()
{
uint[] a = { 0x44332211, 0x88776655 };
uint[] a;
if (BitConverter.IsLittleEndian)
{
a = new uint[] { 0x44332211, 0x88776655 };
}
else
{
a = new uint[] { 0x22114433, 0x66558877 };
}
ReadOnlySpan<uint> span = new ReadOnlySpan<uint>(a);
ReadOnlySpan<ushort> asUShort = MemoryMarshal.Cast<uint, ushort>(span);

Expand All @@ -23,7 +31,15 @@ public static void CastReadOnlySpanUIntToUShort()
[Fact]
public static void CastReadOnlySpanShortToLong()
{
short[] a = { 0x1234, 0x2345, 0x3456, 0x4567, 0x5678 };
short[] a;
if (BitConverter.IsLittleEndian)
{
a = new short[] { 0x1234, 0x2345, 0x3456, 0x4567, 0x5678 };
}
else
{
a = new short[] { 0x4567, 0x3456, 0x2345, 0x1234, 0x5678 };
}
ReadOnlySpan<short> span = new ReadOnlySpan<short>(a);
ReadOnlySpan<long> asLong = MemoryMarshal.Cast<short, long>(span);

Expand Down
20 changes: 18 additions & 2 deletions src/libraries/System.Memory/tests/MemoryMarshal/CastSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ public static partial class MemoryMarshalTests
[Fact]
public static void CastSpanUIntToUShort()
{
uint[] a = { 0x44332211, 0x88776655 };
uint[] a;
if (BitConverter.IsLittleEndian)
{
a = new uint[] { 0x44332211, 0x88776655 };
}
else
{
a = new uint[] { 0x22114433, 0x66558877 };
}
Span<uint> span = new Span<uint>(a);
Span<ushort> asUShort = MemoryMarshal.Cast<uint, ushort>(span);

Expand All @@ -35,7 +43,15 @@ public static void CastSpanToEmptyStruct()
[Fact]
public static void CastSpanShortToLong()
{
short[] a = { 0x1234, 0x2345, 0x3456, 0x4567, 0x5678 };
short[] a;
if (BitConverter.IsLittleEndian)
{
a = new short[] { 0x1234, 0x2345, 0x3456, 0x4567, 0x5678 };
}
else
{
a = new short[] { 0x4567, 0x3456, 0x2345, 0x1234, 0x5678 };
}
Span<short> span = new Span<short>(a);
Span<long> asLong = MemoryMarshal.Cast<short, long>(span);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ public void MultiSegmentBytesReaderNumbers()
Assert.Equal(BitConverter.ToInt32(new byte[] { 0, 1, 0, 2 }), intValue);

Assert.True(reader.TryReadBigEndian(out intValue));
Assert.Equal(BitConverter.ToInt32(new byte[] { 4, 3, 2, 1 }), intValue);
Assert.Equal(0x01020304, intValue);

Assert.True(reader.TryReadLittleEndian(out long longValue));
Assert.Equal(BitConverter.ToInt64(new byte[] { 5, 6, 7, 8, 9, 0, 1, 2 }), longValue);
Assert.Equal(0x0201000908070605L, longValue);

Assert.True(reader.TryReadBigEndian(out longValue));
Assert.Equal(BitConverter.ToInt64(new byte[] { 0, 9, 8, 7, 6, 5, 4, 3 }), longValue);
Assert.Equal(0x0304050607080900L, longValue);

Assert.True(reader.TryReadLittleEndian(out short shortValue));
Assert.Equal(BitConverter.ToInt16(new byte[] { 1, 2 }), shortValue);
Assert.Equal(0x0201, shortValue);

Assert.True(reader.TryReadBigEndian(out shortValue));
Assert.Equal(BitConverter.ToInt16(new byte[] { 4, 3 }), shortValue);
Assert.Equal(0x0304, shortValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5755,7 +5755,14 @@ public void BinHex_9(XmlWriterUtils utils)
w.WriteBinHex(Wbase64, 0, (int)Wbase64len);
w.WriteEndElement();
}
Assert.True(utils.CompareReader("<root a='610062006300' />"));
if (System.BitConverter.IsLittleEndian)
{
Assert.True(utils.CompareReader("<root a='610062006300' />"));
}
else
{
Assert.True(utils.CompareReader("<root a='006100620063' />"));
}
}

// Call WriteBinHex and verify results can be read as a string
Expand All @@ -5777,7 +5784,14 @@ public void BinHex_10(XmlWriterUtils utils)
w.WriteBinHex(Wbase64, 0, (int)Wbase64len);
w.WriteEndElement();
}
Assert.True(utils.CompareReader("<root>610062006300</root>"));
if (System.BitConverter.IsLittleEndian)
{
Assert.True(utils.CompareReader("<root>610062006300</root>"));
}
else
{
Assert.True(utils.CompareReader("<root>006100620063</root>"));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using OLEDB.Test.ModuleCore;
using System.Buffers.Binary;

namespace System.Xml.Tests
{
Expand Down Expand Up @@ -47,7 +48,8 @@ public int v3()
string strUni = string.Empty;
for (int i = 0; i < _dbyte.Length; i = i + 2)
{
strUni += (BitConverter.ToChar(_dbyte, i)).ToString();
char c = (char)BinaryPrimitives.ReadUInt16LittleEndian(new Span<byte>(_dbyte, i, 2));
strUni += c.ToString();
}
CError.WriteLine(strUni + " " + XmlConvert.EncodeName(strUni));
CError.Compare(XmlConvert.EncodeName(strUni), "_xFF71_", "EncodeName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using OLEDB.Test.ModuleCore;
using System.Buffers.Binary;

namespace System.Xml.Tests
{
Expand All @@ -26,7 +27,8 @@ public int XmlEncodeName4()
int i = ((CurVariation.id) - 1) * 2;
string strEnVal = string.Empty;

strEnVal = XmlConvert.EncodeName((BitConverter.ToChar(_byte_BaseChar, i)).ToString());
char c = (char)BinaryPrimitives.ReadUInt16LittleEndian(new Span<byte>(_byte_BaseChar, i, 2));
strEnVal = XmlConvert.EncodeName(c.ToString());
CError.Compare(strEnVal, _Expbyte_BaseChar[i / 2], "Comparison failed at " + i);
return TEST_PASS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using OLEDB.Test.ModuleCore;
using System.Buffers.Binary;

namespace System.Xml.Tests
{
Expand All @@ -26,7 +27,8 @@ public int XmlEncodeName5()
int i = ((CurVariation.id) - 1) * 2;
string strEnVal = string.Empty;

strEnVal = XmlConvert.EncodeNmToken((BitConverter.ToChar(_byte_BaseChar, i)).ToString());
char c = (char)BinaryPrimitives.ReadUInt16LittleEndian(new Span<byte>(_byte_BaseChar, i, 2));
strEnVal = XmlConvert.EncodeNmToken(c.ToString());

if (_Expbyte_BaseChar[i / 2] != "_x0387_" && _Expbyte_BaseChar[i / 2] != "_x0640_" && _Expbyte_BaseChar[i / 2] != "_x064B_" && _Expbyte_BaseChar[i / 2] != "_x0670_" && _Expbyte_BaseChar[i / 2] != "_x06D6_" && _Expbyte_BaseChar[i / 2] != "_x06E4_" && _Expbyte_BaseChar[i / 2] != "_x06E7_" && _Expbyte_BaseChar[i / 2] != "_x093C_" && _Expbyte_BaseChar[i / 2] != "_x093E_" && _Expbyte_BaseChar[i / 2] != "_x0962_" && _Expbyte_BaseChar[i / 2] != "_x09E2_" && _Expbyte_BaseChar[i / 2] != "_x09EF_" && _Expbyte_BaseChar[i / 2] != "_x0A71_" && _Expbyte_BaseChar[i / 2] != "_x0ABC_" && _Expbyte_BaseChar[i / 2] != "_x0ABE_" && _Expbyte_BaseChar[i / 2] != "_x0B3C_" && _Expbyte_BaseChar[i / 2] != "_x0B3E_" && _Expbyte_BaseChar[i / 2] != "_x0E31_" && _Expbyte_BaseChar[i / 2] != "_x0E34_" && _Expbyte_BaseChar[i / 2] != "_x0E46_" && _Expbyte_BaseChar[i / 2] != "_x0EB1_" && _Expbyte_BaseChar[i / 2] != "_x0EB4_" && _Expbyte_BaseChar[i / 2] != "_x0EBC_" && _Expbyte_BaseChar[i / 2] != "_x0F3F_")
{
Expand Down
Loading