Exemple #1
0
class TestQueue(unittest.TestCase):
    def setUp(self):
        self.queue = Queue()

    def enqueue_test_values(self):
        self.queue.enqueue(1)
        self.queue.enqueue(2)
        self.queue.enqueue(3)

    def test_initializer(self):
        self.assertEqual(self.queue.linked_list.node_count, 0)

    def test_enqueue(self):
        self.enqueue_test_values()
        self.assertEqual(self.queue.linked_list.node_count, 3)

    def test_dequeue(self):
        self.enqueue_test_values()
        self.assertEqual(self.queue.dequeue().value, 1)
        self.assertEqual(self.queue.dequeue().value, 2)
        self.assertEqual(self.queue.dequeue().value, 3)

    def test_node_at_index(self):
        self.enqueue_test_values()
        self.assertEqual(self.queue.linked_list.node_at_index(1).value, 2)

    def test_is_empty(self):
        self.assertTrue(self.queue.is_empty())
        self.enqueue_test_values()
        self.assertFalse(self.queue.is_empty())
Exemple #2
0
def find_escape_position(total, count):
    q = Queue()
    for i in xrange(total):
        q.enqueue(i + 1)

    while q.size() > 1:
        for i in xrange(count - 1):
            item = q.dequeue()
            q.enqueue(item)
        q.dequeue()

    return q.dequeue()
def find_escape_position(total, count):
    q = Queue()
    for i in xrange(total):
        q.enqueue(i+1)

    while q.size() > 1:
        for i in xrange(count-1):
            item = q.dequeue()
            q.enqueue(item)
        q.dequeue()

    return q.dequeue()
Exemple #4
0
    def test_queue(self):
        q = Queue()
        self.assertEqual(True, q.is_empty())
        self.assertEqual(0, q.size())

        q.enqueue(1)
        self.assertEqual(False, q.is_empty())
        self.assertEqual(1, q.size())

        q.enqueue(True)
        q.enqueue("Hello, World")
        self.assertEqual(3, q.size())

        self.assertEqual(1, q.dequeue())
        self.assertEqual(True, q.dequeue())
        self.assertEqual(1, q.size())

        q.dequeue()
        self.assertEqual(True, q.is_empty())
Exemple #5
0
    def test_queue(self):
        q = Queue()
        self.assertEqual(True, q.is_empty())
        self.assertEqual(0, q.size())

        q.enqueue(1)
        self.assertEqual(False, q.is_empty())
        self.assertEqual(1, q.size())

        q.enqueue(True)
        q.enqueue("Hello, World")
        self.assertEqual(3, q.size())

        self.assertEqual(1, q.dequeue())
        self.assertEqual(True, q.dequeue())
        self.assertEqual(1, q.size())

        q.dequeue()
        self.assertEqual(True, q.is_empty())
Exemple #6
0
def simulation(numSeconds, pagesPerMinute):

    labprinter = Printer(pagesPerMinute)
    printQueue = Queue()
    waitingtimes = []

    for currentSecond in range(numSeconds):

        if newPrintTask():
            task = Task(currentSecond)
            printQueue.enqueue(task)

        if (not labprinter.busy()) and (not printQueue.is_empty()):
            nexttask = printQueue.dequeue()
            waitingtimes.append(nexttask.waitTime(currentSecond))
            labprinter.startNext(nexttask)

        labprinter.tick()

    averageWait = sum(waitingtimes) / len(waitingtimes)
    print("Finished tasks %3d  Average Wait %6.2f secs %3d tasks remaining." %
          (len(waitingtimes), averageWait, printQueue.size()))