public void WriteByte() { // Verify that we can write bytes to a substream that spans the entire parent. using (var parent = new MemoryStream()) { parent.Write(new byte[10]); using (var substream = new SubStream(parent, 0, 10)) { for (int i = 0; i < 10; i++) { substream.WriteByte((byte)i); } } parent.Position = 0; var buffer = parent.ReadBytes(10); Assert.Equal(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, buffer); } // Verify that we can write bytes to a substream that is fully contained within the parent. using (var parent = new MemoryStream()) { parent.Write(new byte[10]); using (var substream = new SubStream(parent, 2, 5)) { for (int i = 0; i < 5; i++) { substream.WriteByte((byte)i); } } parent.Position = 0; var buffer = parent.ReadBytes(10); Assert.Equal(new byte[] { 0, 0, 0, 1, 2, 3, 4, 0, 0, 0 }, buffer); } // Verify that we can't write bytes past the end of the substream when // the substream spans the entire parent. using (var parent = new MemoryStream()) { parent.Write(new byte[10]); using (var substream = new SubStream(parent, 0, 10)) { for (int i = 0; i < 10; i++) { substream.WriteByte((byte)i); } Assert.Throws <IOException>(() => substream.WriteByte((byte)11)); } } // Verify that we can't write bytes past the end of the substream when // the substream that is fully contained within the parent. using (var parent = new MemoryStream()) { parent.Write(new byte[10]); using (var substream = new SubStream(parent, 2, 5)) { for (int i = 0; i < 5; i++) { substream.WriteByte((byte)i); } Assert.Throws <IOException>(() => substream.WriteByte((byte)6)); } } }