Ejemplo n.º 1
0
    def test_stack(self):
        stack = Stack()
        for i in [23, 4, 2, 56]:
            stack.push(i)

        self.assertEqual(stack.size(), 4)
        self.assertEqual(stack.peek(), 56)
        self.assertEqual(stack.pop(), 56)
        stack.pop()
        stack.pop()
        stack.pop()
        self.assertEqual(stack.size(), 0)
        self.assertRaises(RuntimeError, stack.pop)
Ejemplo n.º 2
0
def check_balanced(expr):
    
    opening = '([{'
    closing = ')]}'
    
    opening_d = {opening[i] : closing[i] for i in range(len(opening))}
    closing_d = {closing[i] : opening[i] for i in range(len(opening))}
    
    s = Stack()
    for i, c in enumerate(expr):
        if c in opening_d:
            s.push(c)
        if c in closing_d:
            if not s.is_empty() and opening_d[s.peek()] == c:
                s.pop()
            else:
                print('parenthèse fermante en trop au caractère', i+1)
                return False
                
    return s.size() == 0
Ejemplo n.º 3
0
class StackTest(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.test_stack = Stack()

    def test_to_array(self):
        self.test_stack.push('4')
        self.test_stack.push('5')

        self.assertEqual(self.test_stack.to_array(), ['5', '4'])

    def test_push(self):
        self.test_stack.push(4)
        self.test_stack.push(5)

        self.assertEqual(self.test_stack.to_array(), [5, 4])

    def test_pop(self):
        self.test_stack.push(4)
        self.test_stack.push(5)

        self.assertEqual(self.test_stack.pop(), 5)
        self.assertEqual(self.test_stack.to_array(), [4])

    def test_length(self):
        self.test_stack.push(4)
        self.test_stack.push(5)
        self.test_stack.push(4)
        self.test_stack.pop()

        self.assertEqual(len(self.test_stack), 2)

    def test_peek(self):
        self.test_stack.push(4)
        self.test_stack.push(5)
        self.test_stack.push(3)

        self.assertEqual(self.test_stack.peek(), 3)

    def test_is_empty(self):
        self.assertEqual(self.test_stack.is_empty(), True)
Ejemplo n.º 4
0
class TestStack(unittest.TestCase):
    def setUp(self) -> None:
        self.list = Stack()

    def test_str_stack(self):
        self.list.push(1)
        self.list.push(2)
        self.assertEqual(str(self.list), '[2, 1]')

    def test_iter_stack(self):
        self.list.push(1)
        self.list.push(2)
        self.assertEqual([x for x in self.list], [2, 1])

    def test_len_stack(self):
        self.list.push(1)
        self.list.push(2)
        self.assertEqual(len(self.list), 2)
        _list = Stack([1, 2, 3, 4, 5])
        self.assertEqual(len(_list), 5)

    def test_stack_with_starting_data(self):
        _list = Stack([5, 4, 3])
        self.assertEqual([x for x in _list], [3, 4, 5])
        _list.push(1)
        self.assertEqual([x for x in _list], [1, 3, 4, 5])

    def test_is_empty(self):
        self.assertTrue(self.list.is_empty)
        self.list.push(1)
        self.assertFalse(self.list.is_empty)

    def test_push_stack(self):
        self.list.push(1)
        self.assertEqual([x for x in self.list], [1])
        self.list.push(2)
        self.assertEqual([x for x in self.list], [2, 1])

    def test_pop_stack(self):
        self.list.push(1)
        self.list.push(2)
        self.list.push(3)
        data = self.list.pop()
        self.assertEqual(data, 3)
        self.assertEqual([x for x in self.list], [2, 1])
        self.list.push(10)
        self.list.push(100)
        self.list.pop()
        data = self.list.pop()
        self.assertEqual(data, 10)
        self.assertEqual([x for x in self.list], [2, 1])

    def test_pop_empty(self):
        with self.assertRaises(RuntimeError):
            self.list.push(1)
            self.list.pop()
            self.list.pop()

    def test_peek(self):
        self.list.push(1)
        self.list.push(2)
        data = self.list.peek()
        self.assertEqual(data, 2)

    def test_peek_empty(self):
        with self.assertRaises(RuntimeError):
            self.list.peek()