示例#1
0
        public void TestReadString(
            string value,
            string encodingStr,
            bool isSafe,
            int fixedLength,
            int firstSize,
            int secondSize,
            int offset
            )
        {
            Span <byte> buffer = stackalloc byte[firstSize + secondSize];

            buffer.Clear();

            var encoding = EncodingHelpers.GetEncoding(encodingStr);

            var strLength = fixedLength > -1 ? Math.Min(value.Length, fixedLength) : value.Length;
            var chars     = value.AsSpan(0, strLength);

            encoding.GetBytes(chars, buffer.Slice(offset));

            var reader = new CircularBufferReader(buffer.SliceToLength(firstSize), buffer.Slice(firstSize));

            reader.Seek(offset, SeekOrigin.Begin);

            var actual = reader.ReadString(encoding, isSafe, fixedLength);

            Assert.Equal(value.Substring(0, strLength), actual);
        }
        public void TestWriteString(
            string value,
            string encodingStr,
            int fixedLength,
            int firstSize,
            int secondSize,
            int offset
            )
        {
            Span <byte> buffer = stackalloc byte[firstSize + secondSize];

            buffer.Clear();

            var encoding  = EncodingHelpers.GetEncoding(encodingStr);
            var strLength = fixedLength > -1 ? Math.Min(value.Length, fixedLength) : value.Length;
            var chars     = value.AsSpan(0, strLength);

            var writer = new CircularBufferWriter(buffer[..firstSize], buffer[firstSize..]);
        // First only, beginning

        // Second only

        // Split

        // First only, beginning, fixed length smaller

        // Second only, fixed length smaller

        // Split, fixed length smaller

        // First only, beginning, fixed length bigger

        // Second only, fixed length bigger

        // Split, fixed length bigger
        public void TestWriteString(
            string value,
            string encodingStr,
            int fixedLength,
            int firstSize,
            int secondSize,
            int offset
            )
        {
            Span <byte> buffer = stackalloc byte[firstSize + secondSize];

            buffer.Clear();

            var encoding  = EncodingHelpers.GetEncoding(encodingStr);
            var strLength = fixedLength > -1 ? Math.Min(value.Length, fixedLength) : value.Length;
            var chars     = value.AsSpan(0, strLength);

            var writer = new CircularBufferWriter(buffer.Slice(0, firstSize), buffer.Slice(firstSize));

            writer.Seek(offset, SeekOrigin.Begin);
            writer.WriteString(chars, encoding);

            if (offset > 0)
            {
                Span <byte> testEmpty = stackalloc byte[offset];
                testEmpty.Clear();
                AssertThat.Equal(buffer.Slice(0, offset), testEmpty);
            }

            Span <byte> expectedStr = stackalloc byte[encoding.GetByteCount(chars)];

            encoding.GetBytes(chars, expectedStr.Slice(0));

            AssertThat.Equal(buffer.Slice(offset, expectedStr.Length), expectedStr);
            offset += expectedStr.Length;

            if (offset < buffer.Length)
            {
                Span <byte> testEmpty = stackalloc byte[buffer.Length - offset];
                testEmpty.Clear();
                AssertThat.Equal(buffer.Slice(offset), testEmpty);
            }
        }
示例#4
0
        public void TestReadString(
            string value,
            string encodingStr,
            bool isSafe,
            int fixedLength,
            int firstSize,
            int secondSize,
            int offset
            )
        {
            Span <byte> buffer = stackalloc byte[firstSize + secondSize];

            buffer.Clear();

            var encoding = EncodingHelpers.GetEncoding(encodingStr);

            var strLength = fixedLength > -1 ? Math.Min(value.Length, fixedLength) : value.Length;
            var chars     = value.AsSpan(0, strLength);

            encoding.GetBytes(chars, buffer[offset..]);