Skip to content

Commit

Permalink
Merge pull request #4671 from mithileshz/StreamsComparerEnhancement
Browse files Browse the repository at this point in the history
Increment by 'Actual bytes read' rather than the buffer size in StreamsComparer.cs
  • Loading branch information
stevenaw committed Apr 9, 2024
2 parents 4fa8396 + 6576b20 commit 612dd8c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static EqualMethodResult Equal_Enhanced(Stream xStream, Stream yStream, o

if (MemoryExtensions.SequenceEqual<byte>(bufferExpected, bufferActual))
{
readByte += BUFFER_SIZE;
readByte += readActual;
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static EqualMethodResult Equal(object x, object y, ref Tolerance toleranc

if (MemoryExtensions.SequenceEqual<byte>(bufferExpected, bufferActual))
{
readByte += BUFFER_SIZE;
readByte += readActual;
continue;
}

Expand All @@ -88,7 +88,6 @@ public static EqualMethodResult Equal(object x, object y, ref Tolerance toleranc
return EqualMethodResult.ComparedNotEqual;
}
}
readByte += BUFFER_SIZE;
}
}
finally
Expand Down
26 changes: 26 additions & 0 deletions src/NUnitFramework/tests/Constraints/EqualConstraintTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ public void UnSeekableLargeActualStreamUnequal()
Assert.That(entryStream, Is.Not.EqualTo(expectedStream));
}

[Test]
public void ShortReadingMemoryStream_AssertErrorMessageFailurePointIsCorrect()
{
var unequalStream = HelloString.Remove(HelloString.Length - 1, 1) + ".";

using var expectedStream = new ShortReadingMemoryStream(Encoding.UTF8.GetBytes(HelloString));

using var entryStream = new ShortReadingMemoryStream(Encoding.UTF8.GetBytes(unequalStream));

var ex = Assert.Throws<AssertionException>(() => Assert.That(entryStream, Is.EqualTo(expectedStream)));

Assert.That(ex?.Message, Does.Contain("Stream lengths are both 9. Streams differ at offset 8."));
}

[Test]
public void SeekableEmptyStreamEqual()
{
Expand All @@ -269,6 +283,18 @@ private static ZipArchive CreateZipArchive(string content)

return new ZipArchive(archiveContents, ZipArchiveMode.Read, leaveOpen: false);
}

private class ShortReadingMemoryStream : MemoryStream
{
public ShortReadingMemoryStream(byte[] bytes) : base(bytes)
{
}

public override int Read(byte[] buffer, int offset, int count)
{
return base.Read(buffer, offset, 2);
}
}
}

#endregion
Expand Down

0 comments on commit 612dd8c

Please sign in to comment.