public void ProduceTest()
        {
            var Buffer = new ProduceConsumeBuffer <int>();

            Buffer.Produce(new int[] { 10, 20, -2, 3, 15 });
            Assert.AreEqual(Buffer.IndexOf(-2), 2);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="outputStream"></param>
        /// <param name="sequence"></param>
        /// <param name="maxReaded"></param>
        /// <returns></returns>
        public long CopyUpToSequence(Stream outputStream, byte[] sequence, long maxReaded = long.MaxValue)
        {
            long totalCopied = 0;

            void Consume(int length)
            {
                if (length <= 0)
                {
                    return;
                }
                var consumedBytes = _buffer.Consume(length);

                outputStream?.Write(consumedBytes, 0, consumedBytes.Length);
                totalCopied += consumedBytes.Length;
            }

            while (true)
            {
                var tempBufferReaded = InputStream.Read(TempBuffer, 0,
                                                        (int)Math.Min(TempBuffer.Length, maxReaded - totalCopied));
                if (tempBufferReaded > 0)
                {
                    _buffer.Produce(TempBuffer, 0, tempBufferReaded);
                }

                if (_buffer.ConsumeRemaining <= sequence.Length)
                {
                    break;
                }

                var foundIndex = _buffer.IndexOf(sequence);

                // Not Found
                if (foundIndex == -1)
                {
                    Consume(_buffer.ConsumeRemaining - sequence.Length);
                }
                // Found!
                else
                {
                    Consume(foundIndex);
                    // Remove Sequence.
                    _buffer.Consume(sequence.Length);

                    return(totalCopied);
                }
            }

            Consume(_buffer.ConsumeRemaining);

            return(totalCopied);
        }
		public void ConsumeTest()
		{
			var Buffer = new ProduceConsumeBuffer<int>();
			Buffer.Produce(new int[] { 10, 20, -2, 3, 15 });
			Assert.AreEqual(Buffer.IndexOf(-2), 2);
			CollectionAssert.AreEqual(Buffer.Consume(2), new int[] { 10, 20 });
			Assert.AreEqual(Buffer.IndexOf(-2), 0);
			CollectionAssert.AreEqual(Buffer.Consume(1), new int[] { -2 });
			Assert.AreEqual(Buffer.IndexOf(-2), -1);
			Buffer.Produce(new int[] { 4, 2 });
			CollectionAssert.AreEqual(Buffer.Items, new int[] { 3, 15, 4, 2 });

			Assert.AreEqual(-1, Buffer.IndexOf(new int[] { 3, 15, 4, 1 }));
			Assert.AreEqual(0, Buffer.IndexOf(new int[] { 3, 15, 4 }));
			Assert.AreEqual(1, Buffer.IndexOf(new int[] { 15, 4 }));
			Assert.AreEqual(2, Buffer.IndexOf(new int[] { 4, 2 }));
		}
        public void ConsumeTest()
        {
            var Buffer = new ProduceConsumeBuffer <int>();

            Buffer.Produce(new int[] { 10, 20, -2, 3, 15 });
            Assert.AreEqual(Buffer.IndexOf(-2), 2);
            CollectionAssert.AreEqual(Buffer.Consume(2), new int[] { 10, 20 });
            Assert.AreEqual(Buffer.IndexOf(-2), 0);
            CollectionAssert.AreEqual(Buffer.Consume(1), new int[] { -2 });
            Assert.AreEqual(Buffer.IndexOf(-2), -1);
            Buffer.Produce(new int[] { 4, 2 });
            CollectionAssert.AreEqual(Buffer.Items, new int[] { 3, 15, 4, 2 });

            Assert.AreEqual(-1, Buffer.IndexOf(new int[] { 3, 15, 4, 1 }));
            Assert.AreEqual(0, Buffer.IndexOf(new int[] { 3, 15, 4 }));
            Assert.AreEqual(1, Buffer.IndexOf(new int[] { 15, 4 }));
            Assert.AreEqual(2, Buffer.IndexOf(new int[] { 4, 2 }));
        }
		public void ProduceTest()
		{
			var Buffer = new ProduceConsumeBuffer<int>();
			Buffer.Produce(new int[] { 10, 20, -2, 3, 15});
			Assert.AreEqual(Buffer.IndexOf(-2), 2);
		}