Пример #1
0
        public static void DoTest()
        {
            var mms = new MinMaxStack();

            mms.Push(5);

            Assert.That(5, Is.EqualTo(mms.Peek()));
            Assert.That(5, Is.EqualTo(mms.GetMin()));
            Assert.That(5, Is.EqualTo(mms.GetMax()));

            mms.Push(99);

            Assert.That(99, Is.EqualTo(mms.Peek()));
            Assert.That(5, Is.EqualTo(mms.GetMin()));
            Assert.That(99, Is.EqualTo(mms.GetMax()));

            mms.Push(2);

            Assert.That(2, Is.EqualTo(mms.Peek()));
            Assert.That(2, Is.EqualTo(mms.GetMin()));
            Assert.That(99, Is.EqualTo(mms.GetMax()));

            int v = mms.Pop();

            Assert.That(2, Is.EqualTo(v));
            Assert.That(5, Is.EqualTo(mms.GetMin()));
            Assert.That(99, Is.EqualTo(mms.GetMax()));
        }
    public static void Main(string[] args)
    {
        MinMaxStack s = new MinMaxStack(10);

        for (int i = 1; i <= 100; i++)
        {
            s.Push(i);
        }
        for (int i = 1; i <= 100; i++)
        {
            s.Pop();
        }
        s.print();
    }
Пример #3
0
        public void TestStackMin()
        {
            var stack = new MinMaxStack <int>();

            Assert.Throws <Exception>(() => { var _ = stack.Peek; }, "Stack underflow");
            Assert.Throws <Exception>(() => { var _ = stack.Pop(); }, "Stack underflow");

            var fixtures = new[] {
                Tuple.Create(100, 100),
                Tuple.Create(200, 100),
                Tuple.Create(300, 100),
                Tuple.Create(10, 10),
                Tuple.Create(10, 10),
                Tuple.Create(30, 10),
                Tuple.Create(-1, -1)
            };

            for (var idx = 0; idx < fixtures.Length; idx++)
            {
                int val = fixtures[idx].Item1, min = fixtures[idx].Item2;
                stack.Push(val);
                Assert.AreEqual(min, stack.Min);
            }

            for (var idx = fixtures.Length - 1; idx >= 0; idx--)
            {
                var min = fixtures[idx].Item2;
                Assert.AreEqual(min, stack.Min);
                stack.Pop();
            }

            stack.Push(-1);
            Assert.AreEqual(-1, stack.Min);
            stack.Push(-2);
            Assert.AreEqual(-2, stack.Min);
            stack.Pop();
            Assert.AreEqual(-1, stack.Min);
            stack.Push(-3);
            Assert.AreEqual(-3, stack.Min);
            stack.Pop();
            stack.Pop();

            Assert.Throws <Exception>(() => { var _ = stack.Peek; }, "Stack underflow");
            Assert.Throws <Exception>(() => { var _ = stack.Pop(); }, "Stack underflow");
        }