Ejemplo n.º 1
0
        public void Basic()
        {
            const int Size = 64;

            buffer_ = new ReadWriteRingBuffer(Size);

            Assert.IsFalse(buffer_.IsFull);
            Assert.IsTrue(buffer_.IsEmpty);

            buffer_.WriteByte(1);

            Assert.IsFalse(buffer_.IsFull);
            Assert.IsFalse(buffer_.IsEmpty);
            Assert.AreEqual(1, buffer_.Count);

            Assert.AreEqual(1, buffer_.ReadByte());

            Assert.IsFalse(buffer_.IsFull);
            Assert.IsTrue(buffer_.IsEmpty);

            for (var i = 0; i < buffer_.Length; ++i)
            {
                buffer_.WriteByte(unchecked ((byte)(i & 0xff)));
            }

            Assert.IsTrue(buffer_.IsFull);
            Assert.IsFalse(buffer_.IsEmpty);

            buffer_.Close();

            Assert.IsTrue(buffer_.IsClosed);

            var caught = false;

            try
            {
                buffer_.WriteByte(1);
            }
            catch
            {
                caught = true;
            }

            Assert.IsTrue(caught);

            var count    = Size;
            var expected = 0;

            while (count != 0)
            {
                Assert.AreEqual(count, buffer_.Count);
                Assert.AreEqual(expected, buffer_.ReadByte());
                count--;
                expected = (expected + 1) & 0xff;
            }

            Assert.IsTrue(buffer_.IsEmpty);
            Assert.AreEqual(-1, buffer_.ReadByte());
        }
Ejemplo n.º 2
0
        public void Threaded()
        {
            buffer_     = new ReadWriteRingBuffer(8);
            readTarget_ = writeTarget_ = 16384;

            var reader = new Thread(Reader);

            reader.Start();

            var writer = new Thread(Writer);

            writer.Start();

            writer.Join();
            reader.Join();
        }
Ejemplo n.º 3
0
        public void Buffered()
        {
            const int Size = 64;

            buffer_ = new ReadWriteRingBuffer(Size);

            var writeBuffer = new byte[16];

            for (var i = 0; i < 16; ++i)
            {
                writeBuffer[i] = (byte)i;
            }

            buffer_.Write(writeBuffer, 0, 3);
            Assert.AreEqual(3, buffer_.Count);

            var readBuffer = new byte[16];

            Assert.AreEqual(3, buffer_.Read(readBuffer, 0, 3));
            for (var i = 0; i < 3; ++i)
            {
                Assert.AreEqual(i, readBuffer[i]);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WindowedStream"/> class.
 /// </summary>
 /// <param name="size">The size.</param>
 public WindowedStream(int size)
 {
     ringBuffer = new ReadWriteRingBuffer(size);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WindowedStream"/> class.
 /// </summary>
 /// <param name="size">The size.</param>
 /// <param name="token">The cancellation token.</param>
 public WindowedStream(int size, CancellationToken?token = null)
 {
     ringBuffer = new ReadWriteRingBuffer(size, token);
 }