forked from donsheehy/datastructures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teststack.py
68 lines (57 loc) · 1.65 KB
/
teststack.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
import unittest
from ds2.stack import ListStack, BadStack, AnotherStack
class StackTests:
def Stack(self):
raise NotImplementedError
def testinit(self):
s = self.Stack()
def testpushandpop(self):
s = self.Stack()
s.push(3)
s.push(5)
self.assertEqual(s.pop(), 5)
s.push(7)
self.assertEqual(s.pop(), 7)
self.assertEqual(s.pop(), 3)
def testpeek(self):
s = self.Stack()
s.push("first")
s.push("second")
s.push("third")
self.assertEqual(s.peek(), "third")
self.assertEqual(s.peek(), "third")
s.pop()
self.assertEqual(s.peek(), "second")
self.assertEqual(s.peek(), "second")
s.pop()
self.assertEqual(s.peek(), "first")
def testlen(self):
s = self.Stack()
self.assertEqual(len(s), 0)
for i in range(10):
s.push(i)
s.push(i+1)
self.assertEqual(len(s), i+2)
s.pop()
self.assertEqual(len(s), i+1)
def testisempty(self):
s = self.Stack()
self.assertTrue(s.isempty())
s.push(1)
self.assertFalse(s.isempty())
s.pop()
self.assertTrue(s.isempty())
def testpopemptyraiseserror(self):
s = self.Stack()
try:
s.pop()
except (IndexError, RuntimeError):
pass
class TestListStack(unittest.TestCase, StackTests):
Stack = ListStack
class TestAnotherStack(unittest.TestCase, StackTests):
Stack = AnotherStack
class TestBadStack(unittest.TestCase, StackTests):
Stack = BadStack
if __name__ == '__main__':
unittest.main()