Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ttpro1995/329f1d4a271fc477cbfe90d2a60f0cc9 to your computer and use it in GitHub Desktop.
Save ttpro1995/329f1d4a271fc477cbfe90d2a60f0cc9 to your computer and use it in GitHub Desktop.
# Thai Thien
# CORRECT SPECIFICATION:
#
# the Queue class provides a fixed-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 if and only if the queue currently
# holds no elements, and False otherwise.
#
# full() returns True if and only if the queue cannot hold
# any more elements, and False otherwise.
#
# 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.
#
# Example:
# q = Queue(1)
# is_empty = q.empty()
# succeeded = q.enqueue(10)
# is_full = q.full()
# value = q.dequeue()
#
# 1. Should create a Queue q that can only hold 1 element
# 2. Should then check whether q is empty, which should return True
# 3. Should attempt to put 10 into the queue, and return True
# 4. Should check whether q is now full, which should return True
# 5. Should attempt to dequeue and put the result into value, which
# should be 10
#
# Your test function should run assertion checks and throw an
# AssertionError for each of the 5 incorrect Queues. Pressing
# submit will tell you how many you successfully catch so far.
from queue_test import *
def test():
q = Queue(1)
is_empty = q.empty()
succeeded = q.enqueue(10)
is_full = q.full()
value = q.dequeue()
assert is_full
assert is_empty
assert value == 10
assert succeeded
q_2 = Queue(2)
succeeded = q_2.enqueue(10)
is_full = q_2.full()
is_empty = q_2.empty()
assert succeeded
assert not is_empty
assert not is_full
succeeded = q_2.enqueue(20)
is_empty = q_2.empty()
is_full = q_2.full()
assert succeeded
assert not is_empty
assert is_full
succeeded = q_2.enqueue(30)
is_empty = q_2.empty()
is_full = q_2.full()
assert not succeeded
assert not is_empty
assert is_full
x = q_2.dequeue()
is_empty = q_2.empty()
is_full = q_2.full()
assert x == 10
assert not is_empty
assert not is_full
x = q_2.dequeue()
is_empty = q_2.empty()
is_full = q_2.full()
assert x == 20
assert is_empty
assert not is_full
x = q_2.dequeue()
assert x == None
x = q_2.dequeue()
assert x == None
q3 = Queue(5)
q3.enqueue(1)
q3.enqueue(2)
q3.enqueue(3)
q3.enqueue(4)
q3.enqueue(5)
xxx = q3.enqueue(6)
is_full = q3.full()
is_empty = q3.empty()
assert is_full
assert not is_empty
assert not xxx
qqq = Queue(10000)
for i in range(0,10000):
qqq.enqueue(i)
is_full = qqq.full()
is_empty = qqq.empty()
assert is_full
assert not is_empty
for i in range(0,10000):
v = qqq.dequeue()
assert v == i
is_full = qqq.full()
is_empty = qqq.empty()
assert not is_full
assert is_empty
t = qqq.enqueue(65636)
is_empty = qqq.empty()
assert t
assert not is_empty
v = qqq.dequeue()
assert v == 65636
is_empty = qqq.empty()
assert is_empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment