-
Notifications
You must be signed in to change notification settings - Fork 50
/
stack_test.py
72 lines (61 loc) · 1.63 KB
/
stack_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!python
from stack import Stack
import unittest
class StackTest(unittest.TestCase):
def test_init(self):
s = Stack()
assert s.peek() is None
assert s.length() == 0
assert s.is_empty() is True
def test_init_with_list(self):
s = Stack(['A', 'B', 'C'])
assert s.peek() == 'C'
assert s.length() == 3
assert s.is_empty() is False
def test_length(self):
s = Stack()
assert s.length() == 0
s.push('A')
assert s.length() == 1
s.push('B')
assert s.length() == 2
s.pop()
assert s.length() == 1
s.pop()
assert s.length() == 0
def test_push(self):
s = Stack()
s.push('A')
assert s.peek() == 'A'
assert s.length() == 1
s.push('B')
assert s.peek() == 'B'
assert s.length() == 2
s.push('C')
assert s.peek() == 'C'
assert s.length() == 3
assert s.is_empty() is False
def test_peek(self):
s = Stack()
assert s.peek() is None
s.push('A')
assert s.peek() == 'A'
s.push('B')
assert s.peek() == 'B'
s.pop()
assert s.peek() == 'A'
s.pop()
assert s.peek() is None
def test_pop(self):
s = Stack(['A', 'B', 'C'])
assert s.pop() == 'C'
assert s.length() == 2
assert s.pop() == 'B'
assert s.length() == 1
assert s.pop() == 'A'
assert s.length() == 0
assert s.is_empty() is True
with self.assertRaises(ValueError):
s.pop()
if __name__ == '__main__':
unittest.main()