-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.py
74 lines (57 loc) · 2.11 KB
/
queue.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
import unittest
#A queue is like a line at the grocery store - first in, first out (FIFO)
#You 'enqueue' things to the back of the queue,
#then 'dequeue' them from the front of the queue.
class Queue(object):
def __init__(self):
print('new')
class TestQueue(unittest.TestCase):
def test_new_queue_is_empty(self):
my_queue = Queue()
#len(my_queue) calls my_queue.__len__()
self.assertEquals(len(my_queue), 0)
def test_can_add_one_thing_to_queue(self):
my_queue = Queue()
my_queue.enqueue('a')
self.assertEquals(len(my_queue), 1)
def test_can_add_many_things_to_queue(self):
my_queue = Queue()
my_queue.enqueue('a')
my_queue.enqueue('b')
my_queue.enqueue('c')
my_queue.enqueue('d')
self.assertEquals(len(my_queue), 4)
def test_can_remove_item_from_queue(self):
my_queue = Queue()
my_queue.enqueue('a')
my_queue.enqueue('b')
my_queue.enqueue('c')
my_queue.enqueue('d')
front = my_queue.dequeue()
self.assertEquals(len(my_queue), 3)
self.assertEquals(front, 'a')
front = my_queue.dequeue()
self.assertEquals(len(my_queue), 2)
self.assertEquals(front, 'b')
front = my_queue.dequeue()
self.assertEquals(len(my_queue), 1)
self.assertEquals(front, 'c')
front = my_queue.dequeue()
self.assertEquals(len(my_queue), 0)
self.assertEquals(front, 'd')
def test_can_add_and_remove_from_queue(self):
my_queue = Queue()
my_queue.enqueue('a')
my_queue.enqueue('b')
my_queue.enqueue('c')
my_queue.enqueue('d')
self.assertEquals(my_queue.dequeue(), 'a')
my_queue.enqueue('e')
my_queue.enqueue('f')
self.assertEquals(my_queue.dequeue(), 'b')
self.assertEquals(my_queue.dequeue(), 'c')
my_queue.enqueue('g')
self.assertEquals(my_queue.dequeue(), 'd')
self.assertEquals(my_queue.dequeue(), 'e')
self.assertEquals(my_queue.dequeue(), 'f')
self.assertEquals(my_queue.dequeue(), 'g')