예제 #1
0
class TestStack(unittest.TestCase):
    def setUp(self):
        self.stack = Stack()

    def tearDown(self):
        del self.stack

    def test_push_pop_peek(self):
        values = [10, 20, 30, 40, 50]
        for value in values:
            self.stack.push(value)

        self.assertEqual(len(values), self.stack.size())
        values.reverse()
        for value in values:
            self.assertEqual(value, self.stack.peek())
            self.assertEqual(value, self.stack.pop())

    def test_pop(self):
        self.stack.push(10)
        self.assertEqual(1, self.stack.size())
        self.assertEqual(10, self.stack.pop())
        self.assertEqual(0, self.stack.size())

    def test_peek(self):
        self.stack.push(10)
        self.assertEqual(1, self.stack.size())
        self.assertEqual(10, self.stack.peek())
        self.assertEqual(1, self.stack.size())

    def test_pop_empty(self):
        self.assertIsNone(self.stack.pop())

    def test_peek_empty(self):
        self.assertIsNone(self.stack.peek())
def test():
    s = Stack()
    assert s.is_empty() is True

    s.push(4)
    assert s.items == [4]

    s.push("dog")
    assert s.items == [4, "dog"]

    assert s.peek() == "dog"

    s.push(True)
    assert s.items == [4, "dog", True]

    assert s.size() == 3

    assert s.is_empty() is False

    s.push(8.4)
    assert s.items == [4, "dog", True, 8.4]

    assert s.pop() == 8.4

    assert s.pop() is True

    assert s.size() == 2
예제 #3
0
def test_peek_stack_with_elements():
    stack = Stack([1, 2, "Dog"])

    expected = "Dog"
    output = stack.peek()
    assert output == expected
    assert stack.stack[-1] == expected
예제 #4
0
def sort_stack(stack):
    buffer_stack = Stack()
    while(not stack.is_empty()):
        value = stack.pop()
        if(buffer_stack.is_empty):
            buffer_stack.push(value)
        else:
            if(not buffer_stack.is_empty()):
                while(buffer_stack.peek() > value):
                        temp = buffer_stack.pop()
                        stack.push(temp)
            else:
                buffer_stack.push(value)
    return buffer_stack
예제 #5
0
def test_peek():
    """Test peek."""
    s = Stack()
    s.push(3)
    assert s.peek() == 3
예제 #6
0
class TestStack(unittest.TestCase):
    def setUp(self) -> None:
        self.stack = Stack()

    def test_pop(self) -> None:
        self.stack.stack = [1]
        self.assertEqual(self.stack.pop(), 1)
        self.assertEqual(self.stack.stack, [])
        self.stack.stack = []
        self.assertIsNone(self.stack.pop())
        self.assertEqual(self.stack.stack, [])
        self.stack.stack = [1, 2, 3]
        self.assertEqual(self.stack.pop(), 3)
        self.assertEqual(self.stack.stack, [1, 2])

    def test_push(self) -> None:
        self.stack.push(1)
        self.assertEqual(self.stack.stack, [1])
        self.stack.push(2.0)
        self.assertEqual(self.stack.stack, [1, 2.0])
        self.stack.push("3")
        self.assertEqual(self.stack.stack, [1, 2.0, "3"])

    def test_peek(self) -> None:
        self.assertIsNone(self.stack.peek())
        self.stack.stack = [1]
        self.assertEqual(self.stack.peek(), 1)
        self.stack.stack = [1, 2, 3]
        self.assertEqual(self.stack.peek(), 3)
        self.assertEqual(self.stack.stack, [1, 2, 3])

    def test_search(self) -> None:
        self.assertIsNone(self.stack.search(1))
        self.stack.stack = [1]
        self.assertEqual(self.stack.search(1), 1)
        self.stack.stack = [1, 2, 3]
        self.assertEqual(self.stack.search(1), 3)
        self.assertEqual(self.stack.search(2), 2)
        self.assertEqual(self.stack.search(3), 1)

    def test_is_empty(self) -> None:
        self.assertTrue(self.stack.is_empty())
        self.stack.stack = [1]
        self.assertFalse(self.stack.is_empty())
        self.stack.stack = [1, 2, 3]
        self.assertFalse(self.stack.is_empty())

    def test_clear(self) -> None:
        self.stack.stack = [1, 2, 3]
        self.stack.clear()
        self.assertEqual(self.stack.stack, [])

    def test___str__(self) -> None:
        self.stack.stack = [1, 2, 3]
        self.assertEqual(self.stack.__str__(), "[1, 2, 3]")

    def test___len__(self) -> None:
        self.stack.stack = [1, 2, 3]
        self.assertEqual(len(self.stack), 3)

    def test_big_test(self) -> None:
        test_case = 100_000
        for i in range(test_case):
            self.stack.push(i)
        for i in reversed(range(test_case)):
            self.assertEqual(self.stack.pop(), i)
        self.assertIsNone(self.stack.pop())

    def test_repr(self) -> None:
        for i in range(6):
            self.stack.push(i)
        self.assertEqual(self.stack.__repr__(), "Stack [0, 1, 2, 3, 4, 5]")
        self.stack.push(6)
        self.assertEqual(self.stack.__repr__(),
                         "Stack [0, 1, 2, 3, 4, 5, ...]")
예제 #7
0
def test_peek_empty_stack():
    stack = Stack()

    expected = None
    output = stack.peek()
    assert output == expected