Skip to content

Commit

Permalink
fix: endianness typo
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulusParssinen committed Jun 18, 2024
1 parent b7df0db commit 9fa3737
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
32 changes: 20 additions & 12 deletions Shockky.Tests/IO/ShockwaveWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public void Write_VariableInteger_AreEqual(int value)
}

[Fact]
public void Write_BigEndian_NumericValues_AreEqual() => Write_NumericValues_AreEqual(isBigEndian: true);
public void Write_BigEndian_NumericValues_AreEqual() => Write_NumericValues_AreEqual(reverseEndianness: true);

[Fact]
public void Write_LittleEndian_NumericValues_AreEqual() => Write_NumericValues_AreEqual(isBigEndian: false);
public void Write_LittleEndian_NumericValues_AreEqual() => Write_NumericValues_AreEqual(reverseEndianness: false);

private void Write_NumericValues_AreEqual(bool isBigEndian)
private void Write_NumericValues_AreEqual(bool reverseEndianness)
{
const int OUTPUT_SIZE = sizeof(byte)
+ sizeof(short) * 2
Expand All @@ -47,26 +47,34 @@ private void Write_NumericValues_AreEqual(bool isBigEndian)
+ sizeof(uint) * 2;

Span<byte> buffer = stackalloc byte[OUTPUT_SIZE];
var output = new ShockwaveWriter(buffer, isBigEndian);
var input = new ShockwaveReader(buffer, isBigEndian);
var output = new ShockwaveWriter(buffer, reverseEndianness);
var input = new ShockwaveReader(buffer, reverseEndianness);

output.WriteByte((byte)42);
output.WriteInt16LittleEndian((short)4242);
output.WriteInt16BigEndian((short)4242);
output.WriteUInt16LittleEndian((ushort)4242);
output.WriteUInt16BigEndian((ushort)4242);
output.WriteByte(42);

output.WriteInt16LittleEndian(4242);
output.WriteInt16BigEndian(4242);

output.WriteUInt16LittleEndian(4242);
output.WriteUInt16BigEndian(4242);

output.WriteInt32LittleEndian(123456789);
output.WriteInt32BigEndian(123456789);
output.WriteUInt32LittleEndian((uint)123456789);
output.WriteUInt32BigEndian((uint)123456789);

output.WriteUInt32LittleEndian(123456789);
output.WriteUInt32BigEndian(123456789);

Assert.Equal(42, input.ReadByte());

Assert.Equal(4242, input.ReadInt16LittleEndian());
Assert.Equal(4242, input.ReadInt16BigEndian());

Assert.Equal((ushort)4242, input.ReadUInt16LittleEndian());
Assert.Equal((ushort)4242, input.ReadUInt16BigEndian());

Assert.Equal(123456789, input.ReadInt32LittleEndian());
Assert.Equal(123456789, input.ReadInt32BigEndian());

Assert.Equal((uint)123456789, input.ReadUInt32LittleEndian());
Assert.Equal((uint)123456789, input.ReadUInt32BigEndian());
}
Expand Down
2 changes: 1 addition & 1 deletion Shockky/IO/ShockwaveReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public uint ReadUInt32BigEndian()
Advance(sizeof(uint));

return ReverseEndianness ?
value : BinaryPrimitives.ReverseEndianness(value);
BinaryPrimitives.ReverseEndianness(value) : value;
}

public ulong ReadUInt64LittleEndian()
Expand Down
10 changes: 5 additions & 5 deletions Shockky/IO/ShockwaveWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void WriteInt16LittleEndian(short value)
}
public void WriteInt16BigEndian(short value)
{
if (!_reverseEndianness)
if (_reverseEndianness)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Expand All @@ -77,7 +77,7 @@ public void WriteUInt16LittleEndian(ushort value)
}
public void WriteUInt16BigEndian(ushort value)
{
if (!_reverseEndianness)
if (_reverseEndianness)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Expand All @@ -98,7 +98,7 @@ public void WriteInt32LittleEndian(int value)
}
public void WriteInt32BigEndian(int value)
{
if (!_reverseEndianness)
if (_reverseEndianness)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Expand All @@ -119,7 +119,7 @@ public void WriteUInt32LittleEndian(uint value)
}
public void WriteUInt32BigEndian(uint value)
{
if (!_reverseEndianness)
if (_reverseEndianness)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Expand All @@ -140,7 +140,7 @@ public void WriteUInt64LittleEndian(ulong value)
}
public void WriteUInt64BigEndian(ulong value)
{
if (!_reverseEndianness)
if (_reverseEndianness)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Expand Down

0 comments on commit 9fa3737

Please sign in to comment.