From c713f6e235c4e1cb6003805b71cc8e20746cd0ba Mon Sep 17 00:00:00 2001 From: Levi Broderick Date: Tue, 30 Apr 2019 18:51:57 -0700 Subject: [PATCH] Add more UTF-16 validation tests (dotnet/corefx#37252) Commit migrated from https://github.com/dotnet/corefx/commit/a8dacfb2d4fd38aafce49adc8de30f31e08bd598 --- .../Utf16UtilityTests.ValidateChars.netcoreapp.cs | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf16UtilityTests.ValidateChars.netcoreapp.cs b/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf16UtilityTests.ValidateChars.netcoreapp.cs index 510a22c..7e5281e 100644 --- a/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf16UtilityTests.ValidateChars.netcoreapp.cs +++ b/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf16UtilityTests.ValidateChars.netcoreapp.cs @@ -5,6 +5,7 @@ using System.Buffers; using System.Globalization; using System.Linq; +using System.Numerics; using System.Reflection; using System.Runtime.InteropServices; using Xunit; @@ -117,6 +118,28 @@ namespace System.Text.Unicode.Tests GetIndexOfFirstInvalidUtf16Sequence_Test_Core(chars, 15, expectedRuneCount: 13, expectedUtf8ByteCount: 20); } + [Fact] + public void GetIndexOfFirstInvalidUtf16Sequence_WithStandaloneLowSurrogateCharAtStart() + { + // The input stream will be a vector's worth of ASCII chars, followed by a single standalone low + // surrogate char, then padded with U+0000 until it's a multiple of the vector size. + // Using Vector.Count here as a stand-in for Vector.Count. + + char[] chars = new char[Vector.Count * 2]; + for (int i = 0; i < Vector.Count; i++) + { + chars[i] = 'x'; // ASCII char + } + + chars[Vector.Count] = '\uDEAD'; // standalone low surrogate char + + for (int i = 0; i <= Vector.Count; i++) + { + // Expect all ASCII chars to be consumed, low surrogate char to be marked invalid. + GetIndexOfFirstInvalidUtf16Sequence_Test_Core(chars[(Vector.Count - i)..], i, i, i); + } + } + private static void GetIndexOfFirstInvalidUtf16Sequence_Test_Core(string unprocessedInput, int expectedIdxOfFirstInvalidChar, int expectedRuneCount, long expectedUtf8ByteCount) { char[] processedInput = ProcessInput(unprocessedInput).ToCharArray(); -- 2.7.4