Tests for Read_CharArrayTest reading more than necessary (dotnet/corefx#40521)
authorJan Kotas <jkotas@microsoft.com>
Fri, 30 Aug 2019 15:36:01 +0000 (08:36 -0700)
committerGitHub <noreply@github.com>
Fri, 30 Aug 2019 15:36:01 +0000 (08:36 -0700)
* Tests for Read_CharArrayTest reading more than necessary

* Update src/System.IO/tests/BinaryReader/BinaryReaderTests.cs

Co-Authored-By: Stephen Toub <stoub@microsoft.com>
Commit migrated from https://github.com/dotnet/corefx/commit/a192823a84145546be747cc520625062fe6cb5a8

src/libraries/System.IO/tests/BinaryReader/BinaryReaderTests.cs

index 7cc2034..0175f76 100644 (file)
@@ -126,7 +126,7 @@ namespace System.IO.Tests
 
                 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));
                 }
             }
         }
@@ -187,5 +187,46 @@ namespace System.IO.Tests
                 }
             }
         }
+
+        // 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());
+                }
+            }
+        }
     }
 }