public void ThrowsIfConsumeIsLessThanZero() { var output = new ConsumableArrayBufferWriter(); WriteData(output, 1); Assert.Throws <ArgumentException>(() => output.Consume(-1)); }
public void ThrowsIfConsumePlusConsumedWouldOverflow() { var output = new ConsumableArrayBufferWriter(256); WriteData(output, 100); output.Consume(50); Assert.Throws <InvalidOperationException>(() => output.Consume(int.MaxValue)); }
public void ConsumeReducesUnconsumedWrittenCount() { var output = new ConsumableArrayBufferWriter(); WriteData(output, 2); Assert.Equal(2, output.UnconsumedWrittenCount); output.Consume(1); Assert.Equal(1, output.UnconsumedWrittenCount); }
public void ThrowsIfTotalConsumedIsGreaterThanWritten() { var output = new ConsumableArrayBufferWriter(); Assert.Throws <InvalidOperationException>(() => output.Consume(1)); WriteData(output, 2); output.Consume(1); output.Consume(1); Assert.Throws <InvalidOperationException>(() => output.Consume(1)); WriteData(output, 2); Assert.Throws <InvalidOperationException>(() => output.Consume(3)); }
public void IfConsumedEqualsWrittenFreeCapacityIsReset() { var output = new ConsumableArrayBufferWriter <T>(256); Assert.Equal(256, output.FreeCapacity); WriteData(output, 128); Assert.Equal(128, output.FreeCapacity); output.Consume(64); Assert.Equal(128, output.FreeCapacity); output.Consume(64); Assert.Equal(256, output.FreeCapacity); }
public void IfConsumedEqualsWritten_AndCapacityIsLarge_BackingArrayIsReturned() { var output = new ConsumableArrayBufferWriter(512); var capacity = output.Capacity; Assert.InRange(capacity, 512, int.MaxValue); Assert.Equal(capacity, output.FreeCapacity); WriteData(output, 128); Assert.Equal(capacity - 128, output.FreeCapacity); output.Consume(64); Assert.Equal(capacity - 128, output.FreeCapacity); output.Consume(64); Assert.Equal(0, output.Capacity); Assert.Equal(0, output.FreeCapacity); }
public void ConsumeIsNotIncludedInWrittenSpanOrMemory() { var output = new ConsumableArrayBufferWriter(); WriteData(output, 2); var oldSpan = output.WrittenSpan; var oldMemory = output.WrittenMemory; Assert.Equal(2, oldSpan.Length); Assert.Equal(2, oldMemory.Length); output.Consume(1); var newSpan = output.WrittenSpan; var newMemory = output.WrittenMemory; Assert.Equal(1, newSpan.Length); Assert.Equal(1, newMemory.Length); Assert.True(oldSpan[1..].SequenceEqual(newSpan));
public void IfConsumedEqualsWritten_AndCapacityIsSmall_FreeCapacityIsReset() { ConsumableArrayBufferWriter output; do { output = new ConsumableArrayBufferWriter(256); }while (output.Capacity >= 512); var capacity = output.Capacity; Assert.InRange(capacity, 256, 512); Assert.Equal(capacity, output.FreeCapacity); WriteData(output, 128); Assert.Equal(capacity - 128, output.FreeCapacity); output.Consume(64); Assert.Equal(capacity - 128, output.FreeCapacity); output.Consume(64); Assert.Equal(capacity, output.FreeCapacity); }