Ejemplo n.º 1
0
    def test_equals(self):
        sa = Stack()
        sb = Stack()
        sc = Stack()

        self.assertEqual(sa, sb)

        for i in range(50):
            n = random.randint(1, 1000)

            sa.push(n)

        self.assertNotEqual(sa, sb)

        while (not sa.is_empty()):
            k = sa.pop()
            sb.push(k)
            sc.push(k)

        self.assertEqual(sb, sc)

        while (not sb.is_empty()):
            k = sb.pop()
            sa.push(k)

        while (not sc.is_empty()):
            k = sc.pop()
            sb.push(k)

        self.assertEqual(sa, sb)
Ejemplo n.º 2
0
    def test_push(self):
        s = Stack()

        self.assertTrue(s.is_empty())
        s.push('a')
        self.assertFalse(s.is_empty())

        for i in range(50):
            n = random.randint(0, 1000)

            s.push(n)
            self.assertEqual(s.peek(), n)
Ejemplo n.º 3
0
    def test_is_empty(self):
        s = Stack()

        self.assertTrue(s.is_empty())

        for i in range(10):
            s.push(i)

        self.assertFalse(s.is_empty())

        for i in range(10):
            s.pop()

        self.assertTrue(s.is_empty())
Ejemplo n.º 4
0
    def test_str(self):
        s = Stack()

        self.assertEqual(str(s), '')

        for i in range(5):
            s.push(i)

        self.assertEqual(str(s), '4\n3\n2\n1\n0')

        s.pop()
        s.pop()

        self.assertEqual(str(s), '2\n1\n0')

        s.pop()
        s.push(300)
        s.push(10)

        self.assertEqual(str(s), '10\n300\n1\n0')

        while (not s.is_empty()):
            s.pop()

        n = random.randint(1, 365)

        s.push(n)

        self.assertEqual(str(s), str(n))