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
Tests for Read_CharArrayTest reading more than necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotas committed Aug 22, 2019
1 parent a25beee commit 8cc66de
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/System.IO/tests/BinaryReader/BinaryReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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.

Expand Down 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());
}
}
}
}
}

0 comments on commit 8cc66de

Please sign in to comment.