Пример #1
0
        public static void GetByteCount_Span_MatchesEncodingByteCount()
        {
            const string TextString = "hello world";
            Encoding     e          = Encoding.UTF8;

            Assert.Equal(e.GetByteCount(TextString), e.GetEncoder().GetByteCount(TextString.AsSpan(), flush: true));
        }
Пример #2
0
        public static void GetBytes_Span_MatchesEncodingGetBytes()
        {
            const string TextString = "hello world";
            Encoding     e          = Encoding.UTF8;

            byte[] bytes = new byte[e.GetByteCount(TextString)];
            Assert.Equal(bytes.Length, e.GetEncoder().GetBytes(TextString.AsSpan(), bytes, flush: true));
            Assert.Equal(e.GetBytes(TextString), bytes);
        }
Пример #3
0
        public static void Convert_Span_MatchesGetBytes()
        {
            const string TextString = "hello world";
            Encoding     encoding   = Encoding.UTF8;
            Encoder      encoder    = encoding.GetEncoder();

            byte[] bytes;

            bytes = new byte[encoding.GetByteCount(TextString)];
            encoder.Convert(TextString.AsSpan(), bytes.AsSpan(0, 2), true, out int charsUsed, out int bytesUsed, out bool completed);
            Assert.Equal(encoding.GetBytes(TextString).AsSpan(0, 2).ToArray(), bytes.AsSpan(0, 2).ToArray());
            Assert.Equal(2, charsUsed);
            Assert.Equal(2, bytesUsed);
            Assert.False(completed);

            bytes = new byte[encoding.GetByteCount(TextString)];
            encoder.Convert(TextString.AsSpan(), bytes, true, out charsUsed, out bytesUsed, out completed);
            Assert.Equal(encoding.GetBytes(TextString), bytes);
            Assert.Equal(TextString.Length, charsUsed);
            Assert.Equal(bytes.Length, bytesUsed);
            Assert.True(completed);
        }