Skip to content

Commit

Permalink
Add early return for Streams Equal comparison when they are empty (#4674
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mithileshz committed Mar 27, 2024
1 parent bb18e96 commit 4fa8396
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Expand Up @@ -30,8 +30,14 @@ public static EqualMethodResult Equal(object x, object y, ref Tolerance toleranc

bool bothSeekable = xStream.CanSeek && yStream.CanSeek;

if (bothSeekable && xStream.Length != yStream.Length)
return EqualMethodResult.ComparedNotEqual;
if (bothSeekable)
{
if (xStream.Length != yStream.Length)
return EqualMethodResult.ComparedNotEqual;

if (xStream.Length == 0)
return EqualMethodResult.ComparedEqual;
}

byte[] bufferExpected = new byte[BUFFER_SIZE];
byte[] bufferActual = new byte[BUFFER_SIZE];
Expand Down
10 changes: 10 additions & 0 deletions src/NUnitFramework/tests/Constraints/EqualConstraintTests.cs
Expand Up @@ -243,6 +243,16 @@ public void UnSeekableLargeActualStreamUnequal()
Assert.That(entryStream, Is.Not.EqualTo(expectedStream));
}

[Test]
public void SeekableEmptyStreamEqual()
{
using var expectedStream = new MemoryStream(Encoding.UTF8.GetBytes(string.Empty));

using var actualStream = new MemoryStream(Encoding.UTF8.GetBytes(string.Empty));

Assert.That(actualStream, Is.EqualTo(expectedStream));
}

private static ZipArchive CreateZipArchive(string content)
{
var archiveContents = new MemoryStream();
Expand Down

0 comments on commit 4fa8396

Please sign in to comment.