Пример #1
0
        public void MultiSegmentBytesReaderNumbers()
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 0 },
                new byte[] { 1, 2 },
                new byte[] { 3, 4 },
                new byte[] { 5, 6, 7, 8 },
                new byte[] { 8, 0 },
                new byte[] { 1, },
                new byte[] { 0, 2, },
                new byte[] { 1, 2, 3, 4 },
                new byte[] { 5, 6 },
                new byte[] { 7, 8, 9, },
                new byte[] { 0, 1, 2, 3 },
                new byte[] { 4, 5 },
                new byte[] { 6, 7, 8, 9 },
                new byte[] { 0, 1, 2, 3 },
                new byte[] { 4 },
            });

            ByteBufferReader reader = new ByteBufferReader(bytes);

            Assert.True(reader.TryReadTo(out ReadOnlySequence <byte> bytesValue, 2));
            Span <byte> span = bytesValue.ToArray();

            Assert.Equal(0, span[0]);
            Assert.Equal(1, span[1]);

            Assert.True(reader.TryReadTo(out bytesValue, 5));
            span = bytesValue.ToArray();
            Assert.Equal(3, span[0]);
            Assert.Equal(4, span[1]);

            Assert.True(reader.TryReadTo(out bytesValue, new byte[] { 8, 8 }));
            span = bytesValue.ToArray();
            Assert.Equal(6, span[0]);
            Assert.Equal(7, span[1]);

            //Assert.True(SequenceMarshal.TryRead(ref reader, out int intValue));
            Assert.True(reader.TryReadIntLE(out int intValue));
            Assert.Equal(BitConverter.ToInt32(new byte[] { 0, 1, 0, 2 }), intValue);

            Assert.True(reader.TryReadInt(out intValue));
            Assert.Equal(BitConverter.ToInt32(new byte[] { 4, 3, 2, 1 }), intValue);

            Assert.True(reader.TryReadLongLE(out long longValue));
            Assert.Equal(BitConverter.ToInt64(new byte[] { 5, 6, 7, 8, 9, 0, 1, 2 }), longValue);

            Assert.True(reader.TryReadLong(out longValue));
            Assert.Equal(BitConverter.ToInt64(new byte[] { 0, 9, 8, 7, 6, 5, 4, 3 }), longValue);

            Assert.True(reader.TryReadShortLE(out short shortValue));
            Assert.Equal(BitConverter.ToInt16(new byte[] { 1, 2 }), shortValue);

            Assert.True(reader.TryReadShort(out shortValue));
            Assert.Equal(BitConverter.ToInt16(new byte[] { 4, 3 }), shortValue);
        }