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

Commit

Permalink
CoreFx #22406 Span based APIs - Text Reader Writer (#23786)
Browse files Browse the repository at this point in the history
  • Loading branch information
WinCPP authored and Paulo Janotti committed Sep 21, 2017
1 parent dd48b05 commit c09ce65
Show file tree
Hide file tree
Showing 15 changed files with 1,184 additions and 88 deletions.
28 changes: 1 addition & 27 deletions src/System.IO/tests/StreamReader/StreamReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,7 @@ protected virtual Stream GetLargeStream()

protected Tuple<char[], StreamReader> GetCharArrayStream()
{
var chArr = new char[]{
char.MinValue
,char.MaxValue
,'\t'
,' '
,'$'
,'@'
,'#'
,'\0'
,'\v'
,'\''
,'\u3190'
,'\uC3A0'
,'A'
,'5'
,'\r'
,'\uFE70'
,'-'
,';'
,'\r'
,'\n'
,'T'
,'3'
,'\n'
,'K'
,'\u00E6'
};
var chArr = TestDataProvider.CharData;
var ms = CreateStream();
var sw = new StreamWriter(ms);

Expand Down
38 changes: 7 additions & 31 deletions src/System.IO/tests/StreamWriter/StreamWriter.WriteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected virtual Stream CreateStream()
[Fact]
public void WriteChars()
{
char[] chArr = setupArray();
char[] chArr = TestDataProvider.CharData;

// [] Write a wide variety of characters and read them back

Expand All @@ -40,30 +40,6 @@ public void WriteChars()
}
}

private static char[] setupArray()
{
return new char[]{
char.MinValue
,char.MaxValue
,'\t'
,' '
,'$'
,'@'
,'#'
,'\0'
,'\v'
,'\''
,'\u3190'
,'\uC3A0'
,'A'
,'5'
,'\uFE70'
,'-'
,';'
,'\u00E6'
};
}

[Fact]
public void NullArray()
{
Expand All @@ -78,7 +54,7 @@ public void NullArray()
[Fact]
public void NegativeOffset()
{
char[] chArr = setupArray();
char[] chArr = TestDataProvider.CharData;

// [] Exception if offset is negative
Stream ms = CreateStream();
Expand All @@ -91,7 +67,7 @@ public void NegativeOffset()
[Fact]
public void NegativeCount()
{
char[] chArr = setupArray();
char[] chArr = TestDataProvider.CharData;

// [] Exception if count is negative
Stream ms = CreateStream();
Expand All @@ -104,7 +80,7 @@ public void NegativeCount()
[Fact]
public void WriteCustomLenghtStrings()
{
char[] chArr = setupArray();
char[] chArr = TestDataProvider.CharData;

// [] Write some custom length strings
Stream ms = CreateStream();
Expand All @@ -127,7 +103,7 @@ public void WriteCustomLenghtStrings()
[Fact]
public void WriteToStreamWriter()
{
char[] chArr = setupArray();
char[] chArr = TestDataProvider.CharData;
// [] Just construct a streamwriter and write to it
//-------------------------------------------------
Stream ms = CreateStream();
Expand All @@ -149,7 +125,7 @@ public void WriteToStreamWriter()
[Fact]
public void TestWritingPastEndOfArray()
{
char[] chArr = setupArray();
char[] chArr = TestDataProvider.CharData;
Stream ms = CreateStream();
StreamWriter sw = new StreamWriter(ms);

Expand All @@ -160,7 +136,7 @@ public void TestWritingPastEndOfArray()
[Fact]
public void VerifyWrittenString()
{
char[] chArr = setupArray();
char[] chArr = TestDataProvider.CharData;
// [] Write string with wide selection of characters and read it back

StringBuilder sb = new StringBuilder(40);
Expand Down
34 changes: 5 additions & 29 deletions src/System.IO/tests/StringWriter/StringWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,9 @@ public class StringWriterTests
static int[] iArrLargeValues = new int[] { int.MaxValue, int.MaxValue - 1, int.MaxValue / 2, int.MaxValue / 10, int.MaxValue / 100 };
static int[] iArrValidValues = new int[] { 10000, 100000, int.MaxValue / 2000, int.MaxValue / 5000, short.MaxValue };

private static char[] getCharArray()
{
return new char[]{
char.MinValue
,char.MaxValue
,'\t'
,' '
,'$'
,'@'
,'#'
,'\0'
,'\v'
,'\''
,'\u3190'
,'\uC3A0'
,'A'
,'5'
,'\uFE70'
,'-'
,';'
,'\u00E6'
};
}

private static StringBuilder getSb()
{
var chArr = getCharArray();
var chArr = TestDataProvider.CharData;
var sb = new StringBuilder(40);
for (int i = 0; i < chArr.Length; i++)
sb.Append(chArr[i]);
Expand Down Expand Up @@ -88,7 +64,7 @@ public static void SimpleWriter()
[Fact]
public static void WriteArray()
{
var chArr = getCharArray();
var chArr = TestDataProvider.CharData;
StringBuilder sb = getSb();
StringWriter sw = new StringWriter(sb);

Expand Down Expand Up @@ -125,7 +101,7 @@ public static void CantWriteNegativeCount()
[Fact]
public static void CantWriteIndexLargeValues()
{
var chArr = getCharArray();
var chArr = TestDataProvider.CharData;
for (int i = 0; i < iArrLargeValues.Length; i++)
{
StringWriter sw = new StringWriter();
Expand All @@ -136,7 +112,7 @@ public static void CantWriteIndexLargeValues()
[Fact]
public static void CantWriteCountLargeValues()
{
var chArr = getCharArray();
var chArr = TestDataProvider.CharData;
for (int i = 0; i < iArrLargeValues.Length; i++)
{
StringWriter sw = new StringWriter();
Expand All @@ -150,7 +126,7 @@ public static void WriteWithOffset()
StringWriter sw = new StringWriter();
StringReader sr;

var chArr = getCharArray();
var chArr = TestDataProvider.CharData;

sw.Write(chArr, 2, 5);

Expand Down
7 changes: 7 additions & 0 deletions src/System.IO/tests/System.IO.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
<Compile Include="$(CommonTestPath)\System\IO\WrappedMemoryStream.cs">
<Link>Common\System\IO\WrappedMemoryStream.cs</Link>
</Compile>
<Compile Include="TestDataProvider\TestDataProvider.cs" />
<Compile Include="TextReader\CharArrayTextReader.cs" />
<Compile Include="TextReader\TextReaderTests.cs" />
<Compile Include="TextReader\TextReaderTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="TextWriter\CharArrayTextWriter.cs" />
<Compile Include="TextWriter\TextWriterTests.cs" />
<Compile Include="TextWriter\TextWriterTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\$(AssemblyName).rd.xml" />
Expand Down
72 changes: 72 additions & 0 deletions src/System.IO/tests/TestDataProvider/TestDataProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.IO.Tests
{
public static class TestDataProvider
{
private static readonly char[] s_charData;
private static readonly char[] s_smallData;
private static readonly char[] s_largeData;

public static object FirstObject { get; } = (object)1;
public static object SecondObject { get; } = (object)"[second object]";
public static object ThirdObject { get; } = (object)"<third object>";
public static object[] MultipleObjects { get; } = new object[] { FirstObject, SecondObject, ThirdObject };

public static string FormatStringOneObject { get; } = "Object is {0}";
public static string FormatStringTwoObjects { get; } = $"Object are '{0}', {SecondObject}";
public static string FormatStringThreeObjects { get; } = $"Objects are {0}, {SecondObject}, {ThirdObject}";
public static string FormatStringMultipleObjects { get; } = "Multiple Objects are: {0}, {1}, {2}";

static TestDataProvider()
{
s_charData = new char[]
{
char.MinValue,
char.MaxValue,
'\t',
' ',
'$',
'@',
'#',
'\0',
'\v',
'\'',
'\u3190',
'\uC3A0',
'A',
'5',
'\r',
'\uFE70',
'-',
';',
'\r',
'\n',
'T',
'3',
'\n',
'K',
'\u00E6'
};

s_smallData = "HELLO".ToCharArray();

var data = new List<char>();
for (int count = 0; count < 1000; ++count)
{
data.AddRange(s_smallData);
}
s_largeData = data.ToArray();
}

public static char[] CharData => s_charData;

public static char[] SmallData => s_smallData;

public static char[] LargeData => s_largeData;
}
}
37 changes: 37 additions & 0 deletions src/System.IO/tests/TextReader/CharArrayTextReader.cs
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 CharArrayTextReader : TextReader
{
private readonly char[] _charBuffer;
private int _charPos = 0;

public bool EndOfStream => _charPos >= _charBuffer.Length;

public CharArrayTextReader(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++];
}
}
}
Loading

0 comments on commit c09ce65

Please sign in to comment.