public void CopyToWithAllocations() { using (var context = JsonOperationContext.ShortTermSingleUse()) { var allocation = context.GetMemory(DefaultBufferSize); using (var buffer = new UnmanagedWriteBuffer(context, allocation)) { Assert.Equal(buffer.SizeInBytes, 0); buffer.Write(NoAllocationsBatch, 0, NoAllocationsBatch.Length); Assert.Equal(buffer.SizeInBytes, NoAllocationsBatch.Length); for (var i = 0; i < NoAllocationsBatch.Length; i++) { Assert.Equal(allocation.Address[i], NoAllocationsBatch[i]); } byte[] outputBuffer = new byte[NoAllocationsBatch.Length]; fixed(byte *outputBufferPtr = outputBuffer) buffer.CopyTo(outputBufferPtr); for (var i = 0; i < NoAllocationsBatch.Length; i++) { Assert.Equal(outputBuffer[i], NoAllocationsBatch[i]); } } } }
public void ClearResetsSizeAndEffectivellyClears() { using (var context = JsonOperationContext.ShortTermSingleUse()) { var allocation = context.GetMemory(DefaultBufferSize); using (var buffer = new UnmanagedWriteBuffer(context, allocation)) { Assert.Equal(buffer.SizeInBytes, 0); buffer.Write(AllocationsBatch, 0, AllocationsBatch.Length); Assert.Equal(buffer.SizeInBytes, AllocationsBatch.Length); buffer.Clear(); Assert.Equal(buffer.SizeInBytes, 0); byte[] outputBuffer = new byte[AllocationsBatch.Length]; for (var i = 0; i < outputBuffer.Length; i++) { outputBuffer[i] = 124; fixed(byte *outputBufferPtr = outputBuffer) buffer.CopyTo(outputBufferPtr); foreach (var b in outputBuffer) { Assert.Equal(b, 124); } } } }
public void CopyToThrowsAfterDispose() { using (var context = JsonOperationContext.ShortTermSingleUse()) { var buffer = new UnmanagedWriteBuffer(context, context.GetMemory(DefaultBufferSize)); buffer.Dispose(); var outputBuffer = new byte[DefaultBufferSize]; fixed(byte *outputBufferPtr = outputBuffer) { #if DEBUG try { buffer.CopyTo(outputBufferPtr); Assert.False(true); } catch (ObjectDisposedException) { } catch (Exception) { Assert.False(true); } #else try { buffer.CopyTo(outputBufferPtr); Assert.False(true); } catch (NullReferenceException) { } catch (Exception) { Assert.False(true); } #endif } } }
public void EnsureSingleChunkDoesNotChangeSizeOrContentsWithAllocations() { using (var context = JsonOperationContext.ShortTermSingleUse()) { using (var buffer = new UnmanagedWriteBuffer(context, context.GetMemory(DefaultBufferSize))) { buffer.Write(AllocationsBatch, 0, AllocationsBatch.Length); buffer.EnsureSingleChunk(out byte *address, out int size); Assert.Equal(size, AllocationsBatch.Length); for (int i = 0; i < AllocationsBatch.Length; i++) { Assert.Equal(address[i], AllocationsBatch[i]); } var outputBuffer = new byte[AllocationsBatch.Length]; fixed(byte *outputBufferPtr = outputBuffer) buffer.CopyTo(outputBufferPtr); for (int i = 0; i < AllocationsBatch.Length; i++) Assert.Equal(outputBuffer[i], AllocationsBatch[i]); } } }