Exemplo n.º 1
0
        public void TestCopyTo()
        {
            var stack = CreateOrderedStack(3);
            var copy  = new RandomAccessStack <int>();

            stack.CopyTo(copy, 0);

            Assert.AreEqual(3, stack.Count);
            Assert.AreEqual(0, copy.Count);
            CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, ((IEnumerable <int>)stack).Select(u => u).ToArray());

            stack.CopyTo(copy, -1);

            Assert.AreEqual(3, stack.Count);
            Assert.AreEqual(3, copy.Count);
            CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, ((IEnumerable <int>)stack).Select(u => u).ToArray());

            // Test IEnumerable

            var enumerable = (IEnumerable)copy;
            var enumerator = enumerable.GetEnumerator();

            CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, GetEnumerable(enumerator).Cast <int>().Select(u => u).ToArray());

            copy.CopyTo(stack, 2);

            Assert.AreEqual(5, stack.Count);
            Assert.AreEqual(3, copy.Count);

            CollectionAssert.AreEqual(new int[] { 1, 2, 3, 2, 3 }, stack.Select(u => u).ToArray());
            CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, copy.Select(u => u).ToArray());
        }
Exemplo n.º 2
0
        RandomAccessStack <int> CreateOrderedStack(int count)
        {
            var check = new int[count];
            var stack = new RandomAccessStack <int>();

            for (int x = 1; x <= count; x++)
            {
                stack.Push(x);
                check[x - 1] = x;
            }

            Assert.AreEqual(count, stack.Count);
            CollectionAssert.AreEqual(check, stack.Select(u => u).ToArray());

            return(stack);
        }
Exemplo n.º 3
0
        public void TestInsertPeek()
        {
            var stack = new RandomAccessStack <int>();

            stack.Insert(0, 3);
            stack.Insert(1, 1);
            stack.Insert(1, 2);

            Assert.ThrowsException <InvalidOperationException>(() => stack.Insert(4, 2));

            Assert.AreEqual(3, stack.Count);
            CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, stack.Select(u => u).ToArray());

            Assert.AreEqual(3, stack.Peek(0));
            Assert.AreEqual(2, stack.Peek(1));
            Assert.AreEqual(1, stack.Peek(-1));
        }