forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_stack.py
151 lines (121 loc) · 4.62 KB
/
test_stack.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from algorithms.stack import (
first_is_consecutive, second_is_consecutive,
is_sorted,
remove_min,
first_stutter, second_stutter,
first_switch_pairs, second_switch_pairs,
is_valid,
simplify_path,
ArrayStack, LinkedListStack,
OrderedStack
)
import unittest
class TestSuite(unittest.TestCase):
def test_is_consecutive(self):
self.assertTrue(first_is_consecutive([3, 4, 5, 6, 7]))
self.assertFalse(first_is_consecutive([3, 4, 6, 7]))
self.assertFalse(first_is_consecutive([3, 2, 1]))
self.assertTrue(second_is_consecutive([3, 4, 5, 6, 7]))
self.assertFalse(second_is_consecutive([3, 4, 6, 7]))
self.assertFalse(second_is_consecutive([3, 2, 1]))
def test_is_sorted(self):
# Test case: bottom [6, 3, 5, 1, 2, 4] top
self.assertFalse(is_sorted([6, 3, 5, 1, 2, 4]))
self.assertTrue(is_sorted([1, 2, 3, 4, 5, 6]))
self.assertFalse(is_sorted([3, 4, 7, 8, 5, 6]))
def test_remove_min(self):
# Test case: bottom [2, 8, 3, -6, 7, 3] top
self.assertEqual([2, 8, 3, 7, 3], remove_min([2, 8, 3, -6, 7, 3]))
# Test case: 2 smallest value [2, 8, 3, 7, 3]
self.assertEqual([4, 8, 7], remove_min([4, 8, 3, 7, 3]))
def test_stutter(self):
# Test case: bottom [3, 7, 1, 14, 9] top
self.assertEqual([3, 3, 7, 7, 1, 1, 14, 14, 9, 9],
first_stutter([3, 7, 1, 14, 9]))
self.assertEqual([3, 3, 7, 7, 1, 1, 14, 14, 9, 9],
second_stutter([3, 7, 1, 14, 9]))
def test_switch_pairs(self):
# Test case: even number of values in stack
# bottom [3, 8, 17, 9, 1, 10] top
self.assertEqual([8, 3, 9, 17, 10, 1],
first_switch_pairs([3, 8, 17, 9, 1, 10]))
self.assertEqual([8, 3, 9, 17, 10, 1],
second_switch_pairs([3, 8, 17, 9, 1, 10]))
# Test case: odd number of values in stack
# bottom [3, 8, 17, 9, 1] top
self.assertEqual([8, 3, 9, 17, 1],
first_switch_pairs([3, 8, 17, 9, 1]))
self.assertEqual([8, 3, 9, 17, 1],
second_switch_pairs([3, 8, 17, 9, 1]))
def test_is_valid_parenthesis(self):
self.assertTrue(is_valid("[]"))
self.assertTrue(is_valid("[]()[]"))
self.assertFalse(is_valid("[[[]]"))
self.assertTrue(is_valid("{([])}"))
self.assertFalse(is_valid("(}"))
def test_simplify_path(self):
p = '/my/name/is/..//keon'
self.assertEqual('/my/name/keon', simplify_path(p))
class TestStack(unittest.TestCase):
def test_ArrayStack(self):
stack = ArrayStack()
stack.push(1)
stack.push(2)
stack.push(3)
# test __iter__()
it = iter(stack)
self.assertEqual(3, next(it))
self.assertEqual(2, next(it))
self.assertEqual(1, next(it))
self.assertRaises(StopIteration, next, it)
# test __len__()
self.assertEqual(3, len(stack))
# test __str__()
self.assertEqual(str(stack), "Top-> 3 2 1")
# test is_empty()
self.assertFalse(stack.is_empty())
# test peek()
self.assertEqual(3, stack.peek())
# test pop()
self.assertEqual(3, stack.pop())
self.assertEqual(2, stack.pop())
self.assertEqual(1, stack.pop())
self.assertTrue(stack.is_empty())
def test_LinkedListStack(self):
stack = LinkedListStack()
stack.push(1)
stack.push(2)
stack.push(3)
# test __iter__()
it = iter(stack)
self.assertEqual(3, next(it))
self.assertEqual(2, next(it))
self.assertEqual(1, next(it))
self.assertRaises(StopIteration, next, it)
# test __len__()
self.assertEqual(3, len(stack))
# test __str__()
self.assertEqual(str(stack), "Top-> 3 2 1")
# test is_empty()
self.assertFalse(stack.is_empty())
# test peek()
self.assertEqual(3, stack.peek())
# test pop()
self.assertEqual(3, stack.pop())
self.assertEqual(2, stack.pop())
self.assertEqual(1, stack.pop())
self.assertTrue(stack.is_empty())
class TestOrderedStack(unittest.TestCase):
def test_OrderedStack(self):
stack = OrderedStack()
self.assertTrue(stack.is_empty())
stack.push(1)
stack.push(4)
stack.push(3)
stack.push(6)
"bottom - > 1 3 4 6 "
self.assertEqual(6, stack.pop())
self.assertEqual(4, stack.peek())
self.assertEqual(3, stack.size())
if __name__ == "__main__":
unittest.main()