예제 #1
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());
        }
예제 #2
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>();
        }
예제 #3
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]));
            });
        }