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])); }); } }