Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Comparing stream to itself returns false #199

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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