forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Corefx #22406 Span overloads and tests
- Loading branch information
Showing
10 changed files
with
1,094 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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. | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public class TextReaderImpl : TextReader | ||
{ | ||
private char[] _charBuffer; | ||
int _charPos = 0; | ||
|
||
public bool EndOfStream => _charPos >= _charBuffer.Length; | ||
|
||
public TextReaderImpl(char[] data) | ||
{ | ||
_charBuffer = data; | ||
} | ||
|
||
public override int Peek() | ||
{ | ||
if (_charPos == _charBuffer.Length) | ||
{ | ||
return -1; | ||
} | ||
return _charBuffer[_charPos]; | ||
} | ||
|
||
public override int Read() | ||
{ | ||
if (_charPos == _charBuffer.Length) | ||
{ | ||
return -1; | ||
} | ||
return _charBuffer[_charPos++]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,281 @@ | ||
// 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.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public partial class TextReaderTests | ||
{ | ||
protected virtual char[] GetSmallData() | ||
{ | ||
return new char[] { 'H', 'E', 'L', 'L', 'O' }; | ||
} | ||
|
||
protected virtual char[] GetLargeData() | ||
{ | ||
char[] testData = new char[] { 'H', 'E', 'L', 'L', 'O' }; | ||
|
||
List<char> data = new List<char>(); | ||
for (int i = 0; i < 1000; i++) | ||
{ | ||
data.AddRange(testData); | ||
} | ||
|
||
return data.ToArray(); | ||
} | ||
|
||
protected Tuple<char[], TextReaderImpl> GetCharArray() | ||
{ | ||
var chArr = new char[]{ | ||
char.MinValue | ||
,char.MaxValue | ||
,'\t' | ||
,' ' | ||
,'$' | ||
,'@' | ||
,'#' | ||
,'\0' | ||
,'\v' | ||
,'\'' | ||
,'\u3190' | ||
,'\uC3A0' | ||
,'A' | ||
,'5' | ||
,'\r' | ||
,'\uFE70' | ||
,'-' | ||
,';' | ||
,'\r' | ||
,'\n' | ||
,'T' | ||
,'3' | ||
,'\n' | ||
,'K' | ||
,'\u00E6' | ||
}; | ||
|
||
return new Tuple<char[], TextReaderImpl>(chArr, new TextReaderImpl(chArr)); | ||
} | ||
|
||
[Fact] | ||
public void EndOfStream() | ||
{ | ||
using (var tr = new TextReaderImpl(GetSmallData())) | ||
{ | ||
var result = tr.ReadToEnd(); | ||
|
||
Assert.Equal("HELLO", result); | ||
|
||
Assert.True(tr.EndOfStream, "End of TextReader was not true after ReadToEnd"); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void NotEndOfStream() | ||
{ | ||
using (var tr = new TextReaderImpl(GetSmallData())) | ||
{ | ||
var charBuff = new char[3]; | ||
var result = tr.Read(charBuff, 0, 3); | ||
|
||
Assert.Equal(3, result); | ||
|
||
Assert.Equal("HEL", new string(charBuff)); | ||
|
||
Assert.False(tr.EndOfStream, "End of TextReader was true after ReadToEnd"); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task ReadToEndAsync() | ||
{ | ||
using (var tr = new TextReaderImpl(GetLargeData())) | ||
{ | ||
var result = await tr.ReadToEndAsync(); | ||
|
||
Assert.Equal(5000, result.Length); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void TestRead() | ||
{ | ||
var baseInfo = GetCharArray(); | ||
var tr = baseInfo.Item2; | ||
|
||
for (int i = 0; i < baseInfo.Item1.Length; i++) | ||
{ | ||
int tmp = tr.Read(); | ||
Assert.Equal((int)baseInfo.Item1[i], tmp); | ||
} | ||
|
||
tr.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void ArgumentNullOnNullArray() | ||
{ | ||
var baseInfo = GetCharArray(); | ||
var tr = baseInfo.Item2; | ||
|
||
Assert.Throws<ArgumentNullException>(() => tr.Read(null, 0, 0)); | ||
} | ||
|
||
[Fact] | ||
public void ArgumentOutOfRangeOnInvalidOffset() | ||
{ | ||
var tr = GetCharArray().Item2; | ||
Assert.Throws<ArgumentOutOfRangeException>(() => tr.Read(new char[0], -1, 0)); | ||
} | ||
|
||
[Fact] | ||
public void ArgumentOutOfRangeOnNegativCount() | ||
{ | ||
var tr = GetCharArray().Item2; | ||
AssertExtensions.Throws<ArgumentException>(null, () => tr.Read(new char[0], 0, 1)); | ||
} | ||
|
||
[Fact] | ||
public void ArgumentExceptionOffsetAndCount() | ||
{ | ||
var tr = GetCharArray().Item2; | ||
AssertExtensions.Throws<ArgumentException>(null, () => tr.Read(new char[0], 2, 0)); | ||
} | ||
|
||
[Fact] | ||
public void EmptyInput() | ||
{ | ||
using (var tr = new TextReaderImpl(new char[] { })) | ||
{ | ||
var buffer = new char[10]; | ||
int read = tr.Read(buffer, 0, 1); | ||
Assert.Equal(0, read); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ReadCharArr() | ||
{ | ||
var baseInfo = GetCharArray(); | ||
var tr = baseInfo.Item2; | ||
|
||
var chArr = new char[baseInfo.Item1.Length]; | ||
|
||
var read = tr.Read(chArr, 0, chArr.Length); | ||
|
||
Assert.Equal(chArr.Length, read); | ||
for (int i = 0; i < baseInfo.Item1.Length; i++) | ||
{ | ||
Assert.Equal(baseInfo.Item1[i], chArr[i]); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ReadBlockCharArr() | ||
{ | ||
var baseInfo = GetCharArray(); | ||
var tr = baseInfo.Item2; | ||
|
||
var chArr = new char[baseInfo.Item1.Length]; | ||
|
||
var read = tr.ReadBlock(chArr, 0, chArr.Length); | ||
|
||
Assert.Equal(chArr.Length, read); | ||
for (int i = 0; i < baseInfo.Item1.Length; i++) | ||
{ | ||
Assert.Equal(baseInfo.Item1[i], chArr[i]); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async void ReadBlockAsyncCharArr() | ||
{ | ||
var baseInfo = GetCharArray(); | ||
var tr = baseInfo.Item2; | ||
|
||
var chArr = new char[baseInfo.Item1.Length]; | ||
|
||
var read = await tr.ReadBlockAsync(chArr, 0, chArr.Length); | ||
|
||
Assert.Equal(chArr.Length, read); | ||
for (int i = 0; i < baseInfo.Item1.Length; i++) | ||
{ | ||
Assert.Equal(baseInfo.Item1[i], chArr[i]); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task ReadAsync() | ||
{ | ||
var baseInfo = GetCharArray(); | ||
|
||
var tr = baseInfo.Item2; | ||
|
||
var chArr = new char[baseInfo.Item1.Length]; | ||
|
||
var read = await tr.ReadAsync(chArr, 4, 3); | ||
|
||
Assert.Equal(read, 3); | ||
for (int i = 0; i < 3; i++) | ||
{ | ||
Assert.Equal(baseInfo.Item1[i], chArr[i + 4]); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ReadLines() | ||
{ | ||
var baseInfo = GetCharArray(); | ||
var tr = baseInfo.Item2; | ||
|
||
string valueString = new string(baseInfo.Item1); | ||
|
||
|
||
var data = tr.ReadLine(); | ||
Assert.Equal(valueString.Substring(0, valueString.IndexOf('\r')), data); | ||
|
||
data = tr.ReadLine(); | ||
Assert.Equal(valueString.Substring(valueString.IndexOf('\r') + 1, 3), data); | ||
|
||
data = tr.ReadLine(); | ||
Assert.Equal(valueString.Substring(valueString.IndexOf('\n') + 1, 2), data); | ||
|
||
data = tr.ReadLine(); | ||
Assert.Equal((valueString.Substring(valueString.LastIndexOf('\n') + 1)), data); | ||
} | ||
|
||
[Fact] | ||
public void ReadLines2() | ||
{ | ||
var baseInfo = GetCharArray(); | ||
var tr = baseInfo.Item2; | ||
|
||
string valueString = new string(baseInfo.Item1); | ||
|
||
var temp = new char[10]; | ||
tr.Read(temp, 0, 1); | ||
var data = tr.ReadLine(); | ||
Assert.Equal(valueString.Substring(1, valueString.IndexOf('\r') - 1), data); | ||
} | ||
|
||
[Fact] | ||
public async Task ReadLineAsyncContinuousNewLinesAndTabs() | ||
{ | ||
var newLineTabData = new char[] { '\n', '\n', '\r', '\r', '\n' }; | ||
var tr = new TextReaderImpl(newLineTabData); | ||
|
||
for (int i = 0; i < 4; i++) | ||
{ | ||
var data = await tr.ReadLineAsync(); | ||
Assert.Equal(string.Empty, data); | ||
} | ||
|
||
var eol = await tr.ReadLineAsync(); | ||
Assert.Null(eol); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/System.IO/tests/TextReader/TextReaderTests.netcoreapp..cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public partial class TextReaderTests | ||
{ | ||
[Fact] | ||
public void ReadSpan() | ||
{ | ||
var baseInfo = GetCharArray(); | ||
var tr = baseInfo.Item2; | ||
|
||
var chArr = new char[baseInfo.Item1.Length]; | ||
var chSpan = new Span<char>(chArr, 0, baseInfo.Item1.Length); | ||
|
||
var read = tr.Read(chSpan); | ||
|
||
Assert.Equal(chArr.Length, read); | ||
for (int i = 0; i < baseInfo.Item1.Length; i++) | ||
{ | ||
Assert.Equal(baseInfo.Item1[i], chArr[i]); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ReadBlockSpan() | ||
{ | ||
var baseInfo = GetCharArray(); | ||
var tr = baseInfo.Item2; | ||
|
||
var chArr = new char[baseInfo.Item1.Length]; | ||
var chSpan = new Span<char>(chArr, 0, baseInfo.Item1.Length); | ||
|
||
var read = tr.Read(chSpan); | ||
|
||
Assert.Equal(chArr.Length, read); | ||
for (int i = 0; i < baseInfo.Item1.Length; i++) | ||
{ | ||
Assert.Equal(baseInfo.Item1[i], chArr[i]); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.