Beispiel #1
0
        public void Pop_on_empty_Stack_throws_Exception()
        {
            var s = new CountableStack <string>(2);

            s.Put("s1");
            s.Put("s2");
            s.Pop();
            s.Pop();

            try
            {
                s.Pop();
                // se non lancia eccezione,
                // il test deve fallire!
                Assert.Fail("Did not throw Exception");
            }
            catch (InvalidOperationException ex)
            {
                // se viene catturata una InvalidOperationException
                // è tutto a posto
            }
            catch (Exception)
            {
                // se viene lanciata qualche altra eccezione
                // il test deve fallire!
                Assert.Fail("Threw wrong Exception");
            }
        }
Beispiel #2
0
        public void Put_on_full_Stack_throws_Exception()
        {
            var s = new CountableStack <string>(2);

            s.Put("s1");
            s.Put("s2");
            s.Put("s3");
        }
Beispiel #3
0
        public void Pop_returns_the_elements_in_reverse_order()
        {
            var s = new CountableStack <string>(2);

            s.Put("s1");
            s.Put("s2");

            Assert.AreEqual("s2", s.Pop());
            Assert.AreEqual("s1", s.Pop());
        }
Beispiel #4
0
        public void new_Stack_can_Put_up_to_max_capacity()
        {
            var s = new CountableStack <int>(3);

            s.Put(100);
            Assert.IsTrue(s.CanPut());
            s.Put(150);
            Assert.IsTrue(s.CanPut());
            s.Put(200);
            Assert.IsFalse(s.CanPut());
        }
Beispiel #5
0
        public void Full_Stack_Count_with_no_valid_elements_returns_0()
        {
            var s = new CountableStack <string>(10);

            s.Put("aa");
            s.Put("z");

            Assert.AreEqual(0, s.Count(x => x == null));
            Assert.AreEqual(0, s.Count(x => x.StartsWith("c")));
            Assert.AreEqual(0, s.Count(x => x.Length > 3));
        }
Beispiel #6
0
        public void An_unsubscribed_does_not_receive_the_Put_element()
        {
            var s    = new CountableStack <int>(3);
            var sub1 = new StackSubscriber <int>();
            var sub2 = new StackSubscriber <int>();

            s.Subscribe(sub1);
            s.Subscribe(sub2);
            s.Put(40);
            s.Unsubscribe(sub1);
            s.Put(41);
            Assert.AreEqual(40, sub1.LastPut);
            Assert.AreEqual(41, sub2.LastPut);
        }
Beispiel #7
0
        public void The_subscribers_receive_the_Pop_element()
        {
            var s = new CountableStack <int>(3);

            s.Put(40);
            s.Put(41);
            var sub1 = new StackSubscriber <int>();
            var sub2 = new StackSubscriber <int>();

            s.Subscribe(sub1);
            s.Subscribe(sub2);

            s.Pop();
            Assert.AreEqual(41, sub1.LastPop);
            Assert.AreEqual(41, sub2.LastPop);
        }
Beispiel #8
0
        public void Stack_with_1_element_can_Pop()
        {
            var s = new CountableStack <int>(2);

            s.Put(5);
            Assert.IsTrue(s.CanPop());
        }
Beispiel #9
0
        public void Stack_emptied_cannot_Pop()
        {
            var s = new CountableStack <int>(2);

            s.Put(5);
            s.Pop();
            Assert.IsFalse(s.CanPop());
        }
Beispiel #10
0
        public void new_Stack_can_Pop_down_to_empty()
        {
            var s = new CountableStack <int>(3);

            s.Put(100);
            s.Put(150);
            s.Put(200);
            Assert.IsTrue(s.CanPop());

            s.Pop();
            Assert.IsTrue(s.CanPop());

            s.Pop();
            Assert.IsTrue(s.CanPop());

            s.Pop();
            Assert.IsFalse(s.CanPop());
        }
Beispiel #11
0
        public void Full_Stack_Count_returns_correct_number_of_elements()
        {
            var s = new CountableStack <string>(10);

            s.Put(null);
            s.Put("ciao");
            s.Put("mondo");
            s.Put("another string");

            Assert.AreEqual(1, s.Count(x => x == null));

            // Short Circuit conditions:
            // && restituisce false alla prima condizione falsa,
            // NON controlla tutte le condizioni.
            Assert.AreEqual(1, s.Count(x => x != null && x.StartsWith("c")));

            Assert.AreEqual(3, s.Count(x => x != null && x.Length > 3));
        }
Beispiel #12
0
        public void Empty_Stack_returns_0()
        {
            var s = new CountableStack <string>(0);

            Assert.AreEqual(0, s.Count(x => x == null));
            Assert.AreEqual(0, s.Count(x => x.StartsWith("c")));
            Assert.AreEqual(0, s.Count(x => x.Length > 3));

            s = new CountableStack <string>(10);

            Assert.AreEqual(0, s.Count(x => x == null));
            Assert.AreEqual(0, s.Count(x => x.StartsWith("c")));
            Assert.AreEqual(0, s.Count(x => x.Length > 3));

            s = new CountableStack <string>(20);
            s.Put("cc");
            s.Put("heeeeey");
            s.Pop();
            s.Pop();

            Assert.AreEqual(0, s.Count(x => x == null));
            Assert.AreEqual(0, s.Count(x => x.StartsWith("c")));
            Assert.AreEqual(0, s.Count(x => x.Length > 3));
        }