Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Add TextReader/Writer Memory-based virtuals #24434

Merged
merged 3 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/System.IO/tests/StringReader/StringReader.CtorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace System.IO.Tests
{
public class ReaderTests
public partial class StringReaderTests
{
[Fact]
public static void StringReaderWithNullString()
Expand Down
122 changes: 122 additions & 0 deletions src/System.IO/tests/StringReader/StringReaderTests.netcoreapp.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
2 changes: 1 addition & 1 deletion src/System.IO/tests/StringWriter/StringWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace System.IO.Tests
{
public class StringWriterTests
public partial class StringWriterTests
{
static int[] iArrInvalidValues = new int[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, int.MinValue, short.MinValue };
static int[] iArrLargeValues = new int[] { int.MaxValue, int.MaxValue - 1, int.MaxValue / 2, int.MaxValue / 10, int.MaxValue / 100 };
Expand Down
40 changes: 40 additions & 0 deletions src/System.IO/tests/StringWriter/StringWriterTests.netcoreapp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 StringWriterTests
{
[Fact]
public async Task WriteSpanMemory_Success()
{
var sw = new StringWriter();

sw.Write((Span<char>)new char[0]);
sw.Write((Span<char>)new char[] { 'a' });
sw.Write((Span<char>)new char[] { 'b', 'c', 'd' });
sw.WriteLine((Span<char>)new char[] { 'e' });

await sw.WriteAsync((ReadOnlyMemory<char>)new char[0]);
await sw.WriteAsync((ReadOnlyMemory<char>)new char[] { 'f' });
await sw.WriteAsync((ReadOnlyMemory<char>)new char[] { 'g', 'h', 'i' });
await sw.WriteLineAsync((ReadOnlyMemory<char>)new char[] { 'j' });

Assert.Equal("abcde" + Environment.NewLine + "fghij" + Environment.NewLine, sw.ToString());
}

[Fact]
public async Task Precanceled_ThrowsException()
{
var writer = new StringWriter();

await Assert.ThrowsAnyAsync<OperationCanceledException>(() => writer.WriteAsync(Memory<char>.Empty, new CancellationToken(true)));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => writer.WriteLineAsync(Memory<char>.Empty, new CancellationToken(true)));
}
}
}
6 changes: 4 additions & 2 deletions src/System.IO/tests/System.IO.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@
<Compile Include="Stream\Stream.Methods.cs" />
<Compile Include="Stream\Stream.TestLeaveOpen.cs" />
<Compile Include="Stream\Stream.TimeoutTests.cs" />
<Compile Include="StringReader\StringReaderTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="StringReader\StringReader.CtorTests.cs" />
<Compile Include="StringWriter\StringWriterTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="StringWriter\StringWriterTests.cs" />
<Compile Include="$(CommonTestPath)\System\Buffers\NativeOwnedMemory.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" >
<Compile Include="$(CommonTestPath)\System\Buffers\NativeOwnedMemory.cs" Condition="'$(TargetGroup)' == 'netcoreapp'">
<Link>Common\System\Buffers\NativeOwnedMemory.cs</Link>
</Compile>
<Compile Include="$(CommonTestPath)\System\IO\CallTrackingStream.cs">
Expand All @@ -77,4 +79,4 @@
<EmbeddedResource Include="Resources\$(AssemblyName).rd.xml" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>
24 changes: 22 additions & 2 deletions src/System.IO/tests/TextWriter/TextWriterTests.netcoreapp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -32,5 +30,27 @@ public void WriteLineCharSpanTest()
Assert.Equal(new string(rs) + tw.NewLine, tw.Text);
}
}

[Fact]
public async Task WriteCharMemoryTest()
{
using (CharArrayTextWriter tw = NewTextWriter)
{
var rs = new Memory<char>(TestDataProvider.CharData, 4, 6);
await tw.WriteAsync(rs);
Assert.Equal(new string(rs.Span), tw.Text);
}
}

[Fact]
public async Task WriteLineCharMemoryTest()
{
using (CharArrayTextWriter tw = NewTextWriter)
{
var rs = new Memory<char>(TestDataProvider.CharData, 4, 6);
await tw.WriteLineAsync(rs);
Assert.Equal(new string(rs.Span) + tw.NewLine, tw.Text);
}
}
}
}
Loading