예제 #1
0
        public void TestQueue()
        {
            var queue = new IndexedQueue <int>(new int[] { 1, 2, 3 });

            queue.Count.Should().Be(3);

            queue.Enqueue(4);
            queue.Count.Should().Be(4);
            queue.Peek().Should().Be(1);
            queue.TryPeek(out var a).Should().BeTrue();
            a.Should().Be(1);

            queue[0].Should().Be(1);
            queue[1].Should().Be(2);
            queue[2].Should().Be(3);
            queue.Dequeue().Should().Be(1);
            queue.Dequeue().Should().Be(2);
            queue.Dequeue().Should().Be(3);
            queue[0] = 5;
            queue.TryDequeue(out a).Should().BeTrue();
            a.Should().Be(5);

            queue.Enqueue(4);
            queue.Clear();
            queue.Count.Should().Be(0);
        }
예제 #2
0
        public void TestDefault()
        {
            var queue = new IndexedQueue <int>(10);

            queue.Count.Should().Be(0);

            queue = new IndexedQueue <int>();
            queue.Count.Should().Be(0);
            queue.TrimExcess();
            queue.Count.Should().Be(0);

            queue = new IndexedQueue <int>(Array.Empty <int>());
            queue.Count.Should().Be(0);
            queue.TryPeek(out var a).Should().BeFalse();
            a.Should().Be(0);
            queue.TryDequeue(out a).Should().BeFalse();
            a.Should().Be(0);

            Assert.ThrowsException <InvalidOperationException>(() => queue.Peek());
            Assert.ThrowsException <InvalidOperationException>(() => queue.Dequeue());
            Assert.ThrowsException <IndexOutOfRangeException>(() => _         = queue[-1]);
            Assert.ThrowsException <IndexOutOfRangeException>(() => queue[-1] = 1);
            Assert.ThrowsException <IndexOutOfRangeException>(() => _         = queue[1]);
            Assert.ThrowsException <IndexOutOfRangeException>(() => queue[1]  = 1);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => new IndexedQueue <int>(-1));
        }