diff --git a/src/Yarhl.UnitTests/IO/DataStreamTests.cs b/src/Yarhl.UnitTests/IO/DataStreamTests.cs index c23db69b..d619cb79 100644 --- a/src/Yarhl.UnitTests/IO/DataStreamTests.cs +++ b/src/Yarhl.UnitTests/IO/DataStreamTests.cs @@ -1606,6 +1606,18 @@ public void WriteSegmentToGuards() Throws.InstanceOf()); } + [Test] + public void CompareSameStreams() + { + using var stream1 = new DataStream(); + stream1.WriteByte(0xCA); + stream1.WriteByte(0xFE); + stream1.WriteByte(0x00); + stream1.WriteByte(0xFF); + DataStream stream2 = stream1; + Assert.IsTrue(stream1.Compare(stream2)); + } + [Test] public void CompareTwoEqualStreams() { diff --git a/src/Yarhl/IO/DataStream.cs b/src/Yarhl/IO/DataStream.cs index baa56516..4ce701eb 100644 --- a/src/Yarhl/IO/DataStream.cs +++ b/src/Yarhl/IO/DataStream.cs @@ -674,13 +674,19 @@ public void WriteSegmentTo(long start, long length, Stream stream) /// Stream to compare with. public bool Compare(Stream otherStream) { - if (otherStream == null) + if (otherStream == null) { throw new ArgumentNullException(nameof(otherStream)); + } // We can't check if the other stream is disposed because Stream // doesn't provide the property, so we delay it to the Seek method. - if (Disposed) + if (Disposed) { throw new ObjectDisposedException(nameof(DataStream)); + } + + if (this == otherStream) { + return true; + } long startPosition = Position; long otherStreamStartPosition = otherStream.Position;