Ejemplo n.º 1
0
        public void TestPosition()
        {
            var rnd = new Random();

            for (var c = 0; c < count; c++)
            {
                var memSize = 10_000;
                var mem     = new byte[memSize * 10];
                var answer  = new byte[memSize];
                for (var i = 0; i < memSize; i++)
                {
                    answer[i] = (byte)rnd.Next(0, 10);
                }
                var span1 = new MemoryStreamer(mem);
                var span2 = new MemoryStreamer(mem);

                for (var i = 0; i < memSize; i++)
                {
                    span1.Position += answer[i];
                    span1.Write(answer[i]);
                }

                // Ensure span is not zero
                for (var i = 0; i < memSize; i++)
                {
                    span2.Position += answer[i];
                    var r = span2.ReadByte();
                    Assert.Equal(answer[i], r);
                }
            }
        }
Ejemplo n.º 2
0
        public void TestUInt64()
        {
            var rnd = new System.Random();

            for (var c = 0; c < count; c++)
            {
                var mem   = new byte[sizeof(UInt64) * writeRepeatCount];
                var span1 = new MemoryStreamer(mem);
                var span2 = new MemoryStreamer(mem);
                var a     = new UInt64[writeRepeatCount];

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var n = rnd.NextUInt64();
                    a[i] = n;
                    span1.Write(n);
                }

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var r = span2.ReadUInt64();
                    Assert.Equal(a[i], r);
                }
            }
        }
Ejemplo n.º 3
0
        public void TestSizedString()
        {
            var rnd = new Random();

            for (var c = 0; c < count; c++)
            {
                var memSize = rnd.Next(0, 1024);
                var mem     = new byte[(memSize + 4) * writeRepeatCount * 4];
                var answer  = new string[writeRepeatCount];
                var span1   = new MemoryStreamer(mem);
                var span2   = new MemoryStreamer(mem);

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var str = rnd.NextString("abcæøå诶	比西αβγ", memSize);
                    answer[i] = str;
                    span1.WriteSized(str);
                }

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var r = span2.ReadSizedString(out var len);
                    Assert.Equal(answer[i], r);
                }
            }
        }
Ejemplo n.º 4
0
        public void TestStreamReadWrite()
        {
            var rnd = new Random();

            for (var c = 0; c < count; c++)
            {
                var memSize = rnd.Next(1, 10_000);
                var mem     = new byte[(memSize + 4) * writeRepeatCount];
                var answer  = new byte[memSize * writeRepeatCount];
                rnd.NextBytes(answer);

                for (var bc = 1; bc < writeRepeatCount; bc++)
                {
                    for (var offset = 0; offset < bc; offset++)
                    {
                        var span1 = new MemoryStreamer(mem);
                        var span2 = new MemoryStreamer(mem);

                        span1.Write(answer, offset, bc - offset);
                        var buffer = new byte[bc + offset];
                        span2.Read(buffer, offset, bc - offset);

                        Assert.Equal(new Span <byte>(answer).Slice(offset, bc - offset).ToArray(), new Memory <byte>(buffer).Slice(offset, bc - offset).ToArray());
                    }
                }

                Assert.Throws <ArgumentNullException>(() =>
                {
                    var s = new MemoryStreamer(mem);
                    s.Read(null, 0, 1);
                });
            }
        }
Ejemplo n.º 5
0
        public void TestOtherStreamStuff()
        {
            var mem = new byte[2];
            var ms  = new MemoryStreamer(mem);

            Assert.Throws <ArgumentOutOfRangeException>(() => ms.SetLength(mem.Length + 1));
            ms.Flush();
        }
Ejemplo n.º 6
0
        public void TestLength()
        {
            var mem = new byte[100];
            var ss  = new MemoryStreamer(mem);

            Assert.Equal(100, ss.Length);
            Assert.Equal(100, ss.MaxLength);
            ss.SetLength(0);
            Assert.Equal(0, ss.Length);
            ss.Write((UInt32)0);
            Assert.Equal(4, ss.Length);
            ss.Clear();
            Assert.Equal(0, ss.Length);
        }
Ejemplo n.º 7
0
        public void TestSize()
        {
            var rnd = new System.Random();

            for (var c = 0; c < count; c++)
            {
                var mem   = new byte[sizeof(UInt32) * writeRepeatCount];
                var span1 = new MemoryStreamer(mem);
                var span2 = new MemoryStreamer(mem);
                var a     = new UInt32[writeRepeatCount];

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var  sr = rnd.Next(0, 4);
                    uint n;
                    switch (sr)
                    {
                    case 0:
                        n = (UInt32)rnd.Next(0, 0b00111111);
                        break;

                    case 1:
                        n = (UInt32)rnd.Next(0b01000000, 0b00111111_11111111);
                        break;

                    case 2:
                        n = (UInt32)rnd.Next(0b01000000_00000000, 0b00111111_11111111_11111111);
                        break;

                    //case 3:
                    default:
                        n = (UInt32)rnd.Next(0b01000000_00000000_00000000, 0b00111111_11111111_11111111_11111111);
                        break;
                    }

                    a[i] = n;
                    span1.WriteSize(n);
                }

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var r = span2.ReadSize(out var len);
                    Assert.Equal(span2.MeasureWriteSize(a[i]), len);
                    Assert.Equal(a[i], r);
                }
            }
        }
Ejemplo n.º 8
0
        public void TestCanReadCanWrite()
        {
            var mem = new byte[2];
            var s   = new Memory <byte>(mem);
            var ros = new ReadOnlyMemory <byte>(mem);

            var ss = new MemoryStreamer(s);

            Assert.True(ss.CanRead);
            Assert.True(ss.CanWrite);
            Assert.True(ss.CanSeek);
            var ross = new ReadOnlyMemoryStreamer(ros);

            Assert.True(ross.CanRead);
            Assert.False(ross.CanWrite);
            Assert.True(ross.CanSeek);
        }
Ejemplo n.º 9
0
        public void TestPosOverflow()
        {
            var mem = new byte[2];

            {
                var s  = new Memory <byte>(mem);
                var ss = new MemoryStreamer(s);
                ss.Position = mem.Length;
                Assert.Equal(mem.Length, ss.Position);
                ss.Position = 0;
                Assert.Equal(0, ss.Position);
            }
            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                var s       = new Memory <byte>(mem);
                var ss      = new MemoryStreamer(s);
                ss.Position = mem.Length + 1;
            });
        }
Ejemplo n.º 10
0
        public void TestSizedReadWriteSpan()
        {
            var rnd = new Random();

            for (var c = 0; c < count; c++)
            {
                var memSize = rnd.Next(1, 10_000);
                var mem     = new byte[(memSize + 4) * writeRepeatCount];
                var answer  = new byte[memSize * writeRepeatCount];
                rnd.NextBytes(answer);

                var span1 = new MemoryStreamer(mem);
                var span2 = new MemoryStreamer(mem);
                span1.WriteSized(new Span <byte>(answer));
                span1.Write(1234);

                var bytes = span2.ReadSizedBytes(out var length);
                Assert.Equal(answer, bytes);
                Assert.Equal(1234, span2.ReadInt32());
            }
        }
Ejemplo n.º 11
0
        public void TestReadOnlyMemory()
        {
            var rnd = new Random();

            for (var c = 0; c < count; c++)
            {
                var memSize = rnd.Next(1, 10_000);
                var mem     = new byte[(memSize + 4) * writeRepeatCount];
                var answer  = new byte[memSize * writeRepeatCount];
                rnd.NextBytes(answer);
                var span1 = new MemoryStreamer(mem);
                var span2 = new MemoryStreamer(mem);

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var a = new Span <byte>(answer).Slice(memSize * i, memSize);
                    span1.Write(a);
                }

                // Ensure span is not zero
                var ac = 0;
                for (var wrc = 0; wrc < writeRepeatCount; wrc++)
                {
                    var r = span2.ReadBytes(memSize);

                    for (var i = 0; i < r.Length; i++)
                    {
                        Assert.Equal(answer[ac++], r[i]);
                    }
                }

                Assert.Throws <ArgumentException>(() =>
                {
                    var s = new MemoryStreamer(mem);
                    s.Write(new Span <byte>(new byte[mem.Length + 1]));
                });
            }
        }
Ejemplo n.º 12
0
        public void TestGuid()
        {
            for (var c = 0; c < count; c++)
            {
                var mem   = new byte[16 * writeRepeatCount];
                var span1 = new MemoryStreamer(mem);
                var span2 = new MemoryStreamer(mem);
                var a     = new Guid[writeRepeatCount];

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var n = Guid.NewGuid();
                    a[i] = n;
                    span1.Write(n);
                }

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var r = span2.ReadGuid();
                    Assert.Equal(a[i], r);
                }
            }
        }
Ejemplo n.º 13
0
        public void TestVLQInt32()
        {
            var rnd = new Random();

            for (var c = 0; c < count; c++)
            {
                var memSize = rnd.Next(1, 10_000);
                var mem     = new byte[memSize * SpanUtils.MeasureVLQ(UInt32.MaxValue) + 1];
                var span1   = new MemoryStreamer(mem);
                var span2   = new MemoryStreamer(mem);

                var data = new Int32[memSize];
                for (var i = 0; i < memSize; i++)
                {
                    data[i] = rnd.NextInt32();
                }

                for (var i = 0; i < memSize; i++)
                {
                    span1.WriteVLQ(data[i]);
                }

                for (var i = 0; i < memSize; i++)
                {
                    Assert.Equal(data[i], span2.ReadVLQInt32(out var len));
                    Assert.Equal(SpanUtils.MeasureVLQ(data[i]), len);
                }

                // Check overflow
                new Span <byte>(mem).Fill(0xFF);
                Assert.Throws <OverflowException>(() =>
                {
                    var span3 = new MemoryStreamer(mem);
                    span3.ReadVLQInt32(out _);
                });
            }
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="maxSize">Maximum packet size. 5 bytes is used for header.</param>
 public MessageObject(int maxSize)
 {
     _buffer = new byte[maxSize];
     // User writable memory starts after header size
     Stream = new MemoryStreamer(new Memory <byte>(_buffer, 4, _buffer.Length - 5));
 }