From 9fa3737b0dc81234878a72a484ed4f928c470e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulus=20P=C3=A4rssinen?= Date: Wed, 19 Jun 2024 01:12:27 +0300 Subject: [PATCH] fix: endianness typo --- Shockky.Tests/IO/ShockwaveWriterTests.cs | 32 +++++++++++++++--------- Shockky/IO/ShockwaveReader.cs | 2 +- Shockky/IO/ShockwaveWriter.cs | 10 ++++---- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Shockky.Tests/IO/ShockwaveWriterTests.cs b/Shockky.Tests/IO/ShockwaveWriterTests.cs index 6fd7661..a8b4015 100644 --- a/Shockky.Tests/IO/ShockwaveWriterTests.cs +++ b/Shockky.Tests/IO/ShockwaveWriterTests.cs @@ -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 @@ -47,26 +47,34 @@ private void Write_NumericValues_AreEqual(bool isBigEndian) + sizeof(uint) * 2; Span 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()); } diff --git a/Shockky/IO/ShockwaveReader.cs b/Shockky/IO/ShockwaveReader.cs index 2813c1e..2289220 100644 --- a/Shockky/IO/ShockwaveReader.cs +++ b/Shockky/IO/ShockwaveReader.cs @@ -127,7 +127,7 @@ public uint ReadUInt32BigEndian() Advance(sizeof(uint)); return ReverseEndianness ? - value : BinaryPrimitives.ReverseEndianness(value); + BinaryPrimitives.ReverseEndianness(value) : value; } public ulong ReadUInt64LittleEndian() diff --git a/Shockky/IO/ShockwaveWriter.cs b/Shockky/IO/ShockwaveWriter.cs index 2fa5eb1..c62cfcb 100644 --- a/Shockky/IO/ShockwaveWriter.cs +++ b/Shockky/IO/ShockwaveWriter.cs @@ -56,7 +56,7 @@ public void WriteInt16LittleEndian(short value) } public void WriteInt16BigEndian(short value) { - if (!_reverseEndianness) + if (_reverseEndianness) { value = BinaryPrimitives.ReverseEndianness(value); } @@ -77,7 +77,7 @@ public void WriteUInt16LittleEndian(ushort value) } public void WriteUInt16BigEndian(ushort value) { - if (!_reverseEndianness) + if (_reverseEndianness) { value = BinaryPrimitives.ReverseEndianness(value); } @@ -98,7 +98,7 @@ public void WriteInt32LittleEndian(int value) } public void WriteInt32BigEndian(int value) { - if (!_reverseEndianness) + if (_reverseEndianness) { value = BinaryPrimitives.ReverseEndianness(value); } @@ -119,7 +119,7 @@ public void WriteUInt32LittleEndian(uint value) } public void WriteUInt32BigEndian(uint value) { - if (!_reverseEndianness) + if (_reverseEndianness) { value = BinaryPrimitives.ReverseEndianness(value); } @@ -140,7 +140,7 @@ public void WriteUInt64LittleEndian(ulong value) } public void WriteUInt64BigEndian(ulong value) { - if (!_reverseEndianness) + if (_reverseEndianness) { value = BinaryPrimitives.ReverseEndianness(value); }