Ejemplo n.º 1
0
    def testUnionTwoQueues(self):

        for i in range(5):
            for j in range(5):

                queue1 = Queue(20)

                for k in range(10):
                    queue1.enqueue(k+i*100)

                queue2 = Queue(20)

                for k in range(10):
                    queue2.enqueue(k+j*1000)

                resultat = Calculator.union(queue1, queue2)

                self.assertIsInstance(resultat, Queue)

                self.assertEqual(resultat.max_size, queue1.max_size + queue2.max_size)
                self.assertEqual(resultat.size(), queue1.size() + queue2.size())

                for k in range(10):
                    self.assertEqual(resultat.dequeue(), k+i*100)

                for k in range(10):
                    self.assertEqual(resultat.dequeue(), k+j*1000)
Ejemplo n.º 2
0
    def test_b_UnionQueue(self):
        queue1 = Queue(3)
        queue2 = Queue(3)

        numbers = [1, 2, 3, 4, 5, 6]
        numbersqueue1 = [1, 2, 3]
        numbersqueue2 = [4, 5, 6]

        for i in numbersqueue1:
            queue1.enqueue(i)

        for i in numbersqueue2:
            queue2.enqueue(i)

        unitedStack = Calculator.union(queue1, queue2)

        numbersContained = 0
        for i in numbers:
            number = unitedStack.dequeue().value
            if number in numbers:
                numbersContained += 1
                numbers.remove(number)

        self.assertEqual(numbersContained, len(numbers))
        self.assertEqual(unitedStack.max_size,
                         queue1.max_size + queue2.max_size)
Ejemplo n.º 3
0
 def test_05_test_Visit_LogQueue(self):
     queue = Queue(6);
     for i in range(5):
         queue.enqueue(i);
     queue.accept(FilePrinter("./ThisIsATestFile.txt",""));
     with open('./ThisIsATestFile.txt', 'r') as myfile:
         result=myfile.read();
     self.assertEqual(result, "\n|0|1|2|3|4|\n");
Ejemplo n.º 4
0
    def testVisitLogQueue(self):

        queue = Queue(10)

        for i in range(5):
            queue.enqueue(i)

        queue.accept(ScreenPrinter(""))

        self.assertEqual(self.capturedOutput.getvalue(), "\n\n|0|1|2|3|4|\n\n")
Ejemplo n.º 5
0
 def setUp(self):
     self.MAX_SIZE1 = 10
     self.MAX_SIZE2 = 5
     self.stack = Stack(self.MAX_SIZE1)
     self.stack.push(15)
     self.stack.push(1)
     self.stack.push(5)
     self.queue = Queue(self.MAX_SIZE2)
     self.queue.enqueue(16)
     self.queue.enqueue(13)
     self.queue.enqueue(46)
Ejemplo n.º 6
0
    def testVisitLogQueue(self):

        queue = Queue(10)

        for i in range(5):
            queue.enqueue(i)

        queue.accept(FilePrinter("./test.txt", ""))

        with open('./test.txt', 'r') as myfile:
            result = myfile.read()

        self.assertEqual(result, "\n|0|1|2|3|4|\n")
Ejemplo n.º 7
0
    def testUnionStackAndQueue(self):
        stacks = []

        for n in range(5):
            stack = Stack(20)

            for i in range(10):
                stack.push(i+n*100)

            stacks.append(stack)

        queues = []

        for n in range(5):
            queue = Queue(20)

            for i in range(10):
                queue.enqueue(i+n*1000)

            queues.append(queue)

        for stack in stacks:
            for queue in queues:
                resultat = Calculator.union(stack, queue)

                self.assertIsInstance(resultat, LinkedList)
                self.assertNotIsInstance(resultat, Stack)
                self.assertNotIsInstance(resultat, Queue)

                self.assertEqual(resultat.size(), stack.size() + queue.size())


                noeud = stack.first

                while noeud.next:
                    self.assertTrue(contient(resultat, noeud.value))
                    noeud = noeud.next


                noeud = queue.first

                while noeud.next:
                    self.assertTrue(contient(resultat, noeud.value))
                    noeud = noeud.next
Ejemplo n.º 8
0
    def testEnqueueDequeue(self):

        for size in range(5, 10):
            queue = Queue(size)

            for i in range(1, size):
                queue.enqueue(i)

                self.assertEqual(queue.check(), 1)
                self.assertFalse(queue.isEmpty())
                self.assertFalse(queue.isFull())
                self.assertEqual(queue.size(), i)

            queue.enqueue("last")

            self.assertEqual(queue.check(), 1)
            self.assertFalse(queue.isEmpty())
            self.assertTrue(queue.isFull())
            self.assertEqual(queue.size(), size)

            for i in range(10):
                self.assertRaises(ValueError, queue.enqueue, "fail")

            self.assertEqual(queue.check(), 1)
            self.assertFalse(queue.isEmpty())
            self.assertTrue(queue.isFull())
            self.assertEqual(queue.size(), size)

            queueSize = queue.size()

            for i in range(1, size):
                self.assertEqual(queue.dequeue(), i)
                self.assertFalse(queue.isEmpty())
                self.assertFalse(queue.isFull())
                queueSize -= 1
                self.assertEqual(queue.size(), queueSize)

            self.assertEqual(queue.dequeue(), "last")

            self.assertTrue(queue.isEmpty())
            self.assertFalse(queue.isFull())
            self.assertEqual(queue.size(), 0)
            self.assertRaises(ValueError, queue.dequeue)
Ejemplo n.º 9
0
 def setUp(self):
     self.screenPrinter = ScreenPrinter("test")
     self.linkedList = LinkedList()
     self.stack = Stack(1)
     self.queue = Queue(1)
Ejemplo n.º 10
0
 def testInvalidUnion(self):
     for obj in [LinkedList(), Stack(5), Queue(5)]:
         for invalid in [1, "fail", -3.4, Node(1)]:
             self.assertRaises(ValueError, Calculator.union, obj, invalid)
Ejemplo n.º 11
0
 def setUp(self):
     self.queue = Queue(3)
Ejemplo n.º 12
0
 def setUp(self):
     self.printer = FilePrinter("testFile.txt", "testName")
     self.linkedList = LinkedList()
     self.stack = Stack(3)
     self.queue = Queue(3)
Ejemplo n.º 13
0
 def test_04_should_raise_two_different_types(self):
     self.assertRaises(ValueError, Calculator.union, Stack(
         self.MAX_SIZE1), Queue(self.MAX_SIZE1))
Ejemplo n.º 14
0
 def test_04_should_raise_error_queue_even_with_first_null(self):
     self.assertRaises(ValueError, Calculator.union, Queue(self.MAX_SIZE1), self.queue)
Ejemplo n.º 15
0
 def test_04_empty_node_points_to_None(self):
     self.assertRaises(ValueError, Calculator.union, Stack(
         self.MAX_SIZE1), Queue(self.MAX_SIZE1))
Ejemplo n.º 16
0
 def testInit(self):
     for size in range(5, 10):
         queue = Queue(size)
         self.assertEqual(queue.max_size, size)
         self.assertFalse(queue.isFull())
         self.assertRaises(ValueError, queue.dequeue)