예제 #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())
예제 #2
0
def simulation():
    person_queue = Queue()
    for i in xrange(10):
        person_queue.enqueue(Person(0))

    waiting_list = []

    elevators = []
    for i in xrange(6):
        elevators.append(Elevator())

    def new_comer(time):
        # every seconds comes 1 to 5 person
        for i in xrange(random.randint(1, 6)):
            person_queue.enqueue(Person(time))

    for sec in xrange(3600):
        new_comer(sec)
        for e in elevators:
            if e.is_available() and not person_queue.is_empty():
                e.take(person_queue, sec, waiting_list)

            e.tick()
    print "Average waiting time on 1st floor: %f" % \
        (sum(waiting_list) / float(len(waiting_list)))
예제 #3
0
def simulation():
    person_queue = Queue()
    for i in xrange(10):
        person_queue.enqueue(Person(0))

    waiting_list = []

    elevators = []
    for i in xrange(6):
        elevators.append(Elevator())

    def new_comer(time):
        # every seconds comes 1 to 5 person
        for i in xrange(random.randint(1, 6)):
            person_queue.enqueue(Person(time))

    for sec in xrange(3600):
        new_comer(sec)
        for e in elevators:
            if e.is_available() and not person_queue.is_empty():
                e.take(person_queue, sec, waiting_list)

            e.tick()
    print "Average waiting time on 1st floor: %f" % \
        (sum(waiting_list) / float(len(waiting_list)))
예제 #4
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()
예제 #5
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()
예제 #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()))
예제 #7
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())
예제 #8
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())