Skip to content

Commit b69bfca

Browse files
authored
Add doctest for circular queue overflow condition
Added a doctest to test the QUEUE IS FULL exception when attempting to enqueue an element into a full circular queue. This improves test coverage for line 67 in data_structures/queues/circular_queue.py. Fixes #9943
1 parent c79034c commit b69bfca

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

data_structures/queues/circular_queue.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# Implementation of Circular Queue (using Python lists)
2-
3-
42
class CircularQueue:
53
"""Circular FIFO queue with a fixed capacity"""
64

@@ -17,7 +15,7 @@ def __len__(self) -> int:
1715
>>> len(cq)
1816
0
1917
>>> cq.enqueue("A") # doctest: +ELLIPSIS
20-
<data_structures.queues.circular_queue.CircularQueue object at ...
18+
<data_structures.queues.circular_queue.CircularQueue object at ...>
2119
>>> cq.array
2220
['A', None, None, None, None]
2321
>>> len(cq)
@@ -51,21 +49,28 @@ def enqueue(self, data):
5149
"""
5250
This function inserts an element at the end of the queue using self.rear value
5351
as an index.
52+
5453
>>> cq = CircularQueue(5)
5554
>>> cq.enqueue("A") # doctest: +ELLIPSIS
56-
<data_structures.queues.circular_queue.CircularQueue object at ...
55+
<data_structures.queues.circular_queue.CircularQueue object at ...>
5756
>>> (cq.size, cq.first())
5857
(1, 'A')
5958
>>> cq.enqueue("B") # doctest: +ELLIPSIS
60-
<data_structures.queues.circular_queue.CircularQueue object at ...
59+
<data_structures.queues.circular_queue.CircularQueue object at ...>
6160
>>> cq.array
6261
['A', 'B', None, None, None]
6362
>>> (cq.size, cq.first())
6463
(2, 'A')
64+
>>> cq2 = CircularQueue(2)
65+
>>> cq2.enqueue("X").enqueue("Y") # doctest: +ELLIPSIS
66+
<data_structures.queues.circular_queue.CircularQueue object at ...>
67+
>>> cq2.enqueue("Z") # Queue is full
68+
Traceback (most recent call last):
69+
...
70+
Exception: QUEUE IS FULL
6571
"""
6672
if self.size >= self.n:
6773
raise Exception("QUEUE IS FULL")
68-
6974
self.array[self.rear] = data
7075
self.rear = (self.rear + 1) % self.n
7176
self.size += 1
@@ -75,6 +80,7 @@ def dequeue(self):
7580
"""
7681
This function removes an element from the queue using on self.front value as an
7782
index and returns it
83+
7884
>>> cq = CircularQueue(5)
7985
>>> cq.dequeue()
8086
Traceback (most recent call last):
@@ -93,7 +99,6 @@ def dequeue(self):
9399
"""
94100
if self.size == 0:
95101
raise Exception("UNDERFLOW")
96-
97102
temp = self.array[self.front]
98103
self.array[self.front] = None
99104
self.front = (self.front + 1) % self.n

0 commit comments

Comments
 (0)