Skip to content

Commit

Permalink
Merge pull request #199 from SceneGate/feature/189-compare-itself
Browse files Browse the repository at this point in the history
馃悰  Comparing stream to itself returns false
  • Loading branch information
pleonex committed Nov 24, 2023
2 parents 8283cd2 + abd3b1f commit bf67ed3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Yarhl.UnitTests/IO/DataStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,18 @@ public void WriteSegmentToGuards()
Throws.InstanceOf<ObjectDisposedException>());
}

[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()
{
Expand Down
10 changes: 8 additions & 2 deletions src/Yarhl/IO/DataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,19 @@ public void WriteSegmentTo(long start, long length, Stream stream)
/// <param name="otherStream">Stream to compare with.</param>
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;
Expand Down

0 comments on commit bf67ed3

Please sign in to comment.