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

Tests for Read_CharArrayTest reading more than necessary #40521

Merged
merged 2 commits into from
Aug 30, 2019
Merged
Changes from 1 commit
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
43 changes: 42 additions & 1 deletion src/System.IO/tests/BinaryReader/BinaryReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void Read_InvalidEncoding()

using (var reader = new BinaryReader(str, new NegEncoding()))
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("charsRemaining", () => reader.Read(new char[10], 0, 10));
Assert.ThrowsAny<ArgumentException>(() => reader.Read(new char[10], 0, 10));
}
}
}
Expand Down Expand Up @@ -187,5 +187,46 @@ public void ReadChars(char[] source, int readLength, char[] expected)
}
}
}

// ChunkingStream returns less than requested
class ChunkingStream : MemoryStream
{
public override int Read(byte[] buffer, int offset, int count)
{
return base.Read(buffer, offset, count > 10 ? count - 3 : count);
}

public override int Read(Span<byte> destination)
{
return base.Read(destination.Length > 10 ? destination.Slice(0, destination.Length - 3) : destination);
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void ReadChars_OverReads(bool unicodeEncoding)
{
Encoding encoding = unicodeEncoding ? Encoding.Unicode : Encoding.UTF8;

char[] data1 = "hello world \ud83d\ude03!".ToCharArray(); // 14 code points, 15 chars in UTF-16, 17 bytes in UTF-8
uint data2 = 0xABCDEF01;

using (Stream stream = new ChunkingStream())
{
using (BinaryWriter writer = new BinaryWriter(stream, encoding, leaveOpen: true))
{
writer.Write(data1);
writer.Write(data2);
}

stream.Seek(0, SeekOrigin.Begin);
using (BinaryReader reader = new BinaryReader(stream, encoding, leaveOpen: true))
{
Assert.Equal(data1, reader.ReadChars(data1.Length));
Assert.Equal(data2, reader.ReadUInt32());
}
}
}
}
}