-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from FabioDiBa/python-queue
add python queue
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Queue: | ||
|
||
def __init__(self): | ||
self.list = [] | ||
|
||
def enqueue(self, element): | ||
self.list.append(element) | ||
|
||
def dequeue(self): | ||
assert len(self.list) > 0; "Queue is empty" | ||
return self.list.pop(0) | ||
|
||
def isEmpty(self): | ||
return len(self.list) == 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import unittest | ||
import queue | ||
|
||
class QueueTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.queue = queue.Queue() | ||
|
||
def test_enqueue(self): | ||
self.queue.enqueue(23) | ||
self.assertFalse(self.queue.isEmpty()) | ||
|
||
def test_empty(self): | ||
self.assertTrue(self.queue.isEmpty()) | ||
self.queue.enqueue(42) | ||
self.assertFalse(self.queue.isEmpty()) | ||
|
||
def test_error_when_queue_empty(self): | ||
with self.assertRaises(AssertionError): | ||
self.queue.dequeue() | ||
|
||
def test_multiple_enqueue_dequeue(self): | ||
elements = ['hi', 1, 'hu'] | ||
|
||
for element in elements: | ||
self.queue.enqueue(element) | ||
|
||
for element in elements: | ||
self.assertEqual(self.queue.dequeue(), element) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |