Exemplo n.º 1
0
        /// <summary>
        /// Applies Capacity+1 times the same test to the same buffer "logical" content but with
        /// different internal offsets each time.
        /// The "logical" initial buffer content is restored each time and at the end of the test.
        /// </summary>
        static void TestWithInternalOffsets <T>(FIFOBuffer <T> f, Action <FIFOBuffer <T> > testPredicate)
        {
            var saved = f.ToArray();

            for (int iTry = 0; iTry <= f.Capacity; ++iTry)
            {
                f.Clear();
                for (int i = 0; i < iTry; ++i)
                {
                    f.Push(default(T));
                }
                foreach (var i in saved)
                {
                    f.Push(i);
                }
                while (f.Count > saved.Length)
                {
                    f.Pop();
                }
                f.SequenceEqual(saved).Should().BeTrue();
                testPredicate(f);
            }
            foreach (var i in saved)
            {
                f.Push(i);
            }
            f.Truncate(saved.Length);
        }
Exemplo n.º 2
0
 static void CheckOneValueType <T>(FIFOBuffer <T> f, T value, T otherValue) where T : struct
 {
     CheckOnlyOneValueType <T>(f, value, otherValue);
     f.Pop().Should().Be(value);
     AssertEmpty(f);
     f.Push(value);
     CheckOnlyOneValueType <T>(f, value, otherValue);
 }
Exemplo n.º 3
0
 static void CheckOneValueType <T>(FIFOBuffer <T> f, T value, T otherValue) where T : struct
 {
     CheckOnlyOneValueType <T>(f, value, otherValue);
     Assert.That(f.Pop(), Is.EqualTo(value));
     AssertEmpty(f);
     f.Push(value);
     CheckOnlyOneValueType <T>(f, value, otherValue);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Sends the log entry: creates the sender if necessary and manages the buffer.
        /// </summary>
        /// <param name="monitor">The monitor to use.</param>
        /// <param name="logEvent">The log entry to send.</param>
        /// <returns>The awaitable.</returns>
        /// <exception cref="CKException">
        /// If the sender has not been created yet and the application identity is ready
        /// but CreateSenderAsync returned null: the exception will remove this handler from the sink's handler list.
        /// </exception>
        public virtual async ValueTask HandleAsync(IActivityMonitor monitor, IMulticastLogEntry logEvent)
        {
            if (_sender == null && SenderCanBeCreated)
            {
                _sender = await CreateSenderAsync(monitor);

                if (_sender == null)
                {
                    throw new CKException($"Unable to create the sender.");
                }
            }
            if (_sender != null)
            {
                if (_buffer.Count > 0)
                {
                    while (_buffer.Count > 0)
                    {
                        if (!_sender.IsActuallyConnected || !await _sender.TrySendAsync(monitor, _buffer.Peek()))
                        {
                            _buffer.Push(logEvent);
                            return;
                        }
                        _buffer.Pop();
                    }
                }
                if (!_sender.IsActuallyConnected || !await _sender.TrySendAsync(monitor, logEvent))
                {
                    _buffer.Push(logEvent);
                }
                else if (!_firstSuccess)
                {
                    _firstSuccess = true;
                    if (_buffer.Capacity != _config.LostBufferSize)
                    {
                        monitor.Info($"Successfully sent the first logs: resizing buffer from '{_buffer.Capacity}' to '{_config.LostBufferSize}'.");
                        _buffer.Capacity = _config.LostBufferSize;
                    }
                }
            }
            else
            {
                _buffer.Push(logEvent);
            }
        }
Exemplo n.º 5
0
        public void FIFO_supports_Null_entries()
        {
            var c0 = CultureInfo.InvariantCulture;
            var c1 = this;

            var f = new FIFOBuffer <object>(2);

            AssertEmpty(f);

            // When calling with null, it is the IndexOf( T ) that is called
            // since T is a reference type.
            int iNull = f.IndexOf(null);

            iNull.Should().BeLessThan(0);

            f.Push(c0);
            f.Contains(null).Should().BeFalse();
            f.IndexOf(null).Should().BeLessThan(0);
            f.PeekLast().Should().BeSameAs(c0);
            AssertContains(f, c0);

            f.Push(null);
            f.Count.Should().Be(2);
            f.IndexOf(null).Should().Be(1);
            f.IndexOf(c0).Should().Be(0);
            f.PeekLast().Should().BeNull();
            AssertContains(f, c0, null);

            f.Push(c1);
            f.IndexOf(null).Should().Be(0);
            f.IndexOf(c1).Should().Be(1);
            f.Contains(c0).Should().BeFalse();
            f.IndexOf(c0).Should().BeLessThan(0);
            f.PeekLast().Should().BeSameAs(c1);
            AssertContains(f, null, c1);

            f.Push(null);
            AssertContains(f, c1, null);
            f.Push(null);
            AssertContains(f, null, null);
            f.PopLast().Should().BeNull();
            f.PopLast().Should().BeNull();
            f.Invoking(sut => sut.PopLast()).Should().Throw <InvalidOperationException>();
        }
Exemplo n.º 6
0
        public void FIFO_supports_Null_entries()
        {
            var c0 = CultureInfo.InvariantCulture;
            var c1 = this;

            var f = new FIFOBuffer <object>(2);

            AssertEmpty(f);

            // When calling with null, it is the IndexOf( T ) that is called
            // since T is a reference type.
            int iNull = f.IndexOf(null);

            Assert.That(iNull, Is.LessThan(0));

            f.Push(c0);
            Assert.That(f.Contains(null), Is.False);
            Assert.That(f.IndexOf(null), Is.LessThan(0));
            Assert.That(f.PeekLast(), Is.SameAs(c0));
            AssertContains(f, c0);

            f.Push(null);
            Assert.That(f.Count, Is.EqualTo(2));
            Assert.That(f.IndexOf(null), Is.EqualTo(1));
            Assert.That(f.IndexOf(c0), Is.EqualTo(0));
            Assert.That(f.PeekLast(), Is.Null);
            AssertContains(f, c0, null);

            f.Push(c1);
            Assert.That(f.IndexOf(null), Is.EqualTo(0));
            Assert.That(f.IndexOf(c1), Is.EqualTo(1));
            Assert.That(f.Contains(c0), Is.False);
            Assert.That(f.IndexOf(c0), Is.LessThan(0));
            Assert.That(f.PeekLast(), Is.SameAs(c1));
            AssertContains(f, null, c1);

            f.Push(null);
            AssertContains(f, c1, null);
            f.Push(null);
            AssertContains(f, null, null);
            Assert.That(f.PopLast(), Is.Null);
            Assert.That(f.PopLast(), Is.Null);
            Assert.Throws <InvalidOperationException>(() => f.PopLast());
        }
Exemplo n.º 7
0
        public void FIFO_with_one_and_only_one_Value_Type()
        {
            FIFOBuffer <int> f = new FIFOBuffer <int>(1);

            AssertEmpty(f);

            f.Push(5);
            CheckOneValueType(f, 5, 50);
            f.Pop();
            f.Push(0);
            CheckOneValueType(f, 0, 5);
            f.Push(1);
            CheckOneValueType(f, 1, 0);
            f.Push(2);
            CheckOneValueType(f, 2, 0);

            int iType  = f.IndexOf(2);
            int iBoxed = f.IndexOf((object)2);

            iType.Should().Be(iBoxed);
        }
Exemplo n.º 8
0
        public void FIFOOneValueType()
        {
            FIFOBuffer <int> f = new FIFOBuffer <int>(1);

            AssertEmpty(f);

            f.Push(5);
            CheckOneValueType(f, 5, 50);
            f.Pop();
            f.Push(0);
            CheckOneValueType(f, 0, 5);
            f.Push(1);
            CheckOneValueType(f, 1, 0);
            f.Push(2);
            CheckOneValueType(f, 2, 0);

            int iType  = f.IndexOf(2);
            int iBoxed = f.IndexOf((object)2);

            Assert.That(iType == iBoxed);
        }
Exemplo n.º 9
0
        public void FIFOSupportNull()
        {
            CultureInfo c0 = CultureInfo.InvariantCulture;
            CultureInfo c1 = CultureInfo.GetCultureInfo("fr");

            FIFOBuffer <CultureInfo> f = new FIFOBuffer <CultureInfo>(2);

            AssertEmpty(f);

            // When calling with null, it is the IndexOf( T ) that is called
            // since T is a reference type.
            int iNull = f.IndexOf(null);

            Assert.That(iNull, Is.LessThan(0));

            f.Push(c0);
            Assert.That(f.Contains(null), Is.False);
            Assert.That(f.IndexOf(null), Is.LessThan(0));
            AssertContains(f, c0);

            f.Push(null);
            Assert.That(f.Count, Is.EqualTo(2));
            Assert.That(f.IndexOf(null), Is.EqualTo(1));
            Assert.That(f.IndexOf(c0), Is.EqualTo(0));
            AssertContains(f, c0, null);

            f.Push(c1);
            Assert.That(f.IndexOf(null), Is.EqualTo(0));
            Assert.That(f.IndexOf(c1), Is.EqualTo(1));
            Assert.That(f.Contains(c0), Is.False);
            Assert.That(f.IndexOf(c0), Is.LessThan(0));
            AssertContains(f, null, c1);

            f.Push(null);
            AssertContains(f, c1, null);
            f.Push(null);
            AssertContains(f, null, null);
        }
Exemplo n.º 10
0
        public void FIFO_supports_Peek_and_PeekLast()
        {
            FIFOBuffer <int> f = new FIFOBuffer <int>(0);

            f.Invoking(sut => Console.Write(sut[-1])).Should().Throw <IndexOutOfRangeException>();
            f.Invoking(sut => Console.Write(sut[0])).Should().Throw <IndexOutOfRangeException>();
            f.Invoking(sut => sut.Peek()).Should().Throw <InvalidOperationException>();
            f.Invoking(sut => sut.PeekLast()).Should().Throw <InvalidOperationException>();

            f.Push(5);
            f.Invoking(sut => Console.Write(sut[0])).Should().Throw <IndexOutOfRangeException>();
            f.Invoking(sut => sut.Peek()).Should().Throw <InvalidOperationException>();
            f.Invoking(sut => sut.PeekLast()).Should().Throw <InvalidOperationException>();

            f.Capacity = 1;
            TestWithInternalOffsets(f, b =>
            {
                b.Push(5);
                b[0].Should().Be(5);
                b.Invoking(sut => Console.Write(sut[1])).Should().Throw <IndexOutOfRangeException>();
                b.Peek().Should().Be(5);
                b.PeekLast().Should().Be(5);
                b.Push(6);
                b[0].Should().Be(6, "Only one item in it.");
                b.Invoking(sut => Console.Write(sut[1])).Should().Throw <IndexOutOfRangeException>();
                b.Peek().Should().Be(6);
                b.PeekLast().Should().Be(6);
            });

            f.Clear();
            f.Invoking(sut => Console.Write(sut[0])).Should().Throw <IndexOutOfRangeException>();
            f.Invoking(sut => Console.Write(sut[1])).Should().Throw <IndexOutOfRangeException>();
            f.Invoking(sut => sut.Peek()).Should().Throw <InvalidOperationException>();
            f.Invoking(sut => sut.PeekLast()).Should().Throw <InvalidOperationException>();

            f.Capacity = 2;
            TestWithInternalOffsets(f, b =>
            {
                b.Push(5);
                b[0].Should().Be(5);
                b.Invoking(sut => Console.Write(sut[1])).Should().Throw <IndexOutOfRangeException>();
                b.Peek().Should().Be(5);
                b.PeekLast().Should().Be(5);
                b.Push(6);
                b[0].Should().Be(5);
                b[1].Should().Be(6);
                b.Peek().Should().Be(5);
                b.PeekLast().Should().Be(6);
                b.Pop();
                b[0].Should().Be(6);
                b.Invoking(sut => Console.Write(sut[1])).Should().Throw <IndexOutOfRangeException>();
                b.Peek().Should().Be(6);
                b.PeekLast().Should().Be(6);
                b.Pop();
                b.Invoking(sut => Console.Write(sut[0])).Should().Throw <IndexOutOfRangeException>();
                b.Invoking(sut => Console.Write(sut[1])).Should().Throw <IndexOutOfRangeException>();
                b.Invoking(sut => sut.Peek()).Should().Throw <InvalidOperationException>();
                b.Invoking(sut => sut.PeekLast()).Should().Throw <InvalidOperationException>();

                b.Push(7);
                b.Push(8);
                b.Push(9);
                b[0].Should().Be(8);
                b[1].Should().Be(9);
                b.ToArray().SequenceEqual(new int[] { 8, 9 }).Should().BeTrue();
                b.Peek().Should().Be(8);
                b.PeekLast().Should().Be(9);
                b.Pop().Should().Be(8);
                b.Pop().Should().Be(9);
                AssertEmpty(b);

                b.Push(10);
                b.Push(11);
                b.Push(12);
                b[0].Should().Be(11);
                b[1].Should().Be(12);
                b.Peek().Should().Be(11);
                b.PeekLast().Should().Be(12);
                b.PopLast().Should().Be(12);
                b.Peek().Should().Be(11);
                b.PeekLast().Should().Be(11);
                b.PopLast().Should().Be(11);
                AssertEmpty(b);
            });

            f.Capacity = 3;
            TestWithInternalOffsets(f, b =>
            {
                b.Push(11);
                b.Push(12);
                b.Push(13);
                b[0].Should().Be(11);
                b[1].Should().Be(12);
                b[2].Should().Be(13);
            });


            f.Capacity = 4;
            f.Push(11);
            f.Push(12);
            f.Push(13);
            TestWithInternalOffsets(f, b =>
            {
                b.Push(14);
                b[0].Should().Be(11);
                b[1].Should().Be(12);
                b[2].Should().Be(13);
                b[3].Should().Be(14);
                b.Push(15);
                b[0].Should().Be(12);
                b[1].Should().Be(13);
                b[2].Should().Be(14);
                b[3].Should().Be(15);
                b.Push(16);
                b[0].Should().Be(13);
                b[1].Should().Be(14);
                b[2].Should().Be(15);
                b[3].Should().Be(16);
            });

            f.Capacity = 5;
            AssertContains(f, 11, 12, 13);
            TestWithInternalOffsets(f, b =>
            {
                b.Push(14);
                b.Push(15);
                b.Push(16);
                b.Push(17);
                b[0].Should().Be(13);
                b[1].Should().Be(14);
                b[2].Should().Be(15);
                b[3].Should().Be(16);
                b[4].Should().Be(17);
                f.Invoking(sut => Console.Write(sut[5])).Should().Throw <IndexOutOfRangeException>();
            });
        }
Exemplo n.º 11
0
        public void FIFO_supports_removeAt()
        {
            FIFOBuffer <int> f = new FIFOBuffer <int>(0);

            f.Invoking(sut => sut.RemoveAt(0)).Should().Throw <IndexOutOfRangeException>();
            f.Invoking(sut => sut.RemoveAt(-1)).Should().Throw <IndexOutOfRangeException>();

            f.Capacity = 1;
            f.Push(1);
            TestWithInternalOffsetsAndGrowingCapacity(f, b =>
            {
                AssertContains(b, 1);
                b.RemoveAt(0);
                AssertEmpty(b);
            });

            f.Capacity = 2;
            f.Push(2);
            TestWithInternalOffsetsAndGrowingCapacity(f, b =>
            {
                AssertContains(b, 1, 2);
                b.RemoveAt(0);
                AssertContains(b, 2);
            });

            f.Capacity = 3;
            f.Push(3);
            f.Push(4);
            TestWithInternalOffsetsAndGrowingCapacity(f, b =>
            {
                AssertContains(b, 2, 3, 4);
                b.RemoveAt(2);
                AssertContains(b, 2, 3);
                b.RemoveAt(1);
                AssertContains(b, 2);
                b.RemoveAt(0);
                AssertEmpty(b);
            });

            f.Capacity = 4;
            f.Clear();
            f.Push(2);
            f.Push(3);
            f.Push(4);
            TestWithInternalOffsetsAndGrowingCapacity(f, b =>
            {
                AssertContains(b, 2, 3, 4);
                b.RemoveAt(2);
                AssertContains(b, 2, 3);
                b.RemoveAt(1);
                AssertContains(b, 2);
            });

            f.Push(5);
            TestWithInternalOffsetsAndGrowingCapacity(f, b =>
            {
                AssertContains(b, 2, 3, 4, 5);
                b.RemoveAt(2);
                AssertContains(b, 2, 3, 5);
                b.RemoveAt(1);
                AssertContains(b, 2, 5);
                b.RemoveAt(1);
                AssertContains(b, 2);
            });

            f.Capacity = 5;
            f.Push(6);
            TestWithInternalOffsetsAndGrowingCapacity(f, b =>
            {
                AssertContains(b, 2, 3, 4, 5, 6);
                b.RemoveAt(2);
                AssertContains(b, 2, 3, 5, 6);
                b.RemoveAt(1);
                AssertContains(b, 2, 5, 6);
                b.RemoveAt(2);
                AssertContains(b, 2, 5);
                b.RemoveAt(1);
                AssertContains(b, 2);
            });
        }
Exemplo n.º 12
0
        public void FIFO_change_capacity_preserves_items()
        {
            FIFOBuffer <int> f = new FIFOBuffer <int>(0);

            f.Capacity.Should().Be(0);
            AssertEmpty(f);
            f.Push(5);
            AssertEmpty(f);
            f.Push(12);
            AssertEmpty(f);

            f.Capacity = 1;
            f.Capacity.Should().Be(1);
            AssertEmpty(f);
            f.Push(5);
            AssertContains(f, 5);
            f.Push(6);
            AssertContains(f, 6);

            f.Capacity = 2;
            f.Capacity.Should().Be(2);
            AssertContains(f, 6);
            f.Push(7);
            AssertContains(f, 6, 7);
            f.Push(8);
            AssertContains(f, 7, 8);

            f.Capacity = 4;
            f.Capacity.Should().Be(4);
            AssertContains(f, 7, 8);
            f.Push(9);
            AssertContains(f, 7, 8, 9);
            f.Push(10);
            AssertContains(f, 7, 8, 9, 10);
            f.Push(11);
            AssertContains(f, 8, 9, 10, 11);

            f.Capacity = 7;
            f.Capacity.Should().Be(7);
            AssertContains(f, 8, 9, 10, 11);
            f.Push(12);
            AssertContains(f, 8, 9, 10, 11, 12);
            f.Push(13);
            AssertContains(f, 8, 9, 10, 11, 12, 13);
            f.Push(14);
            AssertContains(f, 8, 9, 10, 11, 12, 13, 14);
            f.Push(15);
            AssertContains(f, 9, 10, 11, 12, 13, 14, 15);

            f.Capacity = 2;
            f.Capacity.Should().Be(2);
            AssertContains(f, 14, 15);

            f.Capacity = 3;
            f.Capacity.Should().Be(3);
            AssertContains(f, 14, 15);
            f.Push(16);
            AssertContains(f, 14, 15, 16);

            f.Capacity = 2;
            f.Capacity.Should().Be(2);
            AssertContains(f, 15, 16);

            f.Capacity = 1;
            f.Capacity.Should().Be(1);
            AssertContains(f, 16);

            f.Capacity = 0;
            f.Capacity.Should().Be(0);
            AssertEmpty(f);

            f.Capacity = 2;
            f.Capacity = 2;
            f.Capacity.Should().Be(2);

            f.ToString().Should().Be(String.Format("Count = {0} (Capacity = {1})", 0, 2));

            //ExceptionTest
            f.Invoking(sut => sut.Capacity = -1).Should().Throw <ArgumentException>();
            f.Invoking(sut => new FIFOBuffer <int>(-1)).Should().Throw <ArgumentException>();
            f.Invoking(sut => sut.CopyTo(new int[2], 0, -1)).Should().Throw <IndexOutOfRangeException>();
        }
Exemplo n.º 13
0
        public void FIFO_supports_Peek_and_PeekLast()
        {
            FIFOBuffer <int> f = new FIFOBuffer <int>(0);

            Assert.Throws <IndexOutOfRangeException>(() => Console.Write(f[-1]));
            Assert.Throws <IndexOutOfRangeException>(() => Console.Write(f[0]));
            Assert.Throws <InvalidOperationException>(() => f.Peek());
            Assert.Throws <InvalidOperationException>(() => f.PeekLast());

            f.Push(5);
            Assert.Throws <IndexOutOfRangeException>(() => Console.Write(f[0]));
            Assert.Throws <InvalidOperationException>(() => f.Peek());
            Assert.Throws <InvalidOperationException>(() => f.PeekLast());

            f.Capacity = 1;
            TestWithInternalOffsets(f, b =>
            {
                b.Push(5);
                Assert.That(b[0], Is.EqualTo(5));
                Assert.Throws <IndexOutOfRangeException>(() => Console.Write(b[1]));
                Assert.That(b.Peek(), Is.EqualTo(5));
                Assert.That(b.PeekLast(), Is.EqualTo(5));
                b.Push(6);
                Assert.That(b[0], Is.EqualTo(6), "Only one item in it.");
                Assert.Throws <IndexOutOfRangeException>(() => Console.Write(b[1]));
                Assert.That(b.Peek(), Is.EqualTo(6));
                Assert.That(b.PeekLast(), Is.EqualTo(6));
            });

            f.Clear();
            Assert.Throws <IndexOutOfRangeException>(() => Console.Write(f[0]));
            Assert.Throws <IndexOutOfRangeException>(() => Console.Write(f[1]));
            Assert.Throws <InvalidOperationException>(() => f.Peek());
            Assert.Throws <InvalidOperationException>(() => f.PeekLast());

            f.Capacity = 2;
            TestWithInternalOffsets(f, b =>
            {
                b.Push(5);
                Assert.That(b[0], Is.EqualTo(5));
                Assert.Throws <IndexOutOfRangeException>(() => Console.Write(b[1]));
                Assert.That(b.Peek(), Is.EqualTo(5));
                Assert.That(b.PeekLast(), Is.EqualTo(5));
                b.Push(6);
                Assert.That(b[0], Is.EqualTo(5));
                Assert.That(b[1], Is.EqualTo(6));
                Assert.That(b.Peek(), Is.EqualTo(5));
                Assert.That(b.PeekLast(), Is.EqualTo(6));
                b.Pop();
                Assert.That(b[0], Is.EqualTo(6));
                Assert.Throws <IndexOutOfRangeException>(() => Console.Write(b[1]));
                Assert.That(b.Peek(), Is.EqualTo(6));
                Assert.That(b.PeekLast(), Is.EqualTo(6));
                b.Pop();
                Assert.Throws <IndexOutOfRangeException>(() => Console.Write(b[0]));
                Assert.Throws <IndexOutOfRangeException>(() => Console.Write(b[1]));
                Assert.Throws <InvalidOperationException>(() => b.Peek());
                Assert.Throws <InvalidOperationException>(() => b.PeekLast());

                b.Push(7);
                b.Push(8);
                b.Push(9);
                Assert.That(b[0], Is.EqualTo(8));
                Assert.That(b[1], Is.EqualTo(9));
                CollectionAssert.AreEqual(b.ToArray(), new int[] { 8, 9 });
                Assert.That(b.Peek(), Is.EqualTo(8));
                Assert.That(b.PeekLast(), Is.EqualTo(9));
                Assert.That(b.Pop(), Is.EqualTo(8));
                Assert.That(b.Pop(), Is.EqualTo(9));
                AssertEmpty(b);

                b.Push(10);
                b.Push(11);
                b.Push(12);
                Assert.That(b[0], Is.EqualTo(11));
                Assert.That(b[1], Is.EqualTo(12));
                Assert.That(b.Peek(), Is.EqualTo(11));
                Assert.That(b.PeekLast(), Is.EqualTo(12));
                Assert.That(b.PopLast(), Is.EqualTo(12));
                Assert.That(b.Peek(), Is.EqualTo(11));
                Assert.That(b.PeekLast(), Is.EqualTo(11));
                Assert.That(b.PopLast(), Is.EqualTo(11));
                AssertEmpty(b);
            });

            f.Capacity = 3;
            TestWithInternalOffsets(f, b =>
            {
                b.Push(11);
                b.Push(12);
                b.Push(13);
                Assert.That(b[0], Is.EqualTo(11));
                Assert.That(b[1], Is.EqualTo(12));
                Assert.That(b[2], Is.EqualTo(13));
            });


            f.Capacity = 4;
            f.Push(11);
            f.Push(12);
            f.Push(13);
            TestWithInternalOffsets(f, b =>
            {
                b.Push(14);
                Assert.That(b[0], Is.EqualTo(11));
                Assert.That(b[1], Is.EqualTo(12));
                Assert.That(b[2], Is.EqualTo(13));
                Assert.That(b[3], Is.EqualTo(14));
                b.Push(15);
                Assert.That(b[0], Is.EqualTo(12));
                Assert.That(b[1], Is.EqualTo(13));
                Assert.That(b[2], Is.EqualTo(14));
                Assert.That(b[3], Is.EqualTo(15));
                b.Push(16);
                Assert.That(b[0], Is.EqualTo(13));
                Assert.That(b[1], Is.EqualTo(14));
                Assert.That(b[2], Is.EqualTo(15));
                Assert.That(b[3], Is.EqualTo(16));
            });

            f.Capacity = 5;
            AssertContains(f, 11, 12, 13);
            TestWithInternalOffsets(f, b =>
            {
                b.Push(14);
                b.Push(15);
                b.Push(16);
                b.Push(17);
                Assert.That(b[0], Is.EqualTo(13));
                Assert.That(b[1], Is.EqualTo(14));
                Assert.That(b[2], Is.EqualTo(15));
                Assert.That(b[3], Is.EqualTo(16));
                Assert.That(b[4], Is.EqualTo(17));
                Assert.Throws <IndexOutOfRangeException>(() => Console.Write(f[5]));
            });
        }
Exemplo n.º 14
0
        public void FIFO_change_capacity_preserves_items()
        {
            FIFOBuffer <int> f = new FIFOBuffer <int>(0);

            Assert.That(f.Capacity, Is.EqualTo(0));
            AssertEmpty(f);
            f.Push(5);
            AssertEmpty(f);
            f.Push(12);
            AssertEmpty(f);

            f.Capacity = 1;
            Assert.That(f.Capacity, Is.EqualTo(1));
            AssertEmpty(f);
            f.Push(5);
            AssertContains(f, 5);
            f.Push(6);
            AssertContains(f, 6);

            f.Capacity = 2;
            Assert.That(f.Capacity, Is.EqualTo(2));
            AssertContains(f, 6);
            f.Push(7);
            AssertContains(f, 6, 7);
            f.Push(8);
            AssertContains(f, 7, 8);

            f.Capacity = 4;
            Assert.That(f.Capacity, Is.EqualTo(4));
            AssertContains(f, 7, 8);
            f.Push(9);
            AssertContains(f, 7, 8, 9);
            f.Push(10);
            AssertContains(f, 7, 8, 9, 10);
            f.Push(11);
            AssertContains(f, 8, 9, 10, 11);

            f.Capacity = 7;
            Assert.That(f.Capacity, Is.EqualTo(7));
            AssertContains(f, 8, 9, 10, 11);
            f.Push(12);
            AssertContains(f, 8, 9, 10, 11, 12);
            f.Push(13);
            AssertContains(f, 8, 9, 10, 11, 12, 13);
            f.Push(14);
            AssertContains(f, 8, 9, 10, 11, 12, 13, 14);
            f.Push(15);
            AssertContains(f, 9, 10, 11, 12, 13, 14, 15);

            f.Capacity = 2;
            Assert.That(f.Capacity, Is.EqualTo(2));
            AssertContains(f, 14, 15);

            f.Capacity = 3;
            Assert.That(f.Capacity, Is.EqualTo(3));
            AssertContains(f, 14, 15);
            f.Push(16);
            AssertContains(f, 14, 15, 16);

            f.Capacity = 2;
            Assert.That(f.Capacity, Is.EqualTo(2));
            AssertContains(f, 15, 16);

            f.Capacity = 1;
            Assert.That(f.Capacity, Is.EqualTo(1));
            AssertContains(f, 16);

            f.Capacity = 0;
            Assert.That(f.Capacity, Is.EqualTo(0));
            AssertEmpty(f);

            f.Capacity = 2;
            f.Capacity = 2;
            Assert.That(f.Capacity, Is.EqualTo(2));

            Assert.That(f.ToString(), Is.EqualTo(String.Format("Count = {0} (Capacity = {1})", 0, 2)));

            //ExceptionTest
            Assert.Throws <ArgumentException>(() => f.Capacity = -1);
            Assert.Throws <ArgumentException>(() => new FIFOBuffer <int>(-1));
            Assert.Throws <IndexOutOfRangeException>(() => f.CopyTo(new int[2], 0, -1));
        }
Exemplo n.º 15
0
        public void FIFOChangeCapacity()
        {
            FIFOBuffer <int> f = new FIFOBuffer <int>(0);

            Assert.That(f.Capacity, Is.EqualTo(0));
            AssertEmpty(f);
            f.Push(5);
            AssertEmpty(f);
            f.Push(12);
            AssertEmpty(f);

            f.Capacity = 1;
            Assert.That(f.Capacity, Is.EqualTo(1));
            AssertEmpty(f);
            f.Push(5);
            AssertContains(f, 5);
            f.Push(6);
            AssertContains(f, 6);

            f.Capacity = 2;
            Assert.That(f.Capacity, Is.EqualTo(2));
            AssertContains(f, 6);
            f.Push(7);
            AssertContains(f, 6, 7);
            f.Push(8);
            AssertContains(f, 7, 8);

            f.Capacity = 4;
            Assert.That(f.Capacity, Is.EqualTo(4));
            AssertContains(f, 7, 8);
            f.Push(9);
            AssertContains(f, 7, 8, 9);
            f.Push(10);
            AssertContains(f, 7, 8, 9, 10);
            f.Push(11);
            AssertContains(f, 8, 9, 10, 11);

            f.Capacity = 7;
            Assert.That(f.Capacity, Is.EqualTo(7));
            AssertContains(f, 8, 9, 10, 11);
            f.Push(12);
            AssertContains(f, 8, 9, 10, 11, 12);
            f.Push(13);
            AssertContains(f, 8, 9, 10, 11, 12, 13);
            f.Push(14);
            AssertContains(f, 8, 9, 10, 11, 12, 13, 14);
            f.Push(15);
            AssertContains(f, 9, 10, 11, 12, 13, 14, 15);

            f.Capacity = 2;
            Assert.That(f.Capacity, Is.EqualTo(2));
            AssertContains(f, 14, 15);

            f.Capacity = 3;
            Assert.That(f.Capacity, Is.EqualTo(3));
            AssertContains(f, 14, 15);
            f.Push(16);
            AssertContains(f, 14, 15, 16);

            f.Capacity = 2;
            Assert.That(f.Capacity, Is.EqualTo(2));
            AssertContains(f, 15, 16);

            f.Capacity = 1;
            Assert.That(f.Capacity, Is.EqualTo(1));
            AssertContains(f, 16);

            f.Capacity = 0;
            Assert.That(f.Capacity, Is.EqualTo(0));
            AssertEmpty(f);
        }