diff options
author | Stephen Toub <stoub@microsoft.com> | 2017-10-05 07:36:18 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-05 07:36:18 +0300 |
commit | 03d705664c6c14b3ac3b1c73caae69e91542f530 (patch) | |
tree | e54917d746874c9e1ae713f3ecf4c960eb7b1f09 /src/System.IO/tests/StringReader | |
parent | fc225bd023e4e692d18442b02454cada7c252368 (diff) |
Add TextReader/Writer Memory-based virtuals (#24434)
* Add Memory-based Read/WriteAsync virtuals to TextReader/Writer
* Override Span/Memory-based APIs on StringReader/Writer
* Address PR feedback
Diffstat (limited to 'src/System.IO/tests/StringReader')
-rw-r--r-- | src/System.IO/tests/StringReader/StringReader.CtorTests.cs | 2 | ||||
-rw-r--r-- | src/System.IO/tests/StringReader/StringReaderTests.netcoreapp.cs | 122 |
2 files changed, 123 insertions, 1 deletions
diff --git a/src/System.IO/tests/StringReader/StringReader.CtorTests.cs b/src/System.IO/tests/StringReader/StringReader.CtorTests.cs index 2144054af3..cec91adbb6 100644 --- a/src/System.IO/tests/StringReader/StringReader.CtorTests.cs +++ b/src/System.IO/tests/StringReader/StringReader.CtorTests.cs @@ -8,7 +8,7 @@ using Xunit; namespace System.IO.Tests { - public class ReaderTests + public partial class StringReaderTests { [Fact] public static void StringReaderWithNullString() diff --git a/src/System.IO/tests/StringReader/StringReaderTests.netcoreapp.cs b/src/System.IO/tests/StringReader/StringReaderTests.netcoreapp.cs new file mode 100644 index 0000000000..cf9ea48ba2 --- /dev/null +++ b/src/System.IO/tests/StringReader/StringReaderTests.netcoreapp.cs @@ -0,0 +1,122 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace System.IO.Tests +{ + public partial class StringReaderTests + { + [Fact] + public void ReadSpan_Success() + { + string input = "abcdef"; + var reader = new StringReader(input); + Span<char> s = new char[2]; + + Assert.Equal(2, reader.Read(s)); + Assert.Equal("ab", new string(s.ToArray())); + + Assert.Equal(1, reader.Read(s.Slice(0, 1))); + Assert.Equal("cb", new string(s.ToArray())); + + Assert.Equal(2, reader.Read(s)); + Assert.Equal("de", new string(s.ToArray())); + + Assert.Equal(1, reader.Read(s)); + Assert.Equal("f", new string(s.Slice(0, 1).ToArray())); + + Assert.Equal(0, reader.Read(s)); + } + + [Fact] + public void ReadBlockSpan_Success() + { + string input = "abcdef"; + var reader = new StringReader(input); + Span<char> s = new char[2]; + + Assert.Equal(2, reader.ReadBlock(s)); + Assert.Equal("ab", new string(s.ToArray())); + + Assert.Equal(1, reader.ReadBlock(s.Slice(0, 1))); + Assert.Equal("cb", new string(s.ToArray())); + + Assert.Equal(2, reader.ReadBlock(s)); + Assert.Equal("de", new string(s.ToArray())); + + Assert.Equal(1, reader.ReadBlock(s)); + Assert.Equal("f", new string(s.Slice(0, 1).ToArray())); + + Assert.Equal(0, reader.ReadBlock(s)); + } + + [Fact] + public async Task ReadMemoryAsync_Success() + { + string input = "abcdef"; + var reader = new StringReader(input); + Memory<char> m = new char[2]; + + Assert.Equal(2, await reader.ReadAsync(m)); + Assert.Equal("ab", new string(m.ToArray())); + + Assert.Equal(1, await reader.ReadAsync(m.Slice(0, 1))); + Assert.Equal("cb", new string(m.ToArray())); + + Assert.Equal(2, await reader.ReadAsync(m)); + Assert.Equal("de", new string(m.ToArray())); + + Assert.Equal(1, await reader.ReadAsync(m)); + Assert.Equal("f", new string(m.Slice(0, 1).ToArray())); + + Assert.Equal(0, await reader.ReadAsync(m)); + } + + [Fact] + public async Task ReadBlockMemoryAsync_Success() + { + string input = "abcdef"; + var reader = new StringReader(input); + Memory<char> m = new char[2]; + + Assert.Equal(2, await reader.ReadBlockAsync(m)); + Assert.Equal("ab", new string(m.ToArray())); + + Assert.Equal(1, await reader.ReadBlockAsync(m.Slice(0, 1))); + Assert.Equal("cb", new string(m.ToArray())); + + Assert.Equal(2, await reader.ReadBlockAsync(m)); + Assert.Equal("de", new string(m.ToArray())); + + Assert.Equal(1, await reader.ReadBlockAsync(m)); + Assert.Equal("f", new string(m.Slice(0, 1).ToArray())); + + Assert.Equal(0, await reader.ReadBlockAsync(m)); + } + + [Fact] + public void Disposed_ThrowsException() + { + var reader = new StringReader("abc"); + reader.Dispose(); + + Assert.Throws<ObjectDisposedException>(() => reader.Read(Span<char>.Empty)); + Assert.Throws<ObjectDisposedException>(() => reader.ReadBlock(Span<char>.Empty)); + Assert.Throws<ObjectDisposedException>(() => { reader.ReadAsync(Memory<char>.Empty); }); + Assert.Throws<ObjectDisposedException>(() => { reader.ReadBlockAsync(Memory<char>.Empty); }); + } + + [Fact] + public async Task Precanceled_ThrowsException() + { + var reader = new StringReader("abc"); + + await Assert.ThrowsAnyAsync<OperationCanceledException>(() => reader.ReadAsync(Memory<char>.Empty, new CancellationToken(true)).AsTask()); + await Assert.ThrowsAnyAsync<OperationCanceledException>(() => reader.ReadBlockAsync(Memory<char>.Empty, new CancellationToken(true)).AsTask()); + } + } +} |