Esempio n. 1
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);
                }
            }
        }
Esempio n. 2
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);
                }
            }
        }
Esempio n. 3
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);
                });
            }
        }
Esempio n. 4
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);
        }
Esempio n. 5
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());
            }
        }
Esempio n. 6
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]));
                });
            }
        }
Esempio n. 7
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);
                }
            }
        }