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