Пример #1
0
    def testUnionTwoLinkedLists(self):

        listes = []

        for n in range(5):
            liste = LinkedList()

            for i in range(10):
                liste.append(i + n*100)

            listes.append(liste)

        for i in range(len(listes)):
            for j in range(i):
                resultat = Calculator.union(listes[i], listes[j])

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

                self.assertEqual(resultat.size(), listes[i].size() + listes[j].size())

                for k in range(len(listes)):
                    if k==i:
                        for elem in listes[k].list:
                            self.assertTrue(contient(resultat, elem))
                            
                    elif k==j:
                        for elem in listes[k].list:
                            self.assertTrue(contient(resultat, elem))
                    else:
                        for elem in listes[k].list:
                            self.assertFalse(contient(resultat, elem))
Пример #2
0
class testLinkedList(unittest.TestCase):
    def setUp(self):
        self.linkedList = LinkedList()
        self.linkedList.append(15)
        self.linkedList.append(1)
        self.linkedList.append(8)

    def test_02_empty_list_when_initialized(self):
        listTest = LinkedList()
        self.assertIsNone(listTest.first)
        self.assertEqual(listTest.n, 0)

    def test_02_should_return_true_when_list_is_empty(self):
        self.assertTrue(LinkedList().isEmpty())

    def test_02_should_return_zero_when_list_is_empty(self):
        self.assertEqual(LinkedList().size(), 0)

    def test_02_should_raise_error_when_checking_an_empty_list(self):
        self.assertRaises(ValueError, LinkedList().check)

    def test_02_should_return_element_when_checking(self):
        testList = LinkedList()
        testList.append(5)
        self.assertEqual(testList.check().value, 5)

    def test_02_should_raise_error_when_peeking_an_empty_list(self):
        self.assertRaises(ValueError, LinkedList().peek)

    def test_02_should_return_element_when_peeking(self):
        testList = LinkedList()
        testList.append(5)
        testList.append(15)
        firstNode = testList.peek()
        self.assertEqual(testList.n, 1)
        self.assertEqual(firstNode, 5)
        self.assertEqual(testList.first.value, 15)

    def test_02_should_return_first_node_if_empty(self):
        testList = LinkedList()
        testList.append(5)
        self.assertEqual(testList.first.value, 5)

    def test_02_should_return_2_as_number_of_nodes(self):
        testList = LinkedList()
        testList.append(5)
        testList.append(15)
        self.assertEqual(testList.n, 2)

    def test_02_should_prepend_new_element_with_an_empty_list(self):
        testList = LinkedList()
        testList.prepend(5)
        self.assertEqual(testList.first.value, 5)

    def test_02_should_prepend_new_element_to_first_position(self):
        testList = LinkedList()
        testList.prepend(5)
        testList.prepend(6)
        self.assertEqual(testList.first.value, 6)
        self.assertEqual(testList.n, 2)
Пример #3
0
    def testInit(self):
        linkedList = LinkedList()

        self.assertTrue(linkedList.isEmpty())
        self.assertEqual(linkedList.size(), 0)

        self.assertRaises(ValueError, linkedList.check)
        self.assertRaises(ValueError, linkedList.peek)
Пример #4
0
 def test_02_should_return_element_when_peeking(self):
     testList = LinkedList()
     testList.append(5)
     testList.append(15)
     firstElement = testList.peek()
     self.assertEqual(firstElement, 5)
     self.assertEqual(testList.list[0], 15)
Пример #5
0
 def test_02_should_return_element_when_peeking(self):
     testList = LinkedList()
     testList.append(5)
     testList.append(15)
     firstNode = testList.peek()
     self.assertEqual(testList.n, 1)
     self.assertEqual(firstNode, 5)
     self.assertEqual(testList.first.value, 15)
Пример #6
0
class FilePrinterTest(unittest.TestCase):

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

    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.accept(self.printer)
        with open(self.printer.file_path, 'r') as testFile:
            self.assertEqual(testFile.read(), self.printer.name + "\n(1)\n")

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

        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+'   1   '+filler)

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

        self.queue.accept(self.printer)
        with open(self.printer.file_path, 'r') as testFile:
            self.assertEqual(testFile.read(), self.printer.name + "\n|1|\n")
Пример #7
0
 def test_05_test_Visit_Log_LinkedList(self):
     linkedList = LinkedList()
     for i in range(5):
         linkedList.append(i)
     linkedList.accept(ScreenPrinter("test"))
     self.assertEqual(self.capturedOutput.getvalue(),
                      "test\n\n(0,1,2,3,4)\n\n")
Пример #8
0
 def test_05_test_Visit_Log_LinkedList(self):
     linkedList = LinkedList();
     for i in range(5):
         linkedList.append(i);
     linkedList.accept(FilePrinter("./ThisIsATestFile.txt","name"));
     with open('./ThisIsATestFile.txt', 'r') as myfile:
         result = myfile.read();
     self.assertEqual(result, "name\n(0,1,2,3,4)\n");
Пример #9
0
class ScreenPrinterTest(unittest.TestCase):
    def setUp(self):
        self.screenPrinter = ScreenPrinter("test")
        self.linkedList = LinkedList()
        self.stack = Stack(1)
        self.queue = Queue(1)

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

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

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

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

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

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

        sys.stdout = io.StringIO()
        self.queue.accept(ScreenPrinter(""))
        self.assertEqual(sys.stdout.getvalue(), "\n\n|1|\n\n")
        sys.stdout = sys.__stdout__
Пример #10
0
    def testVisitLogLinkedList(self):

        liste = LinkedList()

        for i in range(5):
            liste.append(i)

        liste.accept(ScreenPrinter(""))

        self.assertEqual(self.capturedOutput.getvalue(), "\n\n(0,1,2,3,4)\n\n")
Пример #11
0
    def testVisitLogLinkedList(self):

        liste = LinkedList()

        for i in range(5):
            liste.append(i)

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

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

        self.assertEqual(result, "\n(0,1,2,3,4)\n")
Пример #12
0
 def test_02_should_return_first_node_if_empty(self):
     testList = LinkedList()
     testList.append(5)
     self.assertEqual(testList.first.value, 5)
Пример #13
0
 def test_02_should_return_2_as_number_of_nodes(self):
     testList = LinkedList()
     testList.append(5)
     testList.append(15)
     self.assertEqual(testList.n, 2)
Пример #14
0
 def test_02_should_raise_error_when_peeking_an_empty_list(self):
     self.assertRaises(ValueError, LinkedList().peek)
Пример #15
0
class LinkedListTest(unittest.TestCase):
    def setUp(self):
        self.linkedList = LinkedList()

    # Test __init__()
    def test_a_Init(self):
        self.assertTrue(self.linkedList.first == None)
        self.assertTrue(self.linkedList.n == 0)

    # Test __init__() -> IsEmpty() -> size() -> check() -> peek() -> append() -> size() -> check() -> peek()
    def test_b_IsEmptyThenAppendThenIsNotEmpty(self):
        self.assertTrue(self.linkedList.isEmpty())
        self.assertEqual(self.linkedList.size(), 0)
        self.assertRaises(ValueError, self.linkedList.check)
        self.assertRaises(ValueError, self.linkedList.peek)

        self.linkedList.append('test')
        self.assertFalse(self.linkedList.isEmpty())
        self.assertEqual(self.linkedList.size(), 1)
        self.assertEqual(self.linkedList.check().value, 'test')
        self.assertEqual(self.linkedList.peek().value, 'test')
        self.assertTrue(self.linkedList.isEmpty())
        self.assertEqual(self.linkedList.size(), 0)

        self.linkedList.append('test1')
        self.linkedList.append('test2')
        self.linkedList.append('test3')
        self.assertFalse(self.linkedList.isEmpty())
        self.assertEqual(self.linkedList.size(), 3)
        self.assertEqual(self.linkedList.check().value, 'test1')
        self.assertEqual(self.linkedList.peek().value, 'test1')
        self.assertEqual(self.linkedList.size(), 2)
        self.assertEqual(self.linkedList.peek().value, 'test2')
        self.assertEqual(self.linkedList.size(), 1)
        self.assertEqual(self.linkedList.peek().value, 'test3')
        self.assertEqual(self.linkedList.size(), 0)
        self.assertTrue(self.linkedList.isEmpty())

    # Test __init__() -> IsEmpty() -> Prepend() -> Prepend() -> check() -> peek()
    def test_c_Prepend(self):
        self.assertTrue(self.linkedList.isEmpty())
        self.linkedList.prepend('test1')

        self.linkedList.prepend('test2')
        self.assertEqual(self.linkedList.check().value, 'test2')

        self.assertEqual(self.linkedList.peek().value, 'test2')
        self.assertEqual(self.linkedList.peek().value, 'test1')
        self.assertTrue(self.linkedList.isEmpty())
Пример #16
0
 def test_02_should_return_zero_when_list_is_empty(self):
     self.assertEqual(LinkedList().size(), 0)
Пример #17
0
 def test_02_should_return_element_when_checking(self):
     testList = LinkedList()
     testList.append(5)
     self.assertEqual(testList.check().value, 5)
Пример #18
0
 def test_02_empty_list_when_initialized(self):
     listTest = LinkedList()
     self.assertIsNone(listTest.first)
     self.assertEqual(listTest.n, 0)
Пример #19
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)
Пример #20
0
 def setUp(self):
     self.linkedList = LinkedList()
Пример #21
0
 def test_02_should_prepend_new_element_to_first_position(self):
     testList = LinkedList()
     testList.prepend(5)
     testList.prepend(6)
     self.assertEqual(testList.list[0], 6)
     self.assertEqual(testList.size(), 2)
Пример #22
0
 def setUp(self):
     self.printer = FilePrinter("testFile.txt", "testName")
     self.linkedList = LinkedList()
     self.stack = Stack(3)
     self.queue = Queue(3)
Пример #23
0
 def test_02_should_prepend_new_element_to_first_position(self):
     testList = LinkedList()
     testList.prepend(5)
     testList.prepend(6)
     self.assertEqual(testList.first.value, 6)
     self.assertEqual(testList.n, 2)
Пример #24
0
 def setUp(self):
     self.screenPrinter = ScreenPrinter("test")
     self.linkedList = LinkedList()
     self.stack = Stack(1)
     self.queue = Queue(1)
Пример #25
0
 def test_04_should_raise_two_empty_linkedList(self):
     self.assertRaises(ValueError, Calculator.union, LinkedList(),
                       LinkedList())
Пример #26
0
 def test_02_should_return_true_when_list_is_empty(self):
     self.assertTrue(LinkedList().isEmpty())
Пример #27
0
 def test_02_should_prepend_new_element_with_an_empty_list(self):
     testList = LinkedList()
     testList.prepend(5)
     self.assertEqual(testList.first.value, 5)
Пример #28
0
    def test_a_UnionLinkedList(self):
        # If list1 is bigger
        list1 = LinkedList()
        list2 = LinkedList()
        numbers = [1, 2, 3, 4, 5]
        numbersList1 = [1, 2, 3]
        numbersList2 = [4, 5]

        for i in numbersList1:
            list1.append(i)

        for i in numbersList2:
            list2.append(i)

        UnitedList = Calculator.union(list1, list2)
        numbersContained = 0
        for i in numbersList1 + numbersList2:
            number = UnitedList.peek().value
            if number in numbersList1 + numbersList2:
                numbersContained += 1
                numbers.remove(number)
        self.assertEqual(numbersContained, len(numbersList1 + numbersList2))

        # If list2 is bigger
        list1 = LinkedList()
        list2 = LinkedList()
        numbers = [1, 2, 3, 4, 5]
        numbersList1 = [1, 2]
        numbersList2 = [3, 4, 5]

        for i in numbersList1:
            list1.append(i)

        for i in numbersList2:
            list2.append(i)

        UnitedList = Calculator.union(list1, list2)
        numbersContained = 0
        for i in numbersList1 + numbersList2:
            number = UnitedList.peek().value
            if number in numbersList1 + numbersList2:
                numbersContained += 1
                numbers.remove(number)
        self.assertEqual(numbersContained, len(numbersList1 + numbersList2))

        # If lists are the same size
        list1 = LinkedList()
        list2 = LinkedList()
        numbers = [1, 2, 3, 4]
        numbersList1 = [1, 2]
        numbersList2 = [3, 4]

        for i in numbersList1:
            list1.append(i)

        for i in numbersList2:
            list2.append(i)

        UnitedList = Calculator.union(list1, list2)
        numbersContained = 0
        for i in numbersList1 + numbersList2:
            number = UnitedList.peek().value
            if number in numbersList1 + numbersList2:
                numbersContained += 1
                numbers.remove(number)
        self.assertEqual(numbersContained, len(numbersList1 + numbersList2))
Пример #29
0
 def setUp(self):
     self.linkedList = LinkedList()
     self.linkedList.append(15)
     self.linkedList.append(1)
     self.linkedList.append(8)
Пример #30
0
 def test_02_empty_list_when_initialized(self):
     listTest = LinkedList()
     self.assertEqual(len(listTest.list), 0)