public override async Task FlushAsync() { var length = _charBuffer.Length; if (length == 0) { return; } var pages = _charBuffer.Pages; for (var i = 0; i < pages.Count; i++) { var page = pages[i]; var pageLength = Math.Min(length, page.Length); if (pageLength != 0) { await _inner.WriteAsync(page, index : 0, count : pageLength); } length -= pageLength; } Debug.Assert(length == 0); _charBuffer.Clear(); }
public void Clear_WorksIfBufferHasNotBeenWrittenTo() { // Arrange var buffer = new PagedCharBuffer(new CharArrayBufferSource()); // Act buffer.Clear(); // Assert Assert.Equal(0, buffer.Length); }
public void Clear_ReturnsPagesToBufferSource() { // Arrange var bufferSource = new Mock <ICharBufferSource>(); bufferSource.Setup(s => s.Rent(PagedCharBuffer.PageSize)) .Returns(new char[PagedCharBuffer.PageSize]); var buffer = new PagedCharBuffer(bufferSource.Object); // Act buffer.Append(new string('a', PagedCharBuffer.PageSize * 3 + 4)); buffer.Clear(); // Assert Assert.Equal(0, buffer.Length); bufferSource.Verify(s => s.Return(It.IsAny <char[]>()), Times.Exactly(3)); }
public void UseAfterClear_Works() { // Arrange var buffer = new PagedCharBuffer(new CharArrayBufferSource()); // Act - 1 buffer.Append(new string('a', PagedCharBuffer.PageSize)); buffer.Append(new string('b', 10)); buffer.Clear(); // Assert - 1 Assert.Equal(0, buffer.Length); Assert.Equal(1, buffer.Pages.Count); // Act - 2 buffer.Append("efgh"); // Assert - 2 Assert.Equal(4, buffer.Length); Assert.Equal(1, buffer.Pages.Count); Assert.Equal(new[] { 'e', 'f', 'g', 'h' }, buffer.Pages[0].Take(buffer.Length)); }
private async Task FlushAsyncAwaited() { var length = _charBuffer.Length; Debug.Assert(length > 0); var pages = _charBuffer.Pages; var count = pages.Count; for (var i = 0; i < count; i++) { var page = pages[i]; var pageLength = Math.Min(length, page.Length); if (pageLength != 0) { await _inner.WriteAsync(page, index : 0, count : pageLength); } length -= pageLength; } Debug.Assert(length == 0); _charBuffer.Clear(); }