Call Slice in GetMemory (#7113)

This commit is contained in:
Justin Kotalik 2019-01-30 14:26:56 -08:00 committed by GitHub
parent 5ba89945ea
commit 7d21ee1a5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -95,7 +95,7 @@ namespace System.IO.Pipelines
EnsureCapacity(sizeHint);
return _currentSegment;
return _currentSegment.Slice(_position);
}
/// <inheritdoc />

View File

@ -350,6 +350,44 @@ namespace System.IO.Pipelines.Tests
Assert.Throws<ArgumentOutOfRangeException>(() => Writer.GetSpan(-1));
}
[Fact]
public async Task GetMemorySlicesCorrectly()
{
var expectedString = "abcdef";
var memory = Writer.GetMemory();
Encoding.ASCII.GetBytes("abc").CopyTo(memory);
Writer.Advance(3);
memory = Writer.GetMemory();
Encoding.ASCII.GetBytes("def").CopyTo(memory);
Writer.Advance(3);
await Writer.FlushAsync();
Assert.Equal(expectedString, ReadAsString());
}
[Fact]
public async Task GetSpanSlicesCorrectly()
{
var expectedString = "abcdef";
void NonAsyncMethod()
{
var span = Writer.GetSpan();
Encoding.ASCII.GetBytes("abc").CopyTo(span);
Writer.Advance(3);
span = Writer.GetSpan();
Encoding.ASCII.GetBytes("def").CopyTo(span);
Writer.Advance(3);
}
NonAsyncMethod();
await Writer.FlushAsync();
Assert.Equal(expectedString, ReadAsString());
}
private void WriteStringToStream(string input)
{
var buffer = Encoding.ASCII.GetBytes(input);