Esempio n. 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);
        }
Esempio n. 2
0
        public void Basics()
        {
            var buffer = Unpooled.Buffer(1024);
            var writer = new ByteBufferWriter(buffer);

            writer.WriteByte(1);
            writer.WriteShort(12);
            writer.WriteShortLE(21);

            writer.WriteMedium(13);
            writer.WriteMediumLE(31);

            writer.WriteInt(14);
            writer.WriteIntLE(41);
            writer.WriteLong(15);
            writer.WriteLongLE(51);

            writer.WriteDecimal(168.86m);
            writer.WriteDecimalLE(188.88m);

            writer.Flush();

            var reader = new ByteBufferReader(buffer);

            reader.TryRead(out byte b);
            Assert.Equal(1, b);
            reader.TryReadShort(out var sV);
            Assert.Equal(12, sV);
            reader.TryReadShortLE(out sV);
            Assert.Equal(21, sV);

            reader.TryReadMedium(out var iV);
            Assert.Equal(13, iV);
            reader.TryReadMediumLE(out iV);
            Assert.Equal(31, iV);

            reader.TryReadInt(out iV);
            Assert.Equal(14, iV);
            reader.TryReadIntLE(out iV);
            Assert.Equal(41, iV);
            reader.TryReadLong(out var lV);
            Assert.Equal(15, lV);
            reader.TryReadLongLE(out lV);
            Assert.Equal(51, lV);

            reader.TryReadDecimal(out var mV);
            Assert.Equal(168.86m, mV);
            reader.TryReadDecimalLE(out mV);
            Assert.Equal(188.88m, mV);
        }