-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit1_quiz4_2tests.py
160 lines (149 loc) · 3.83 KB
/
unit1_quiz4_2tests.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# CORRECT SPECIFICATION:
#
# the Queue class provides a fized-size FIFO queue of integers
#
# the constructor takes a single parameter: an integer >0 that
# is the maximum number of elements the queue can hold
#
# empty() returns True iff the queue holds no elements
#
# full() returns True iff the queue cannot hold any more elements
#
# enqueue(i) attempts to put the integer i into the queue; it returns
# True if successful and False if the queue is full
#
# dequeue() removes an integer from the queue and returns it,
# or else returns None if the queue is empty
import array
import random
class Queue:
def __init__(self,size_max):
assert size_max > 0
self.max = size_max
self.head = 0
self.tail = 0
self.size = 0
self.data = array.array('i', range(size_max))
def empty(self):
return self.size == 0
def full(self):
return self.size == self.max
def enqueue(self,x):
if self.size == self.max:
return False
self.data[self.tail] = x
self.size += 1
self.tail += 1
if self.tail == self.max:
self.tail = 0
return True
def dequeue(self):
if self.size == 0:
return None
x = self.data[self.head]
self.size -= 1
self.head += 1
if self.head == self.max:
self.head = 0
return x
def test1():
q = Queue(3)
res = q.empty()
if not res:
print "test1 NOT OK"
return
res = q.enqueue(10)
if not res:
print "test1 NOT OK"
return
res = q.enqueue(11)
if not res:
print "test1 NOT OK"
return
x = q.dequeue()
if x != 10:
print "test1 NOT OK"
return
x = q.dequeue()
if x != 11:
print "test1 NOT OK"
return
res = q.empty()
if not res:
print "test1 NOT OK"
return
print "test1 OK"
def test2():
###Your code here.
q2 = Queue(2)
res = q2.full()
if res:
print "test2 NOT OK: queue full without elements"
return
res = q2.dequeue()
if res:
print "test2 NOT OK: dequeue an empty queue return true"
return
val_1 = random.randint(1, 4000)
val_2 = random.randint(1, 4000)
val_3 = random.randint(1, 4000)
res = q2.enqueue(val_1)
if not res:
print "test2 NOT OK: val_1 not enqueued"
return
res = q2.empty()
if res:
print "test2 NOT OK: queue is not empty, but empty() is true"
return
res = q2.enqueue(val_2)
if not res:
print "test2 NOT OK: val_2 not enqueued"
return
res = q2.full()
if not res:
print "test2 NOT OK: queue is full, but full not true"
return
res = q2.enqueue(val_3)
if res:
print "test2 NOT OK: enqueue over a full queue should be false"
return
res = q2.empty()
if res:
print "test2 NOT OK: queue is full (not empty), but empty() is true"
return
x1 = q2.dequeue()
if x1 != val_1:
print "test2 NOT OK: NOT FIFO queue"
return
x2 = q2.dequeue()
if x2 != val_2:
print "test2 NOT OK: NOT FIFO queue"
return
res = q2.empty()
if not res:
print "test2 NOT OK: queue must be empty in the end"
return
res = q2.full()
if res:
print "test2 NOT OK: queue is not full (empty), but full is true"
return
print "test2 OK"
def test3():
###Your code here.
q3 = Queue(1)
res = q3.full()
if res:
print "test3 NOT OK: empty queue shows full"
return
res = q3.empty()
if not res:
print "test3 NOT OK: queue must be empty in the end"
return
res = q3.dequeue()
if res:
print "test3 NOT OK: dequeue an empty queue return true"
return
print "test3 OK"
test1()
test2()
test3()