Exemplo n.º 1
0
        public void Clear_WorksIfBufferHasNotBeenWrittenTo()
        {
            // Arrange
            var buffer = new PagedCharBuffer(new CharArrayBufferSource());

            // Act
            buffer.Clear();

            // Assert
            Assert.Equal(0, buffer.Length);
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        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.Single(buffer.Pages);

            // Act - 2
            buffer.Append("efgh");

            // Assert - 2
            Assert.Equal(4, buffer.Length);
            Assert.Single(buffer.Pages);
            Assert.Equal(new[] { 'e', 'f', 'g', 'h' }, buffer.Pages[0].Take(buffer.Length));
        }
Exemplo n.º 4
0
        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();
        }