Ejemplo n.º 1
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.º 2
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.º 3
0
class testCalculator(unittest.TestCase):
    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)

    # CLass 1: Testing Node
    def test_04_empty_node_points_to_None(self):
        self.assertRaises(ValueError, Calculator.union, Stack(self.MAX_SIZE1), Queue(self.MAX_SIZE1))

    def test_04_should_raise_two_enpty_queues(self):
        self.assertRaises(ValueError, Calculator.union, Stack(self.MAX_SIZE1), Queue(self.MAX_SIZE1))

    def test_04_should_return_new_size_of_merged_queue(self):
        unionList = Calculator.union(self.stack, self.stack)
        self.assertEqual(unionList.n, 6)

    def test_04_should_return_new_size_of_merged_queue(self):
        unionList = Calculator.union(self.stack, self.stack)
        self.assertEqual(unionList.n, 6)

    def test_04_should_return_first_element_of_merged_queue(self):
        unionList = Calculator.union(self.stack, self.stack)
        self.assertEqual(unionList.first.value, 5)

    def test_04_should_raise_error_queue_even_with_first_null(self):
        self.assertRaises(ValueError, Calculator.union, Queue(self.MAX_SIZE1), self.queue)

    def test_04_should_raise_when_one_stack_is_empty(self):
        self.assertRaises(ValueError, Calculator.union, self.stack, Stack(self.MAX_SIZE1))

    def test_04_should_raise_two_different_types(self):
        self.assertRaises(ValueError, Calculator.union, Stack(self.MAX_SIZE1), Queue(self.MAX_SIZE1))

    def test_04_should_raise_two_empty_linkedList(self):
        self.assertRaises(ValueError, Calculator.union, LinkedList(), LinkedList())

    def test_04_should_return_new_size_of_merged_queue(self):
        unionList = Calculator.union(self.stack, self.stack)
        self.assertEqual(unionList.n, 6)

    def test_04_should_return_first_element_of_merged_queue(self):
        unionList = Calculator.union(self.queue, self.queue)
        self.assertEqual(unionList.first.value, 16)

    def test_04_should_return_first_element_of_merged_stack(self):
        unionList = Calculator.union(self.stack, self.stack)
        self.assertEqual(unionList.first.value, 5)


    def test_04_should_raise_when_two_nonEmpty_with_different_type(self):
       self.assertRaises(ValueError, Calculator.union, self.stack, self.queue)
Ejemplo n.º 4
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.º 5
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.º 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
class QueueTest(unittest.TestCase):
    def setUp(self):
        self.queue = Queue(3)

    # Test __init__()
    def test_a_Init(self):
        self.assertEqual(self.queue.max_size, 3)

    # Test __init__() -> Enqueue() -> Dequeue()
    def test_b_EnqueuDequeue(self):
        self.queue.enqueue("test")
        self.assertFalse(self.queue.isEmpty())
        self.assertTrue(self.queue.size() == 1)
        self.assertEqual(self.queue.check().value, "test")
        self.assertEqual(self.queue.peek().value, "test")
        self.assertTrue(self.queue.isEmpty())

        self.queue.enqueue("test1")
        self.queue.enqueue("test2")
        self.queue.enqueue("test3")
        self.assertTrue(self.queue.isFull())
        self.assertEqual(self.queue.size(), self.queue.max_size)
        try:
            self.queue.enqueue("test4")
        except Exception as ex:
            self.assertEqual(str(ex), "Queue overflow")

        self.assertEqual(self.queue.dequeue().value, "test1")
        self.assertEqual(self.queue.dequeue().value, "test2")
        self.assertEqual(self.queue.dequeue().value, "test3")
        try:
            self.queue.dequeue()
        except Exception as ex:
            self.assertEqual(str(ex), "Stack underflow")
Ejemplo n.º 8
0
class FilePrinterTest(unittest.TestCase):

    def setUp(self):
        self.printer = FilePrinter("testFile.txt", "testName")
        self.linkedList = LinkedList()
        self.stack = Stack(3)
        self.queue = Queue(3)

    def tearDown(self):
        try:
            os.remove(self.printer.file_path)
        except IOError:
            pass

    # Test __init__() 
    def test_a_Init(self):
        self.assertEqual(self.printer.file_path, "testFile.txt")
        self.assertEqual(self.printer.name, "testName")

    def test_b_VisitLinkedList(self):
        self.linkedList.append(1)
        self.linkedList.append(2)
        self.linkedList.append(3)

        self.linkedList.accept(self.printer)
        with open(self.printer.file_path, 'r') as testFile:
            self.assertEqual(testFile.read(), self.printer.name + "\n(1,2,3)\n")

    def test_c_VisitStack(self):
        self.stack.push(1)
        self.stack.push(2)
        self.stack.push(3)

        self.stack.accept(self.printer)
        filler="\n-------\n"
        with open(self.printer.file_path, 'r') as testFile:
            self.assertEqual(testFile.read(), self.printer.name +filler+'   3   '+filler+'   2   '+filler+'   1   '+filler)

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

        self.queue.accept(self.printer)
        with open(self.printer.file_path, 'r') as testFile:
            self.assertEqual(testFile.read(), self.printer.name + "\n|1|2|3|\n")
Ejemplo n.º 9
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.º 10
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.º 11
0
class ScreenPrinterTest(unittest.TestCase):
    def setUp(self):
        self.screenPrinter = ScreenPrinter("test")
        self.linkedList = LinkedList()
        self.stack = Stack(3)
        self.queue = Queue(3)

    # Test __init__()
    def test_a_Init(self):
        self.assertEqual(self.screenPrinter.name, "test")

    def test_b_VisitLinkedList(self):
        self.linkedList.append(1)
        self.linkedList.append(2)
        self.linkedList.append(3)

        sys.stdout = io.StringIO()
        self.linkedList.accept(ScreenPrinter(""))
        self.assertEqual(sys.stdout.getvalue(), "\n\n(1,2,3)\n\n")
        sys.stdout = sys.__stdout__

    def test_c_VisitStack(self):
        self.stack.push(1)
        self.stack.push(2)
        self.stack.push(3)

        sys.stdout = io.StringIO()
        self.stack.accept(ScreenPrinter(""))
        filler = "\n-------\n"
        self.assertEqual(
            sys.stdout.getvalue(), '\n' + filler + '   3   ' + filler +
            '   2   ' + filler + '   1   ' + filler + '\n')
        sys.stdout = sys.__stdout__

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

        sys.stdout = io.StringIO()
        self.queue.accept(ScreenPrinter(""))
        self.assertEqual(sys.stdout.getvalue(), "\n\n|1|2|3|\n\n")
        sys.stdout = sys.__stdout__
Ejemplo n.º 12
0
class testqueue(unittest.TestCase):
    def setUp(self):
        self.queue = Queue(100)

    # Séquences de tests pour la tranche first
    def test_03_max_size_set_correctly(self):
        self.assertEqual(self.queue.max_size, 100)

    def test_03_tranche_first_sequence_2(self):
        self.assertRaises(ValueError, self.queue.check)
        self.queue.enqueue(48)
        self.assertEqual(48, self.queue.check().value)

    # Séquences de tests pour la tranche last
    def test_03_tranche_last_sequence_1(self):
        self.assertRaises(ValueError, self.queue.check)
        self.queue.enqueue(37)
        self.queue.dequeue()
        self.assertRaises(ValueError, self.queue.check)

    def test_03_tranche_last_sequence_2(self):
        self.assertRaises(ValueError, self.queue.check)
        self.queue.dequeue()
        self.assertEqual(self.queue.n, 0)
        self.assertRaises(ValueError, self.queue.check)
        self.queue.enqueue(48)

    # Séquences de tests pour la tranche n
    def test_03_tranche_n_sequence_1(self):
        self.assertEqual(0, self.queue.size())
        self.queue.enqueue(37)
        self.assertEqual(1, self.queue.size())
        self.queue.dequeue()
        self.assertEqual(0, self.queue.size())

    def test_03_tranche_n_sequence_2(self):
        self.assertEqual(0, self.queue.size())
        self.queue.dequeue()
        self.assertEqual(self.queue.n, 0)
        self.assertEqual(0, self.queue.size())
        self.queue.enqueue(655)
        self.assertEqual(1, self.queue.size())

    # Séquences de tests pour la tranche full
    def test_03_tranche_full_sequence_1(self):
        self.assertFalse(self.queue.isFull())
        self.queue.enqueue(34)
        self.assertFalse(self.queue.isFull())
        self.queue.dequeue()
        self.assertFalse(self.queue.isFull())

    def test_03_tranche_full_sequence_2(self):
        self.assertFalse(self.queue.isFull())
        self.queue.dequeue()
        self.assertEqual(self.queue.n, 0)
        self.queue.enqueue(37)
        self.assertFalse(self.queue.isFull())

    # Séquences de tests pour la tranche isEmpty

    def test_03_tranche_isEmpty_sequence_1(self):
        self.assertTrue(self.queue.isEmpty())
        self.queue.enqueue(34)
        self.assertFalse(self.queue.isEmpty())
        self.queue.dequeue()
        self.assertTrue(self.queue.isEmpty())
        self.assertRaises(ValueError, self.queue.check)

    def test_03_tranche_isEmpty_sequence_2(self):
        self.assertTrue(self.queue.isEmpty())
        self.queue.dequeue()
        self.assertEqual(self.queue.n, 0)
        self.queue.enqueue(37)
        self.assertFalse(self.queue.isEmpty())

    def test_03_check_should_return_last_element(self):
        self.queue.enqueue(37)
        self.queue.enqueue(45)
        self.assertEqual(self.queue.check().value, 37)

    def test_03_enqueue_should_raise_error_when_queue_full(self):
        for i in range(100):
            self.queue.enqueue(i)
        self.assertRaises(ValueError, self.queue.enqueue, 5)

    def test_03_enqueue_should_change_full_to_true(self):
        for i in range(99):
            self.queue.enqueue(i)
        self.queue.enqueue(4)
        self.assertTrue(self.queue.isFull())

    def test_03_enqueue_should_return_correct_values_for_last_and_first(self):
        self.queue.enqueue(4)
        self.queue.enqueue(5)
        self.assertEqual(self.queue.check().value, 4)

    def test_03_enqueue_should_change_last_to_None(self):
        self.queue.enqueue(4)
        self.queue.enqueue(5)
        self.queue.dequeue()
        self.assertEqual(1, self.queue.n)

    def test_03_enqueue_should_return_correct_value_to_string(self):
        self.queue.enqueue(4)
        self.assertEqual(str(self.queue.check()), '4')