public void InputEndsTooEarlyAndRestartsFromUtf16()
        {
            string inputString1 = TextEncoderTestHelper.GenerateValidStringEndsWithHighStartsWithLow(TextEncoderConstants.DataLength, false);
            string inputString2 = TextEncoderTestHelper.GenerateValidStringEndsWithHighStartsWithLow(TextEncoderConstants.DataLength, true);

            char[] inputCharsAll = new char[inputString1.Length + inputString2.Length];
            inputString1.CopyTo(0, inputCharsAll, 0, inputString1.Length);
            inputString2.CopyTo(0, inputCharsAll, inputString1.Length, inputString2.Length);

            ReadOnlySpan <byte> input1 = MemoryMarshal.AsBytes(inputCharsAll.AsSpan(0, inputString1.Length));
            ReadOnlySpan <byte> input2 = MemoryMarshal.AsBytes(inputCharsAll.AsSpan(inputString1.Length - 1));

            ReadOnlySpan <byte> expected = Text.Encoding.UTF8.GetBytes(inputString1 + inputString2);
            Span <byte>         output   = new byte[expected.Length];

            Assert.Equal(OperationStatus.NeedMoreData, Encodings.Utf16.ToUtf8(input1, output, out int consumed, out int written));
            Assert.Equal(input1.Length - 2, consumed);
            Assert.NotEqual(expected.Length, written);
            Assert.True(expected.Slice(0, written).SequenceEqual(output.Slice(0, written)), "Invalid output sequence [first half]");

            expected = expected.Slice(written);
            Assert.Equal(OperationStatus.Done, Encodings.Utf16.ToUtf8(input2, output, out consumed, out written));
            Assert.Equal(input2.Length, consumed);
            Assert.Equal(expected.Length, written);
            Assert.True(expected.SequenceEqual(output.Slice(0, written)), "Invalid output sequence [second half]");
        }
示例#2
0
        //[InlineData(TextEncoderTestHelper.SupportedEncoding.FromUtf32)] // Open issue: https://github.com/dotnet/corefxlab/issues/1513
        public void InputBufferEndsTooEarlyAndRestart(TextEncoderTestHelper.SupportedEncoding from)
        {
            string inputString1 = TextEncoderTestHelper.GenerateValidStringEndsWithHighStartsWithLow(TextEncoderConstants.DataLength, false);
            string inputString2 = inputString1 + TextEncoderTestHelper.GenerateValidStringEndsWithHighStartsWithLow(TextEncoderConstants.DataLength, true);

            byte[] inputUtf8Bytes1   = TextEncoderTestHelper.GenerateValidUtf8BytesEndsWithHighStartsWithLow(TextEncoderConstants.DataLength, false);
            byte[] tempForUtf8Bytes2 = TextEncoderTestHelper.GenerateValidUtf8BytesEndsWithHighStartsWithLow(TextEncoderConstants.DataLength, true);
            byte[] inputUtf8Bytes2   = new byte[inputUtf8Bytes1.Length + tempForUtf8Bytes2.Length];
            Array.Copy(inputUtf8Bytes1, inputUtf8Bytes2, inputUtf8Bytes1.Length);
            Array.Copy(tempForUtf8Bytes2, 0, inputUtf8Bytes2, inputUtf8Bytes1.Length, tempForUtf8Bytes2.Length);

            uint[] inputUtf32Bytes1   = TextEncoderTestHelper.GenerateValidUtf32EndsWithHighStartsWithLow(TextEncoderConstants.DataLength, false);
            uint[] tempForUtf32Bytes2 = TextEncoderTestHelper.GenerateValidUtf32EndsWithHighStartsWithLow(TextEncoderConstants.DataLength, true);
            uint[] inputUtf32Bytes2   = new uint[inputUtf32Bytes1.Length + tempForUtf32Bytes2.Length];
            Array.Copy(inputUtf32Bytes1, inputUtf32Bytes2, inputUtf32Bytes1.Length);
            Array.Copy(tempForUtf32Bytes2, 0, inputUtf32Bytes2, inputUtf32Bytes1.Length, tempForUtf32Bytes2.Length);

            byte[] uint32Bytes1    = TextEncoderTestHelper.GenerateValidBytesUtf32EndsWithHighStartsWithLow(TextEncoderConstants.DataLength, false);
            byte[] tempUint32Bytes = TextEncoderTestHelper.GenerateValidBytesUtf32EndsWithHighStartsWithLow(TextEncoderConstants.DataLength, true);
            byte[] uint32Bytes2    = new byte[uint32Bytes1.Length + tempUint32Bytes.Length];
            Array.Copy(uint32Bytes1, uint32Bytes2, uint32Bytes1.Length);
            Array.Copy(tempUint32Bytes, 0, uint32Bytes2, uint32Bytes1.Length, tempUint32Bytes.Length);

            byte[]      expectedBytes;
            Span <byte> encodedBytes;

            int charactersConsumed1;
            int charactersConsumed2;

            int bytesWritten1;
            int bytesWritten2;

            switch (from)
            {
            case TextEncoderTestHelper.SupportedEncoding.FromUtf8:
                expectedBytes = Text.Encoding.Convert(testEncoder, testEncoder, inputUtf8Bytes2);
                ReadOnlySpan <byte> firstUtf8  = inputUtf8Bytes1;
                ReadOnlySpan <byte> secondUtf8 = inputUtf8Bytes2;
                encodedBytes = new Span <byte>(new byte[expectedBytes.Length]);
                Assert.False(utf8.TryEncode(firstUtf8, encodedBytes, out charactersConsumed1, out bytesWritten1));
                Assert.True(utf8.TryEncode(secondUtf8.Slice(charactersConsumed1), encodedBytes.Slice(bytesWritten1), out charactersConsumed2, out bytesWritten2));
                break;

            case TextEncoderTestHelper.SupportedEncoding.FromUtf16:
                byte[] inputStringUtf16 = testEncoderUnicode.GetBytes(inputString2);
                expectedBytes = Text.Encoding.Convert(testEncoderUnicode, testEncoder, inputStringUtf16);
                ReadOnlySpan <char> firstUtf16  = inputString1.AsSpan();
                ReadOnlySpan <char> secondUtf16 = inputString2.AsSpan();
                encodedBytes = new Span <byte>(new byte[expectedBytes.Length]);
                Assert.False(utf8.TryEncode(firstUtf16, encodedBytes, out charactersConsumed1, out bytesWritten1));
                Assert.True(utf8.TryEncode(secondUtf16.Slice(charactersConsumed1), encodedBytes.Slice(bytesWritten1), out charactersConsumed2, out bytesWritten2));
                break;

            case TextEncoderTestHelper.SupportedEncoding.FromString:         // Open issue: https://github.com/dotnet/corefxlab/issues/1515
                /*inputStringUtf16 = testEncoderUnicode.GetBytes(inputString2);
                 * expectedBytes = Text.Encoding.Convert(testEncoderUnicode, testEncoder, inputStringUtf16);
                 * string firstInputStr = inputString1;
                 * string secondInputStr = inputString2;
                 * encodedBytes = new Span<byte>(new byte[expectedBytes.Length]);
                 * Assert.False(utf8.TryEncode(firstInputStr, encodedBytes, out bytesWritten1));
                 * Assert.True(utf8.TryEncode(secondInputStr.Substring(charactersConsumed1), encodedBytes.Slice(bytesWritten1), out bytesWritten1));*/
                return;

            case TextEncoderTestHelper.SupportedEncoding.FromUtf32:
            default:
                expectedBytes = Text.Encoding.Convert(testEncoderUtf32, testEncoder, uint32Bytes2);
                ReadOnlySpan <uint> firstInput  = inputUtf32Bytes1.AsSpan();
                ReadOnlySpan <uint> secondInput = inputUtf32Bytes2.AsSpan();
                encodedBytes = new Span <byte>(new byte[TextEncoderTestHelper.GetUtf8ByteCount(inputUtf32Bytes2)]);
                Assert.False(utf8.TryEncode(firstInput, encodedBytes, out charactersConsumed1, out bytesWritten1));
                Assert.True(utf8.TryEncode(secondInput.Slice(charactersConsumed1), encodedBytes.Slice(bytesWritten1), out charactersConsumed2, out bytesWritten2));
                break;
            }

            Assert.Equal(TextEncoderConstants.DataLength * 2, charactersConsumed1 + charactersConsumed2);
            Assert.Equal(expectedBytes.Length, bytesWritten1 + bytesWritten2);
            Assert.True(expectedBytes.AsSpan().SequenceEqual(encodedBytes));
        }