public void Test_Head() { var writer = new SliceWriter(64); var slice = writer.Head(0); Assert.That(slice.Count, Is.EqualTo(0)); Assert.That(slice.Offset, Is.EqualTo(0)); //note: slice.Array is not guaranteed to be equal to writer.Buffer Assert.That(() => writer.Head(1), Throws.InstanceOf <ArgumentOutOfRangeException>()); writer.WriteBytes(Slice.FromString("hello world!")); slice = writer.Head(5); Assert.That(slice.Array, Is.SameAs(writer.Buffer)); Assert.That(slice.Offset, Is.EqualTo(0)); Assert.That(slice.Count, Is.EqualTo(5)); Assert.That(slice.ToStringAscii(), Is.EqualTo("hello")); slice = writer.Head(12); Assert.That(slice.Array, Is.SameAs(writer.Buffer)); Assert.That(slice.Offset, Is.EqualTo(0)); Assert.That(slice.Count, Is.EqualTo(12)); Assert.That(slice.ToStringAscii(), Is.EqualTo("hello world!")); Assert.That(() => writer.Head(13), Throws.InstanceOf <ArgumentOutOfRangeException>()); Assert.That(() => writer.Head(-1), Throws.InstanceOf <ArgumentOutOfRangeException>()); writer.WriteBytes(Slice.FromString("foo")); slice = writer.Head(3); Assert.That(slice.Array, Is.SameAs(writer.Buffer)); Assert.That(slice.Offset, Is.EqualTo(0)); Assert.That(slice.Count, Is.EqualTo(3)); Assert.That(slice.ToStringAscii(), Is.EqualTo("hel")); slice = writer.Head(15); Assert.That(slice.Array, Is.SameAs(writer.Buffer)); Assert.That(slice.Offset, Is.EqualTo(0)); Assert.That(slice.Count, Is.EqualTo(15)); Assert.That(slice.ToStringAscii(), Is.EqualTo("hello world!foo")); Assert.That(() => writer.Head(16), Throws.InstanceOf <ArgumentOutOfRangeException>()); }