Skip to content

Commit

Permalink
Improved IMemoryOwner.AsStream tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed May 7, 2020
1 parent bb779f4 commit e8a9534
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public void Test_IMemoryOwnerExtensions_EmptyIMemoryOwnerStream()
Stream stream = buffer.AsStream();

Assert.IsNotNull(stream);
Assert.AreEqual(stream.Length, buffer.Length);
Assert.AreEqual(buffer.Length, 0);
Assert.AreEqual(stream.Length, 0);
Assert.IsTrue(stream.CanWrite);
}

Expand All @@ -37,5 +38,30 @@ public void Test_MemoryExtensions_IMemoryOwnerStream()
Assert.AreEqual(stream.Length, buffer.Length);
Assert.IsTrue(stream.CanWrite);
}

[TestCategory("IMemoryOwnerExtensions")]
[TestMethod]
public void Test_MemoryExtensions_IMemoryOwnerStream_DoesNotAlterExistingData()
{
MemoryOwner<byte> buffer = MemoryOwner<byte>.Allocate(1024);

// Fill the buffer with sample data
for (int i = 0; i < buffer.Length; i++)
{
buffer.Span[i] = unchecked((byte)(i & byte.MaxValue));
}

Stream stream = buffer.AsStream();

Assert.IsNotNull(stream);
Assert.AreEqual(stream.Length, buffer.Length);
Assert.IsTrue(stream.CanWrite);

// Validate that creating the stream doesn't alter the underlying buffer
for (int i = 0; i < buffer.Length; i++)
{
Assert.AreEqual(buffer.Span[i], unchecked((byte)(i & byte.MaxValue)));
}
}
}
}

0 comments on commit e8a9534

Please sign in to comment.