public async Task Write_String()
        {
            // Arrange
            var pool  = new TestArrayPool();
            var inner = new StringWriter();

            var writer = new PagedBufferedTextWriter(pool, inner);

            // These numbers chosen to hit boundary conditions in buffer lengths
            Assert.Equal(4096, Content.Length); // Update these numbers if this changes.
            var chunkSizes = new int[] { 3, 1021, 1023, 1023, 1, 1, 1024 };

            // Act
            var offset = 0;

            foreach (var chunkSize in chunkSizes)
            {
                var chunk = new string(Content, offset, chunkSize);
                writer.Write(chunk);
                offset += chunkSize;
            }

            await writer.FlushAsync();

            // Assert
            Assert.Equal <char>(Content, inner.ToString().ToCharArray());
        }
        public async Task SynchronousWrites_FollowedByAsyncWriteCharArray_WritesAllContent()
        {
            // Arrange
            var pool  = new TestArrayPool();
            var inner = new StringWriter();

            var writer = new PagedBufferedTextWriter(new TestArrayPool(), inner);

            // Act
            writer.Write('a');
            writer.Write(new[] { 'b', 'c', 'd' });
            writer.Write("ef");
            await writer.WriteAsync(new[] { 'g', 'h', 'i' });

            // Assert
            Assert.Equal("abcdefghi", inner.ToString());
        }
        public void Write_CharArray_Bounded_Null()
        {
            // Arrange
            var pool  = new TestArrayPool();
            var inner = new StringWriter();

            var writer = new PagedBufferedTextWriter(pool, inner);

            // Act & Assert
            Assert.Throws <ArgumentNullException>("buffer", () => writer.Write(null, 0, 0));
        }
        public async Task Write_CharArray_Null()
        {
            // Arrange
            var pool  = new TestArrayPool();
            var inner = new StringWriter();

            var writer = new PagedBufferedTextWriter(pool, inner);

            // Act
            writer.Write((char[])null);

            await writer.FlushAsync();

            // Assert
            Assert.Empty(inner.ToString());
        }
        public async Task FlushAsync_WritesContentToInner_WithLargeArrays()
        {
            // Arrange
            var pool  = new RentMoreArrayPool();
            var inner = new StringWriter();

            var writer = new PagedBufferedTextWriter(pool, inner);

            for (var i = 0; i < Content.Length; i++)
            {
                writer.Write(Content[i]);
            }

            // Act
            await writer.FlushAsync();

            // Assert
            Assert.Equal <char>(Content, inner.ToString().ToCharArray());
        }
        public async Task Write_Char()
        {
            // Arrange
            var pool  = new TestArrayPool();
            var inner = new StringWriter();

            var writer = new PagedBufferedTextWriter(pool, inner);

            // Act
            for (var i = 0; i < Content.Length; i++)
            {
                writer.Write(Content[i]);
            }

            await writer.FlushAsync();

            // Assert
            Assert.Equal <char>(Content, inner.ToString().ToCharArray());
        }
        public async Task FlushAsync_ReturnsPages()
        {
            // Arrange
            var pool  = new TestArrayPool();
            var inner = new StringWriter();

            var writer = new PagedBufferedTextWriter(pool, inner);

            for (var i = 0; i < Content.Length; i++)
            {
                writer.Write(Content[i]);
            }

            // Act
            await writer.FlushAsync();

            // Assert
            Assert.Equal(3, pool.Returned.Count);
        }
        public async Task Write_CharArray()
        {
            // Arrange
            var pool  = new TestArrayPool();
            var inner = new StringWriter();

            var writer = new PagedBufferedTextWriter(pool, inner);

            // These numbers chosen to hit boundary conditions in buffer lengths
            Assert.Equal(4096, Content.Length); // Update these numbers if this changes.
            var chunkSizes = new int[] { 3, 1021, 1023, 1023, 1, 1, 1024 };

            // Act
            var offset = 0;

            foreach (var chunkSize in chunkSizes)
            {
                var chunk = new char[chunkSize];
                for (var j = 0; j < chunkSize; j++)
                {
                    chunk[j] = Content[offset + j];
                }

                writer.Write(chunk);
                offset += chunkSize;
            }

            await writer.FlushAsync();

            // Assert
            var array = inner.ToString().ToCharArray();

            for (var i = 0; i < Content.Length; i++)
            {
                Assert.Equal(Content[i], array[i]);
            }

            Assert.Equal <char>(Content, inner.ToString().ToCharArray());
        }