Imported Upstream version 5.10.0.69

Former-commit-id: fc39669a0b707dd3c063977486506b6793da2890
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-01-29 19:03:06 +00:00
parent d8f8abd549
commit e2950ec768
6283 changed files with 453847 additions and 91879 deletions

View File

@@ -1,4 +1,8 @@
using System.Linq;
// 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.Linq;
using System.Text;
using Xunit;

View File

@@ -10,7 +10,7 @@ using Xunit;
namespace System.IO.Tests
{
public class StreamCopyToTests
public partial class StreamCopyToTests
{
[Fact]
public void IfCanSeekIsFalseLengthAndPositionShouldNotBeCalled()

View File

@@ -0,0 +1,61 @@
// 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.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Tests
{
public partial class StreamCopyToTests
{
[Fact]
public void CopyToAsync_StreamToken_InvalidArgsThrows()
{
Stream s = new MemoryStream();
AssertExtensions.Throws<ArgumentNullException>("destination", () => { s.CopyToAsync(null, default(CancellationToken)); });
}
[Theory]
[InlineData(0)]
[InlineData(42)]
[InlineData(100000)] // greater than 81920, the DefaultCopyBufferSize
public void CopyToAsync_StreamToken_ExpectedBufferSizePropagated(int length)
{
Stream s = new CustomMemoryStream();
s.Write(new byte[length], 0, length);
s.Position = 0;
const int DefaultCopyBufferSize = 81920;
Assert.Equal(Math.Max(1, Math.Min(length, DefaultCopyBufferSize)), ((Task<int>)s.CopyToAsync(Stream.Null, default(CancellationToken))).Result);
}
private sealed class CustomMemoryStream : MemoryStream
{
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) =>
Task.FromResult(bufferSize);
}
[Fact]
public void CopyToAsync_StreamToken_PrecanceledToken_Cancels()
{
var src = new MemoryStream();
Assert.Equal(TaskStatus.Canceled, src.CopyToAsync(Stream.Null, new CancellationToken(true)).Status);
}
[Fact]
public async Task CopyToAsync_StreamToken_AllDataCopied()
{
var src = new MemoryStream();
src.Write(Enumerable.Range(0, 10000).Select(i => (byte)i).ToArray(), 0, 256);
src.Position = 0;
var dst = new MemoryStream();
await src.CopyToAsync(dst, default(CancellationToken));
Assert.Equal<byte>(src.ToArray(), dst.ToArray());
}
}
}

View File

@@ -0,0 +1,203 @@
// 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.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Tests
{
public partial class StreamReaderTests
{
[Theory]
[InlineData(0)]
[InlineData(10)]
public async Task Read_EmptySpan_ReadsNothing(int length)
{
using (var r = new StreamReader(new MemoryStream(Enumerable.Repeat((byte)'s', length).ToArray())))
{
Assert.Equal(0, r.Read(Span<char>.Empty));
Assert.Equal(0, r.ReadBlock(Span<char>.Empty));
Assert.Equal(0, await r.ReadAsync(Memory<char>.Empty));
Assert.Equal(0, await r.ReadBlockAsync(Memory<char>.Empty));
}
}
[Theory]
[InlineData(1, 100, 1)]
[InlineData(1, 100, 101)]
[InlineData(100, 50, 1)]
[InlineData(100, 50, 101)]
public void Read_ReadsExpectedData(int readLength, int totalLength, int bufferSize)
{
var data = new char[totalLength];
var r = new Random(42);
for (int i = 0; i < data.Length; i++)
{
data[i] = (char)('a' + r.Next(0, 26));
}
var result = new char[data.Length];
Span<char> dst = result;
using (var sr = new StreamReader(new MemoryStream(data.Select(i => (byte)i).ToArray()), Encoding.ASCII, false, bufferSize))
{
while (dst.Length > 0)
{
int read = sr.Read(dst);
Assert.InRange(read, 1, dst.Length);
dst = dst.Slice(read);
}
}
Assert.Equal<char>(data, result);
}
[Theory]
[InlineData(1, 100, 1)]
[InlineData(1, 100, 101)]
[InlineData(100, 50, 1)]
[InlineData(100, 50, 101)]
public void ReadBlock_ReadsExpectedData(int readLength, int totalLength, int bufferSize)
{
var data = new char[totalLength];
var r = new Random(42);
for (int i = 0; i < data.Length; i++)
{
data[i] = (char)('a' + r.Next(0, 26));
}
var result = new char[data.Length];
Span<char> dst = result;
using (var sr = new StreamReader(new MemoryStream(data.Select(i => (byte)i).ToArray()), Encoding.ASCII, false, bufferSize))
{
while (dst.Length > 0)
{
int read = sr.ReadBlock(dst);
Assert.InRange(read, 1, dst.Length);
dst = dst.Slice(read);
}
}
Assert.Equal<char>(data, result);
}
[Theory]
[InlineData(1, 100, 1)]
[InlineData(1, 100, 101)]
[InlineData(100, 50, 1)]
[InlineData(100, 50, 101)]
public async Task ReadAsync_ReadsExpectedData(int readLength, int totalLength, int bufferSize)
{
var data = new char[totalLength];
var r = new Random(42);
for (int i = 0; i < data.Length; i++)
{
data[i] = (char)('a' + r.Next(0, 26));
}
var result = new char[data.Length];
Memory<char> dst = result;
using (var sr = new StreamReader(new MemoryStream(data.Select(i => (byte)i).ToArray()), Encoding.ASCII, false, bufferSize))
{
while (dst.Length > 0)
{
int read = await sr.ReadAsync(dst);
Assert.InRange(read, 1, dst.Length);
dst = dst.Slice(read);
}
}
Assert.Equal<char>(data, result);
}
[Theory]
[InlineData(1, 100, 1)]
[InlineData(1, 100, 101)]
[InlineData(100, 50, 1)]
[InlineData(100, 50, 101)]
public async Task ReadBlockAsync_ReadsExpectedData(int readLength, int totalLength, int bufferSize)
{
var data = new char[totalLength];
var r = new Random(42);
for (int i = 0; i < data.Length; i++)
{
data[i] = (char)('a' + r.Next(0, 26));
}
var result = new char[data.Length];
Memory<char> dst = result;
using (var sr = new StreamReader(new MemoryStream(data.Select(i => (byte)i).ToArray()), Encoding.ASCII, false, bufferSize))
{
while (dst.Length > 0)
{
int read = await sr.ReadBlockAsync(dst);
Assert.InRange(read, 1, dst.Length);
dst = dst.Slice(read);
}
}
Assert.Equal<char>(data, result);
}
[Fact]
public void ReadBlock_RepeatsReadsUntilReadDesiredAmount()
{
char[] data = "hello world".ToCharArray();
var ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
var s = new DelegateStream(
canReadFunc: () => true,
readFunc: (buffer, offset, count) => ms.Read(buffer, offset, 1)); // do actual reads a byte at a time
using (var r = new StreamReader(s, Encoding.UTF8, false, 2))
{
var result = new char[data.Length];
Assert.Equal(data.Length, r.ReadBlock((Span<char>)result));
Assert.Equal<char>(data, result);
}
}
[Fact]
public async Task ReadBlockAsync_RepeatsReadsUntilReadDesiredAmount()
{
char[] data = "hello world".ToCharArray();
var ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
var s = new DelegateStream(
canReadFunc: () => true,
readAsyncFunc: (buffer, offset, count, cancellationToken) => ms.ReadAsync(buffer, offset, 1)); // do actual reads a byte at a time
using (var r = new StreamReader(s, Encoding.UTF8, false, 2))
{
var result = new char[data.Length];
Assert.Equal(data.Length, await r.ReadBlockAsync((Memory<char>)result));
Assert.Equal<char>(data, result);
}
}
[Fact]
public async Task ReadAsync_Precanceled_ThrowsException()
{
using (var sr = new StreamReader(new MemoryStream()))
{
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sr.ReadAsync(Memory<char>.Empty, new CancellationToken(true)).AsTask());
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sr.ReadBlockAsync(Memory<char>.Empty, new CancellationToken(true)).AsTask());
}
}
[Fact]
public async Task Read_SpanMemory_DisposedStream_ThrowsException()
{
var sr = new StreamReader(new MemoryStream());
sr.Dispose();
Assert.Throws<ObjectDisposedException>(() => sr.Read(Span<char>.Empty));
Assert.Throws<ObjectDisposedException>(() => sr.ReadBlock(Span<char>.Empty));
await Assert.ThrowsAsync<ObjectDisposedException>(() => sr.ReadAsync(Memory<char>.Empty).AsTask());
await Assert.ThrowsAsync<ObjectDisposedException>(() => sr.ReadBlockAsync(Memory<char>.Empty).AsTask());
}
}
}

View File

@@ -0,0 +1,205 @@
// 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.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Tests
{
public partial class StreamWriterTests
{
[Fact]
public void Write_EmptySpan_WritesNothing()
{
using (var s = new MemoryStream())
using (var writer = new StreamWriter(s))
{
writer.Write(ReadOnlySpan<char>.Empty);
writer.Flush();
Assert.Equal(0, s.Position);
}
}
[Fact]
public void WriteLine_EmptySpan_WritesNewLine()
{
using (var s = new MemoryStream())
using (var writer = new StreamWriter(s))
{
writer.WriteLine(ReadOnlySpan<char>.Empty);
writer.Flush();
Assert.Equal(Environment.NewLine.Length, s.Position);
}
}
[Fact]
public async Task WriteAsync_EmptyMemory_WritesNothing()
{
using (var s = new MemoryStream())
using (var writer = new StreamWriter(s))
{
await writer.WriteAsync(ReadOnlyMemory<char>.Empty);
await writer.FlushAsync();
Assert.Equal(0, s.Position);
}
}
[Fact]
public async Task WriteLineAsync_EmptyMemory_WritesNothing()
{
using (var s = new MemoryStream())
using (var writer = new StreamWriter(s))
{
await writer.WriteLineAsync(ReadOnlyMemory<char>.Empty);
await writer.FlushAsync();
Assert.Equal(Environment.NewLine.Length, s.Position);
}
}
[Theory]
[InlineData(1, 1, 1, false)]
[InlineData(100, 1, 100, false)]
[InlineData(100, 10, 3, false)]
[InlineData(1, 1, 1, true)]
[InlineData(100, 1, 100, true)]
[InlineData(100, 10, 3, true)]
public void Write_Span_WritesExpectedData(int length, int writeSize, int writerBufferSize, bool autoFlush)
{
using (var s = new MemoryStream())
using (var writer = new StreamWriter(s, Encoding.ASCII, writerBufferSize) { AutoFlush = autoFlush })
{
var data = new char[length];
var rand = new Random(42);
for (int i = 0; i < data.Length; i++)
{
data[i] = (char)(rand.Next(0, 26) + 'a');
}
Span<char> source = data;
while (source.Length > 0)
{
int n = Math.Min(source.Length, writeSize);
writer.Write(source.Slice(0, n));
source = source.Slice(n);
}
writer.Flush();
Assert.Equal(data, s.ToArray().Select(b => (char)b));
}
}
[Theory]
[InlineData(1, 1, 1, false)]
[InlineData(100, 1, 100, false)]
[InlineData(100, 10, 3, false)]
[InlineData(1, 1, 1, true)]
[InlineData(100, 1, 100, true)]
[InlineData(100, 10, 3, true)]
public async Task Write_Memory_WritesExpectedData(int length, int writeSize, int writerBufferSize, bool autoFlush)
{
using (var s = new MemoryStream())
using (var writer = new StreamWriter(s, Encoding.ASCII, writerBufferSize) { AutoFlush = autoFlush })
{
var data = new char[length];
var rand = new Random(42);
for (int i = 0; i < data.Length; i++)
{
data[i] = (char)(rand.Next(0, 26) + 'a');
}
ReadOnlyMemory<char> source = data;
while (source.Length > 0)
{
int n = Math.Min(source.Length, writeSize);
await writer.WriteAsync(source.Slice(0, n));
source = source.Slice(n);
}
await writer.FlushAsync();
Assert.Equal(data, s.ToArray().Select(b => (char)b));
}
}
[Theory]
[InlineData(1, 1, 1, false)]
[InlineData(100, 1, 100, false)]
[InlineData(100, 10, 3, false)]
[InlineData(1, 1, 1, true)]
[InlineData(100, 1, 100, true)]
[InlineData(100, 10, 3, true)]
public void WriteLine_Span_WritesExpectedData(int length, int writeSize, int writerBufferSize, bool autoFlush)
{
using (var s = new MemoryStream())
using (var writer = new StreamWriter(s, Encoding.ASCII, writerBufferSize) { AutoFlush = autoFlush })
{
var data = new char[length];
var rand = new Random(42);
for (int i = 0; i < data.Length; i++)
{
data[i] = (char)(rand.Next(0, 26) + 'a');
}
Span<char> source = data;
while (source.Length > 0)
{
int n = Math.Min(source.Length, writeSize);
writer.WriteLine(source.Slice(0, n));
source = source.Slice(n);
}
writer.Flush();
Assert.Equal(length + (Environment.NewLine.Length * (length / writeSize)), s.Length);
}
}
[Theory]
[InlineData(1, 1, 1, false)]
[InlineData(100, 1, 100, false)]
[InlineData(100, 10, 3, false)]
[InlineData(1, 1, 1, true)]
[InlineData(100, 1, 100, true)]
[InlineData(100, 10, 3, true)]
public async Task WriteLineAsync_Memory_WritesExpectedData(int length, int writeSize, int writerBufferSize, bool autoFlush)
{
using (var s = new MemoryStream())
using (var writer = new StreamWriter(s, Encoding.ASCII, writerBufferSize) { AutoFlush = autoFlush })
{
var data = new char[length];
var rand = new Random(42);
for (int i = 0; i < data.Length; i++)
{
data[i] = (char)(rand.Next(0, 26) + 'a');
}
ReadOnlyMemory<char> source = data;
while (source.Length > 0)
{
int n = Math.Min(source.Length, writeSize);
await writer.WriteLineAsync(source.Slice(0, n));
source = source.Slice(n);
}
await writer.FlushAsync();
Assert.Equal(length + (Environment.NewLine.Length * (length / writeSize)), s.Length);
}
}
[Fact]
public async Task WriteAsync_Precanceled_ThrowsCancellationException()
{
using (var writer = new StreamWriter(Stream.Null))
{
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => writer.WriteAsync(ReadOnlyMemory<char>.Empty, new CancellationToken(true)));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => writer.WriteLineAsync(ReadOnlyMemory<char>.Empty, new CancellationToken(true)));
}
}
}
}

View File

@@ -7,7 +7,6 @@
<ProjectGuid>{492EC54D-D2C4-4B3F-AC1F-646B3F7EBB02}</ProjectGuid>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<!-- Default configurations to help VS understand the configurations -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Debug|AnyCPU'" />
@@ -38,16 +37,19 @@
<Compile Include="MemoryStream\MemoryStreamTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="StreamReader\StreamReader.CtorTests.cs" />
<Compile Include="StreamReader\StreamReaderTests.cs" />
<Compile Include="StreamReader\StreamReaderTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="StreamWriter\StreamWriter.BaseStream.cs" />
<Compile Include="StreamWriter\StreamWriter.CloseTests.cs" />
<Compile Include="StreamWriter\StreamWriter.CtorTests.cs" />
<Compile Include="StreamWriter\StreamWriter.FlushTests.cs" />
<Compile Include="StreamWriter\StreamWriter.WriteTests.cs" />
<Compile Include="StreamWriter\StreamWriter.WriteTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="Stream\Stream.ReadWriteSpan.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="Stream\Stream.NullTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="Stream\Stream.NullTests.cs" />
<Compile Include="Stream\Stream.AsyncTests.cs" />
<Compile Include="Stream\Stream.CopyToTests.cs" />
<Compile Include="Stream\Stream.CopyToTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="Stream\Stream.Methods.cs" />
<Compile Include="Stream\Stream.TestLeaveOpen.cs" />
<Compile Include="Stream\Stream.TimeoutTests.cs" />

View File

@@ -1,4 +1,8 @@
using System;
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

View File

@@ -70,6 +70,16 @@ namespace System.IO.Tests
}
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "#23810 not fixed on .NET Framework")]
public void ReadZeroCharacters()
{
using (CharArrayTextReader tr = GetCharArray().textReader)
{
Assert.Equal(0, tr.Read(new char[0], 0, 0));
}
}
[Fact]
public void ArgumentNullOnNullArray()
{