-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayStack_test.py
More file actions
71 lines (66 loc) · 1.92 KB
/
arrayStack_test.py
File metadata and controls
71 lines (66 loc) · 1.92 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Unit test for arrayStack:
#
# init: class method
# test_1: randomly push, pop and count
# test_2: pop from empty stack
import random
import unittest
from arrayStack import arrayStack
from collections import deque
class TestStack(unittest.TestCase):
def test_init(self):
stack = arrayStack();
attr = ['isEmpty', 'count', 'push', 'pop', 'top']
for a in attr:
self.assertTrue(hasattr(stack, a))
def test_1(self):
""" Call push(), pop() and count() with probability
p1, p2 and p3.
"""
dq = deque()
stack = arrayStack();
p1, p2, p3 = (0.4, 0.4, 0.2)
count = 0
stackSize = 0
calls = 1000
while (count < calls):
r = random.uniform(0,1)
if r < p1:
dq.append(r)
stack.push(r)
count += 1
stackSize += 1
elif r < p1 + p2:
if stackSize == 0:
continue
self.assertEqual(dq.pop(), stack.pop())
count += 1
stackSize -= 1
else:
self.assertEqual(stackSize, stack.count())
def test_2(self):
""" Pop from an empty stack
"""
stack = arrayStack()
with self.assertRaises(IndexError):
stack.pop()
with self.assertRaises(IndexError):
stack.top()
count = 0
calls = 1000
while count < calls:
procedure = [1,0] * 10
random.shuffle(procedure)
procedure = [1] + procedure + [0, 0]
with self.assertRaises(IndexError):
for p in procedure:
if p == 1:
stack.push(p)
else:
stack.pop()
count += 1
if __name__ == '__main__':
unittest.main()