Skip to content

Commit

Permalink
Big-endian test case fixes: System.Memory tests (#49690)
Browse files Browse the repository at this point in the history
* Fix endian assumptions in preparing input byte patterns

* Enable tests that were disabled on big-endian systems
  • Loading branch information
uweigand authored Mar 16, 2021
1 parent af2d17b commit 21d8418
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 33 deletions.
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);
}
}
}

0 comments on commit 21d8418

Please sign in to comment.