Example #1
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 SpanStream(mem);
                var span2   = new SpanStream(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);
                }
            }
        }
Example #2
0
        public void TestSizedReadWriteReadOnlySpan()
        {
            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 + 10];
                var answer  = new byte[memSize * writeRepeatCount];
                rnd.NextBytes(answer);

                var span1 = new SpanStream(mem);
                var span2 = new ReadOnlySpanStream(new ReadOnlySpan <byte>(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());
            }
        }
Example #3
0
        public void TestSizedSpan()
        {
            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 SpanStream(mem);
                var span2 = new SpanStream(mem);

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

                var ac = 0;
                for (var wrc = 0; wrc < writeRepeatCount; wrc++)
                {
                    var r = span2.ReadSizedBytes(out var len);

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

                Assert.Throws <ArgumentException>(() =>
                {
                    var s = new SpanStream(mem);
                    s.WriteSized(new Span <byte>(new byte[mem.Length + 1]));
                });
            }
        }