Exemple #1
0
    def test_peek(self):
        q = Queue()

        self.assertRaises(IndexError, q.peek)

        q.enqueue(0)
        self.assertEqual(q.peek(), 0)

        q.enqueue(1)
        self.assertNotEqual(q.peek(), 1)
        self.assertEqual(q.peek(), 0)

        q.dequeue()
        self.assertEqual(q.peek(), 1)
Exemple #2
0
    def test_is_empty(self):
        q = Queue()

        self.assertTrue(q.is_empty())

        for i in range(1000):
            n = random.randint(0, 1000)
            q.enqueue(n)

        counter = 0
        while (not q.is_empty()):
            q.dequeue()
            counter += 1

        self.assertEqual(counter, 1000)
Exemple #3
0
    def test_dequeue(self):
        q = Queue()

        self.assertRaises(IndexError, q.dequeue)

        for i in range(10):
            q.enqueue(i)

        for i in range(9):
            self.assertEqual(q.peek(), i)
            q.dequeue()
            self.assertNotEqual(q.peek(), i)

        self.assertFalse(q.is_empty())
        q.dequeue()
        self.assertTrue(q.is_empty())
Exemple #4
0
def test_nonempty_queue():
    queue = Queue()
    queue.enqueue(1)
    queue.enqueue(2)
    queue.enqueue(3)

    assert len(queue) == 3
    assert queue.dequeue() == 1

    assert queue.front() == 2

    assert len(queue) == 2
    assert queue.dequeue() == 2

    assert len(queue) == 1
    assert queue.dequeue() == 3

    assert len(queue) == 0
Exemple #5
0
    def test_equals(self):
        qa = Queue()
        qb = Queue()

        self.assertEqual(qa, qb)

        for i in range(50):
            r = random.randint(1, 200)

            qa.enqueue(r)
            self.assertNotEqual(qa, qb)
            qb.enqueue(r)
            self.assertEqual(qa, qb)

        for i in range(50):
            qa.dequeue()
            self.assertNotEqual(qa, qb)
            qb.dequeue()
            self.assertEqual(qa, qb)
Exemple #6
0
    def test_str(self):
        q = Queue()

        self.assertEqual(str(q), '')

        s = ''

        for i in range(50):
            r = random.randint(1, 200)

            q.enqueue(r)
            if (s == ''):
                s = str(r)
            else:
                s = str(r) + ' ' + s

        k = len(str(q.peek()))
        s = s[:-(k + 1)]
        q.dequeue()

        self.assertEqual(str(q), s)
Exemple #7
0
class FirstInFirstOut:

    # Default constructor
    def __init__(self, page_sequence, num_of_frames):

        # Initializing a list attribute of page sequences
        self.sequence = page_sequence

        # Initializing the number of page faults
        self.page_fault = 0

        # Initializing the number off frames
        self.frame_size = num_of_frames

        # Creating a queue to use
        self.queue = Queue()

    # Function used to execute the logic of fifo
    def run(self):

        # Iterating through the page sequence
        for i in self.sequence:

            # Next we check if the queue is full or not
            if self.queue.size() < int(self.frame_size):

                # Checking if the page is present in the queue or not
                try:
                    position = self.queue.find(i)
                    # Display the status of the queue
                    self.display(i)
                except ValueError:
                    # if queue is not full add the page to the queue
                    self.queue.enqueue(i)
                    # display the current status of the queue
                    self.display(i)
                    # If it is not present then increment the page fault
                    self.page_fault += 1

            # If the queue is full pop the first page and add new page at the end
            else:
                # Checking if the page is present in the queue or not
                try:
                    position = self.queue.find(i)
                    # Display the status of the queue
                    self.display(i)
                except ValueError:
                    # Pop the page in front of the queue
                    self.queue.dequeue()
                    # Insert the new page to the queue
                    self.queue.enqueue(i)
                    # Display the status of the queue
                    self.display(i)
                    # If it is not present then increment the page fault
                    self.page_fault += 1

    # function used to display the frame and the pages in them
    def display(self, page):

        # Initializing the status string 
        status_string = page + ": <"

        # Iterating through the frames
        for i in range(int(self.frame_size)):

            # Checking if there is a value at the frame
            try:
                # If there is a value then the status string is updated
                value = self.queue.get(i)
                status_string += value + ", "
                # if there isn't a value then an index error is thrown and the status string is updated
            except IndexError:
                status_string += " " + ", "

        # Finally we remove the last comma from status string 
        status_string = status_string[0:len(status_string)-2]
        status_string += ">"

        # Printing the status string
        print(status_string)