public void CopyToTests() { byte[] array = new byte[16] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; SpanByte span = new SpanByte(array); // First a copy to with the full span SpanByte toCopy = new byte[span.Length]; span.CopyTo(toCopy); for (int i = 0; i < span.Length; i++) { Assert.Equal(toCopy[i], span[i], "SpanByte value should be the same as from the original array"); } // Now create a larger span toCopy = new byte[span.Length + 1]; span.CopyTo(toCopy); for (int i = 0; i < span.Length; i++) { Assert.Equal(toCopy[i], span[i], "SpanByte value should be the same as from the original array"); } Assert.Equal(toCopy[span.Length], (byte)0); }
public void EmptySpanTests() { // Empty span SpanByte span = SpanByte.Empty; // Create a destination span larger SpanByte destination = new byte[1]; span.CopyTo(destination); // Now also empty destination = SpanByte.Empty; span.CopyTo(destination); }
public void RaisingExceptionsOfAllKindsTests() { // Should raise an exception on creation Assert.Throws(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(null, 1, 2); }, "ArgumentOutOfRangeException when array is null, start is 1 and length is 2"); Assert.Throws(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(new byte[1], 1, 2); }, "ArgumentOutOfRangeException when array is new byte[1], start is 1 and length is 2"); Assert.Throws(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(new byte[1], 0, 2); }, "ArgumentOutOfRangeException when array is new byte[1], start is 0 and length is 2"); Assert.Throws(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(new byte[1], 2, 0); }, "ArgumentOutOfRangeException when array is new byte[1], start is 2 and length is 0"); // Exception on index access byte[] array = new byte[16] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; Assert.Throws(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(array); var data = span[span.Length]; }); Assert.Throws(typeof(IndexOutOfRangeException), () => { SpanByte span = new SpanByte(array); var data = span[-1]; }); // Copy to with too small destination Assert.Throws(typeof(ArgumentException), () => { SpanByte span = new SpanByte(array); SpanByte destination = new byte[span.Length - 1]; span.CopyTo(destination); }); // Slicing arguments Assert.Throws(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(array); var sliced = span.Slice(span.Length + 1); }); Assert.Throws(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(array); var sliced = span.Slice(1, span.Length); }); Assert.Throws(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(array); var sliced = span.Slice(-1, span.Length); }); Assert.Throws(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(array); var sliced = span.Slice(1, -1); }); }
public unsafe void SpanByteUnitTest1() { Span <byte> payload = stackalloc byte[20]; Span <byte> serialized = stackalloc byte[24]; SpanByte sb = SpanByte.FromFixedSpan(payload); Assert.IsFalse(sb.Serialized); Assert.AreEqual(20, sb.Length); Assert.AreEqual(24, sb.TotalSize); Assert.AreEqual(20, sb.AsSpan().Length); Assert.AreEqual(20, sb.AsReadOnlySpan().Length); fixed(byte *ptr = serialized) sb.CopyTo(ptr); ref SpanByte ssb = ref SpanByte.ReinterpretWithoutLength(serialized);
/// <inheritdoc/> public override void InitialUpdater(ref SpanByte key, ref SpanByte input, ref SpanByte value) { input.CopyTo(ref value); }
/// <inheritdoc /> public override void SingleWriter(ref ulong key, ref SpanByte src, ref SpanByte dst) { src.CopyTo(ref dst); }
/// <inheritdoc/> public override bool InitialUpdater(ref SpanByte key, ref SpanByte input, ref SpanByte value, ref SpanByteAndMemory output, ref RMWInfo rmwInfo) { input.CopyTo(ref value); return(true); }