本文整理汇总了C#中RingBuffer.Write方法的典型用法代码示例。如果您正苦于以下问题:C# RingBuffer.Write方法的具体用法?C# RingBuffer.Write怎么用?C# RingBuffer.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RingBuffer
的用法示例。
在下文中一共展示了RingBuffer.Write方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FillBufferNoMoreItemsAreAdded
public void FillBufferNoMoreItemsAreAdded()
{
//Arrange
const int capacity = 256;
RingBuffer<object> ringBuffer = new RingBuffer<object>(capacity, i => new RingBufferItem<object>(i));
Assert.AreEqual(256, ringBuffer.Capacity);
//act
for (int i = 0; i < capacity; i++)
{
var indexUsed = ringBuffer.Write(new object());
Assert.AreEqual(i, indexUsed, "The write index should match the loop index");
}
//Assert
var index = ringBuffer.Write(new object());
Assert.AreEqual(-1, index);
}
示例2: TestByteRingBuffer
public void TestByteRingBuffer()
{
var CurrentEncoding = Encoding.UTF8;
int BufferSize = 1024;
var Buffer = new RingBuffer<byte>(BufferSize);
// Initial check
Assert.AreEqual(BufferSize, Buffer.AvailableForWrite);
Assert.AreEqual(0, Buffer.AvailableForRead);
// Try peek
CollectionAssert.AreEqual(new byte[0], Buffer.PeekGet(16));
// Write
var DataToWrite = CurrentEncoding.GetBytes("Hello World");
Buffer.Write(DataToWrite);
Assert.AreEqual(BufferSize - DataToWrite.Length, Buffer.AvailableForWrite);
Assert.AreEqual(0 + DataToWrite.Length, Buffer.AvailableForRead);
}
示例3: ConcurrentReadWrite
public void ConcurrentReadWrite()
{
//Arrange
const int capacity = 128;
RingBuffer<object> ringBuffer = new RingBuffer<object>(capacity, i => new RingBufferItem<object>(i));
AutoResetEvent resetEvent = new AutoResetEvent(false);
Action writerAction = () =>
{
for (int i = 0; i < capacity; i++)
{
int writeIndex = ringBuffer.Write(new object());
Assert.AreEqual(i, writeIndex);
resetEvent.Set();
}
};
Action readerAction = () =>
{
for (int i = 0; i < capacity; i++)
{
int readIndex;
object read = ringBuffer.Read(out readIndex);
if (readIndex < 0)
{
resetEvent.WaitOne(100);
read = ringBuffer.Read(out readIndex);
}
Assert.AreEqual(i, readIndex, "readIndex does not equal loop index.");
Assert.IsNotNull(read, "read object in null");
}
};
Parallel.Invoke(writerAction, readerAction);
}
示例4: FillBufferThenReadHalfThenWriteHalf
public void FillBufferThenReadHalfThenWriteHalf()
{
//Arrange
const int capacity = 256;
RingBuffer<object> ringBuffer = new RingBuffer<object>(capacity, i => new RingBufferItem<object>(i));
Assert.AreEqual(256, ringBuffer.Capacity);
//act
for (int i = 0; i < capacity; i++)
{
var indexUsed = ringBuffer.Write(new object());
Assert.AreEqual(i, indexUsed, "The write index should match the loop index");
}
//The buffer should be full, so no more writes are allowed
var index = ringBuffer.Write(new object());
Assert.AreEqual(-1, index);
for (int i = 0; i < capacity / 2; i++)
{
int readIndex;
var item = ringBuffer.Read(out readIndex);
Assert.IsNotNull(item, "Item should not be null");
Assert.AreEqual(i, readIndex, "The Read index should be same as the loop index");
}
//Write again from the beginning
for (int i = 0; i < capacity / 2; i++)
{
var indexUsed = ringBuffer.Write(new object());
Assert.AreEqual(i, indexUsed, "The write index should match the loop index");
}
//Assert
index = ringBuffer.Write(new object());
Assert.AreEqual(-1, index);
}
示例5: FillHalfTheBufferThenReadAll
public void FillHalfTheBufferThenReadAll()
{
//Arrange
const int capacity = 256;
RingBuffer<object> ringBuffer = new RingBuffer<object>(capacity, index => new RingBufferItem<object>(index));
Assert.AreEqual(256, ringBuffer.Capacity);
//act
for (int j = 0; j < capacity / 2; j++)
{
var indexUsed = ringBuffer.Write(new object());
Assert.AreEqual(j, indexUsed, "The write index should match the loop index");
}
int i;
int readIndex = 0;
for (i = 0; i < capacity; i++)
{
var item = ringBuffer.Read(out readIndex);
if (readIndex == -1)
break;
Assert.IsNotNull(item);
Assert.AreEqual(i, readIndex, "The Read index should be same as the loop index");
}
//Assert
Assert.AreEqual(-1, readIndex);
Assert.AreEqual(capacity / 2, i);
}