예제 #1
0
        public static void TryEncode_AsUtf8(GeneralTestData testData)
        {
            Rune rune = new Rune(testData.ScalarValue);

            Assert.Equal(testData.Utf8Sequence.Length, actual: rune.Utf8SequenceLength);

            // First, try with a buffer that's too short

            Span <byte> utf8Buffer = stackalloc byte[rune.Utf8SequenceLength - 1];
            bool        success    = TryEncodeToUtf8Bytes_Fn(ref rune, utf8Buffer, out int bytesWritten);

            Assert.False(success);
            Assert.Equal(0, bytesWritten);

            // Then, try with a buffer that's appropriately sized

            utf8Buffer = stackalloc byte[rune.Utf8SequenceLength];
            success    = TryEncodeToUtf8Bytes_Fn(ref rune, utf8Buffer, out bytesWritten);
            Assert.True(success);
            Assert.Equal(testData.Utf8Sequence.Length, bytesWritten);
            Assert.True(utf8Buffer.SequenceEqual(testData.Utf8Sequence));

            // Finally, try with a buffer that's too long (should succeed)

            utf8Buffer = stackalloc byte[rune.Utf8SequenceLength + 1];
            success    = TryEncodeToUtf8Bytes_Fn(ref rune, utf8Buffer, out bytesWritten);
            Assert.True(success);
            Assert.Equal(testData.Utf8Sequence.Length, bytesWritten);
            Assert.True(utf8Buffer.Slice(0, testData.Utf8Sequence.Length).SequenceEqual(testData.Utf8Sequence));
        }
예제 #2
0
        public static void TryEncode_AsUtf16(GeneralTestData testData)
        {
            Rune rune = new Rune(testData.ScalarValue);

            Assert.Equal(testData.Utf16Sequence.Length, rune.Utf16SequenceLength);

            // First, try with a buffer that's too short

            Span <char> utf16Buffer = stackalloc char[rune.Utf16SequenceLength - 1];
            bool        success     = rune.TryEncode(utf16Buffer, out int charsWritten);

            Assert.False(success);
            Assert.Equal(0, charsWritten);

            // Then, try with a buffer that's appropriately sized

            utf16Buffer = stackalloc char[rune.Utf16SequenceLength];
            success     = rune.TryEncode(utf16Buffer, out charsWritten);
            Assert.True(success);
            Assert.Equal(testData.Utf16Sequence.Length, charsWritten);
            Assert.True(utf16Buffer.SequenceEqual(testData.Utf16Sequence));

            // Finally, try with a buffer that's too long (should succeed)

            utf16Buffer = stackalloc char[rune.Utf16SequenceLength + 1];
            success     = rune.TryEncode(utf16Buffer, out charsWritten);
            Assert.True(success);
            Assert.Equal(testData.Utf16Sequence.Length, charsWritten);
            Assert.True(utf16Buffer.Slice(0, testData.Utf16Sequence.Length).SequenceEqual(testData.Utf16Sequence));
        }
예제 #3
0
        public static void Ctor_Cast_Int32_Valid(GeneralTestData testData)
        {
            Rune rune         = new Rune((int)testData.ScalarValue);
            Rune runeFromCast = (Rune)(int)testData.ScalarValue;

            Assert.Equal(rune, runeFromCast);
            Assert.Equal(testData.ScalarValue, rune.Value);
            Assert.Equal(testData.IsAscii, rune.IsAscii);
            Assert.Equal(testData.IsBmp, rune.IsBmp);
            Assert.Equal(testData.Plane, rune.Plane);
        }
예제 #4
0
        public static void Ctor_Cast_Char_Valid(GeneralTestData testData)
        {
            Rune rune         = new Rune(checked ((char)testData.ScalarValue));
            Rune runeFromCast = (Rune)(char)testData.ScalarValue;

            Assert.Equal(rune, runeFromCast);
            Assert.Equal(testData.ScalarValue, rune.Value);
            Assert.Equal(testData.IsAscii, rune.IsAscii);
            Assert.Equal(testData.IsBmp, rune.IsBmp);
            Assert.Equal(testData.Plane, rune.Plane);
        }
        public static void Ctor_Cast_UInt32_Valid(GeneralTestData testData)
        {
            var codePoint         = new CodePoint((uint)testData.ScalarValue);
            var codePointFromCast = (CodePoint)(uint)testData.ScalarValue;

            Assert.Equal(codePoint, codePointFromCast);
            Assert.Equal(testData.ScalarValue, codePoint.Value);
            Assert.Equal(testData.IsAscii, codePoint.IsAscii);
            Assert.Equal(testData.IsBmp, codePoint.IsBmp);
            Assert.Equal(testData.Plane, codePoint.Plane);
        }
예제 #6
0
        public static void TryEncodeToUtf8(GeneralTestData testData)
        {
            Rune rune = new Rune(testData.ScalarValue);

            Assert.Equal(testData.Utf8Sequence.Length, actual: rune.Utf8SequenceLength);

            // First, try with a buffer that's too short

            Span <byte> utf8Buffer = stackalloc byte[rune.Utf8SequenceLength - 1];
            bool        success    = rune.TryEncodeToUtf8(utf8Buffer, out int bytesWritten);

            Assert.False(success);
            Assert.Equal(0, bytesWritten);

            Assert.Throws <ArgumentException>("destination", () => rune.EncodeToUtf8(new byte[rune.Utf8SequenceLength - 1]));

            // Then, try with a buffer that's appropriately sized

            utf8Buffer = stackalloc byte[rune.Utf8SequenceLength];
            success    = rune.TryEncodeToUtf8(utf8Buffer, out bytesWritten);
            Assert.True(success);
            Assert.Equal(testData.Utf8Sequence.Length, bytesWritten);
            Assert.True(utf8Buffer.SequenceEqual(testData.Utf8Sequence));

            utf8Buffer.Clear();
            Assert.Equal(testData.Utf8Sequence.Length, rune.EncodeToUtf8(utf8Buffer));
            Assert.True(utf8Buffer.SequenceEqual(testData.Utf8Sequence));

            // Finally, try with a buffer that's too long (should succeed)

            utf8Buffer = stackalloc byte[rune.Utf8SequenceLength + 1];
            success    = rune.TryEncodeToUtf8(utf8Buffer, out bytesWritten);
            Assert.True(success);
            Assert.Equal(testData.Utf8Sequence.Length, bytesWritten);
            Assert.True(utf8Buffer.Slice(0, testData.Utf8Sequence.Length).SequenceEqual(testData.Utf8Sequence));

            utf8Buffer.Clear();
            Assert.Equal(testData.Utf8Sequence.Length, rune.EncodeToUtf8(utf8Buffer));
            Assert.True(utf8Buffer.Slice(0, testData.Utf8Sequence.Length).SequenceEqual(testData.Utf8Sequence));
        }
예제 #7
0
 public static void ToStringTests(GeneralTestData testData)
 {
     Assert.Equal(new string(testData.Utf16Sequence), new Rune(testData.ScalarValue).ToString());
 }
예제 #8
0
 public static void TryCreate_UInt32_Valid(GeneralTestData testData)
 {
     Assert.True(Rune.TryCreate((uint)testData.ScalarValue, out Rune result));
     Assert.Equal(testData.ScalarValue, result.Value);
 }
        public static void Utf8SequenceLengthIsCorrect(GeneralTestData testData)
        {
            var codePoint = new CodePoint(testData.ScalarValue);

            Assert.Equal(testData.Utf8Sequence.Length, codePoint.Utf8SequenceLength);
        }