Skip to content

Commit d650f95

Browse files
committed
fix: test coverage of queue_on_pseudo_stack.py
1 parent dae5785 commit d650f95

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

data_structures/queue/queue_on_pseudo_stack.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ def __init__(self):
77
self.length = 0
88

99
def __str__(self):
10+
"""
11+
>>> q = Queue()
12+
>>> q.put(3)
13+
>>> q.put(5)
14+
>>> q.put(7)
15+
>>> print(q)
16+
<3, 5, 7>
17+
18+
>>> q = Queue()
19+
>>> print(q)
20+
<>
21+
"""
1022
printed = "<" + str(self.stack)[1:-1] + ">"
1123
return printed
1224

@@ -15,6 +27,12 @@ def __str__(self):
1527
item to enqueue"""
1628

1729
def put(self, item):
30+
"""
31+
>>> q = Queue()
32+
>>> q.put(3)
33+
>>> assert q.stack == [3]
34+
>>> assert q.length == 1
35+
"""
1836
self.stack.append(item)
1937
self.length = self.length + 1
2038

@@ -24,6 +42,37 @@ def put(self, item):
2442
item that was dequeued"""
2543

2644
def get(self):
45+
"""
46+
Testing get() and inner call to rotate()
47+
48+
>>> q = Queue()
49+
>>> q.put(3)
50+
>>> q.put(5)
51+
>>> q.put(7)
52+
>>> n = q.get()
53+
>>> assert n == 3
54+
>>> assert q.stack == [5,7]
55+
56+
>>> q2 = Queue()
57+
>>> q2.put(3)
58+
>>> n = q2.get()
59+
>>> n = q2.get()
60+
Traceback (most recent call last):
61+
...
62+
IndexError: The queue is empty
63+
64+
>>> q3 = Queue()
65+
>>> n = q3.get()
66+
Traceback (most recent call last):
67+
...
68+
IndexError: The queue is empty
69+
70+
>>> q4 = Queue()
71+
>>> q4.put(3)
72+
>>> n = q4.get()
73+
>>> assert n == 3
74+
"""
75+
2776
self.rotate(1)
2877
dequeued = self.stack[self.length - 1]
2978
self.stack = self.stack[:-1]
@@ -33,10 +82,22 @@ def get(self):
3382

3483
"""Rotates the queue {@code rotation} times
3584
@param rotation
85+
@requirement: |self.length| > 0
3686
number of times to rotate queue"""
3787

3888
def rotate(self, rotation):
89+
"""
90+
>>> q = Queue()
91+
>>> q.put(3)
92+
>>> q.put(5)
93+
>>> q.put(7)
94+
>>> q.rotate(2)
95+
>>> assert q.stack == [7,3,5]
96+
"""
97+
3998
for i in range(rotation):
99+
if self.length == 0:
100+
raise IndexError("The queue is empty")
40101
temp = self.stack[0]
41102
self.stack = self.stack[1:]
42103
self.put(temp)
@@ -46,6 +107,18 @@ def rotate(self, rotation):
46107
@return item at front of self.stack"""
47108

48109
def front(self):
110+
"""
111+
>>> q = Queue()
112+
>>> q.put(3)
113+
>>> n = q.front()
114+
>>> assert n == 3
115+
116+
>>> q2 = Queue()
117+
>>> n = q2.front()
118+
Traceback (most recent call last):
119+
...
120+
IndexError: The queue is empty
121+
"""
49122
front = self.get()
50123
self.put(front)
51124
self.rotate(self.length - 1)
@@ -54,4 +127,15 @@ def front(self):
54127
"""Returns the length of this.stack"""
55128

56129
def size(self):
130+
"""
131+
>>> q = Queue()
132+
>>> q.size()
133+
0
134+
"""
57135
return self.length
136+
137+
138+
if __name__ == "__main__":
139+
import doctest
140+
141+
doctest.testmod()

0 commit comments

Comments
 (0)