public void Test_AppendBytes_Resize_Buffer() { // check buffer resize occurs as intended var original = new byte[32]; var writer = new SliceWriter(original); Assert.That(writer.Buffer, Is.SameAs(original)); // first write should not resize the buffer var aaa = writer.AppendBytes(Slice.Repeat((byte)'a', 24)); Assert.That(aaa.Array, Is.SameAs(original)); // second write should resize the buffer var bbb = writer.AppendBytes(Slice.Repeat((byte)'b', 24)); Assert.That(bbb.Array, Is.SameAs(writer.Buffer)); //note: buffer should have been copied between both calls, so 'aaa' should point to the OLD buffer Assert.That(bbb.Array, Is.Not.SameAs(original)); //but the content should be unchanged Assert.That(writer.ToSlice().ToStringAscii(), Is.EqualTo("aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbb")); // => mutating aaa should not change the buffer aaa.Array[aaa.Offset] = (byte)'Z'; Assert.That(writer.ToSlice().ToStringAscii(), Is.EqualTo("aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbb")); // => but mutating bbb should change the buffer bbb.Array[bbb.Offset] = (byte)'Z'; Assert.That(writer.ToSlice().ToStringAscii(), Is.EqualTo("aaaaaaaaaaaaaaaaaaaaaaaaZbbbbbbbbbbbbbbbbbbbbbbb")); }
public void Test_AppendBytes() { var writer = new SliceWriter(64); var slice = writer.AppendBytes(Slice.Empty); //note: slice.Array is not guaranteed to be equal to writer.Buffer Assert.That(slice.Offset, Is.EqualTo(0)); Assert.That(slice.Count, Is.EqualTo(0)); slice = writer.AppendBytes(Slice.FromString("hello world!")); 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.ToStringUtf8(), Is.EqualTo("hello world!")); Assert.That(writer.ToSlice().ToStringUtf8(), Is.EqualTo("hello world!")); var foo = Slice.FromString("foo"); slice = writer.AppendBytes(foo); Assert.That(slice.Array, Is.SameAs(writer.Buffer)); Assert.That(slice.Offset, Is.EqualTo(12)); Assert.That(slice.Count, Is.EqualTo(3)); Assert.That(slice.ToStringUtf8(), Is.EqualTo("foo")); Assert.That(writer.ToSlice().ToStringUtf8(), Is.EqualTo("hello world!foo")); var bar = Slice.FromString("bar"); unsafe { fixed(byte *ptr = &bar.DangerousGetPinnableReference()) { slice = writer.AppendBytes(ptr, 3); } } Assert.That(slice.Array, Is.SameAs(writer.Buffer)); Assert.That(slice.Offset, Is.EqualTo(15)); Assert.That(slice.Count, Is.EqualTo(3)); Assert.That(slice.ToStringUtf8(), Is.EqualTo("bar")); Assert.That(writer.ToSlice().ToStringUtf8(), Is.EqualTo("hello world!foobar")); var baz = Slice.FromString("baz"); unsafe { fixed(byte *ptr = &baz.DangerousGetPinnableReference()) { //TODO: this test was using ReadOnlySpan<byte>, update it once we enable support for these! slice = writer.AppendBytes(ptr, 3); } } Assert.That(slice.Array, Is.SameAs(writer.Buffer)); Assert.That(slice.Offset, Is.EqualTo(18)); Assert.That(slice.Count, Is.EqualTo(3)); Assert.That(slice.ToStringUtf8(), Is.EqualTo("baz")); Assert.That(writer.ToSlice().ToStringUtf8(), Is.EqualTo("hello world!foobarbaz")); unsafe { slice = writer.AppendBytes(null, 0); } //note: slice.Array is not guaranteed to be equal to writer.Buffer Assert.That(slice.Offset, Is.EqualTo(0)); //REVIEW: should we return (Buffer, Position, 0) instead of (EmptyArray, 0, 0) ? Assert.That(slice.Count, Is.EqualTo(0)); }