예제 #1
0
        public void MultiSegmentBytesReaderNumbers()
        {
            ReadOnlyBytes bytes = ListHelper.CreateRob(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 },
            });

            var reader = BytesReader.Create(bytes);

            var span = bytes.Slice(reader.ReadRange(2)).ToSpan();

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

            span = bytes.Slice(reader.ReadRange(5)).ToSpan();
            Assert.Equal(3, span[0]);
            Assert.Equal(4, span[1]);

            span = bytes.Slice(reader.ReadRange(new byte[] { 8, 8 })).ToSpan();
            Assert.Equal(6, span[0]);
            Assert.Equal(7, span[1]);

            Assert.True(reader.TryRead(out int value, true));
            Assert.Equal(BitConverter.ToInt32(new byte[] { 0, 1, 0, 2 }), value);

            Assert.True(reader.TryRead(out value));
            Assert.Equal(BitConverter.ToInt32(new byte[] { 4, 3, 2, 1 }), value);
        }
예제 #2
0
        static void BytesReaderBenchmarkBaseline()
        {
            int           sections = 10;
            StringBuilder sb       = new StringBuilder();

            for (int i = 0; i < sections; i++)
            {
                sb.Append("123456789012345678\r\n");
            }
            var data = Encoding.UTF8.GetBytes(sb.ToString());

            var eol   = new Span <byte>(s_eol);
            var bytes = new ReadOnlyBytes(data);

            var reader = BytesReader.Create(bytes);

            while (true)
            {
                var range = reader.ReadRange(eol);
                if (range.To == Position.End)
                {
                    break;
                }
            }
        }
예제 #3
0
        public void EmptyBytesReader()
        {
            ReadOnlyBytes bytes  = Create("");
            var           reader = BytesReader.Create(bytes);

            Assert.False(reader.TryReadBytes(out var range, (byte)' '));

            bytes  = Parse("|");
            reader = BytesReader.Create(bytes);
            Assert.False(reader.TryReadBytes(out range, (byte)' '));
        }
예제 #4
0
        public void EmptyBytesReader()
        {
            ReadOnlyBytes bytes  = Create("");
            var           reader = BytesReader.Create(bytes);
            var           range  = reader.ReadRange((byte)' ');

            Assert.Equal(Position.End, range.To);

            bytes  = Parse("|");
            reader = BytesReader.Create(bytes);
            range  = reader.ReadRange((byte)' ');
            Assert.Equal(Position.End, range.To);
        }
예제 #5
0
    static void ParseInt32BytesReader()
    {
        foreach (var iteration in Benchmark.Iterations)
        {
            var reader = BytesReader.Create(s_bytes);

            using (iteration.StartMeasurement()) {
                while (reader.TryParse(out int value))
                {
                    reader.Advance(1);
                }
            }
        }
    }
예제 #6
0
        public void MultiSegmentBytesReader()
        {
            ReadOnlyBytes bytes  = Parse("A|B |CD|#EF&|&");
            var           reader = BytesReader.Create(bytes);

            Assert.True(reader.TryReadBytes(out var ab, (byte)' '));
            Assert.Equal("AB", ab.Utf8ToString());

            Assert.True(reader.TryReadBytes(out var cd, (byte)'#'));
            Assert.Equal("CD", cd.Utf8ToString());

            //Assert.True(reader.TryReadBytes(out var ef, new byte[] { (byte)'&', (byte)'&' }));
            //Assert.Equal("EF", ef.ToString(SymbolTable.InvariantUtf8));
        }
예제 #7
0
        public void SingleSegmentBytesReader()
        {
            ReadOnlyBytes bytes  = Create("AB CD#EF&&");
            var           reader = BytesReader.Create(bytes);

            Assert.True(reader.TryReadBytes(out var ab, (byte)' '));
            Assert.Equal("AB", ab.ToString(SymbolTable.InvariantUtf8));

            Assert.True(reader.TryReadBytes(out var cd, (byte)'#'));
            Assert.Equal("CD", cd.ToString(SymbolTable.InvariantUtf8));

            Assert.True(reader.TryReadBytes(out var ef, new byte[] { (byte)'&', (byte)'&' }));
            Assert.Equal("EF", ef.ToString(SymbolTable.InvariantUtf8));
        }
예제 #8
0
        public void MultiSegmentBytesReader()
        {
            ReadOnlyBytes bytes  = Parse("A|B |CD|#EF&|&");
            var           reader = BytesReader.Create(bytes);

            var ab = bytes.Slice(reader.ReadRange((byte)' '));

            Assert.Equal("AB", ab.ToString(SymbolTable.InvariantUtf8));

            var cd = bytes.Slice(reader.ReadRange((byte)'#'));

            Assert.Equal("CD", cd.ToString(SymbolTable.InvariantUtf8));

            var ef = bytes.Slice(reader.ReadRange(new byte[] { (byte)'&', (byte)'&' }));

            Assert.Equal("EF", ef.ToString(SymbolTable.InvariantUtf8));
        }
예제 #9
0
        public void BytesReaderParse()
        {
            ReadOnlyBytes bytes  = Parse("12|3Tr|ue|456Tr|ue7|89False|");
            var           reader = BytesReader.Create(bytes);

            Assert.True(reader.TryParse(out ulong u64));
            Assert.Equal(123ul, u64);

            Assert.True(reader.TryParse(out bool b));
            Assert.Equal(true, b);

            Assert.True(reader.TryParse(out u64));
            Assert.Equal(456ul, u64);

            Assert.True(reader.TryParse(out b));
            Assert.Equal(true, b);

            Assert.True(reader.TryParse(out u64));
            Assert.Equal(789ul, u64);

            Assert.True(reader.TryParse(out b));
            Assert.Equal(false, b);
        }
예제 #10
0
        static void BytesReaderBenchmarkBaseline()
        {
            int           sections = 10;
            StringBuilder sb       = new StringBuilder();

            for (int i = 0; i < sections; i++)
            {
                sb.Append("1234 ");
            }
            var data = Encoding.UTF8.GetBytes(sb.ToString());

            var readOnlyBytes = new ReadOnlyBytes(data);
            var bytesRange    = new ReadOnlyBytes(data);

            var robReader = BytesReader.Create(readOnlyBytes);

            long robSum = 0;

            while (robReader.TryParse(out int value))
            {
                robSum += value;
                robReader.Advance(1);
            }

            var  brReader = BytesReader.Create(bytesRange);
            long brSum    = 0;

            while (brReader.TryParse(out int value))
            {
                brSum += value;
                brReader.Advance(1);
            }

            Assert.Equal(robSum, brSum);
            Assert.NotEqual(brSum, 0);
        }