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));
}
}
}
}
}
}
+
+ // ChunkingStream returns less than requested
+ private sealed 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());
+ }
+ }
+ }
}
}