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))
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)
def testInit(self): linkedList = LinkedList() self.assertTrue(linkedList.isEmpty()) self.assertEqual(linkedList.size(), 0) self.assertRaises(ValueError, linkedList.check) self.assertRaises(ValueError, linkedList.peek)
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)
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)
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")
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")
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");
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__
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")
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")
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_raise_error_when_peeking_an_empty_list(self): self.assertRaises(ValueError, LinkedList().peek)
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())
def test_02_should_return_zero_when_list_is_empty(self): self.assertEqual(LinkedList().size(), 0)
def test_02_should_return_element_when_checking(self): testList = LinkedList() testList.append(5) self.assertEqual(testList.check().value, 5)
def test_02_empty_list_when_initialized(self): listTest = LinkedList() self.assertIsNone(listTest.first) self.assertEqual(listTest.n, 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)
def setUp(self): self.linkedList = LinkedList()
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)
def setUp(self): self.printer = FilePrinter("testFile.txt", "testName") self.linkedList = LinkedList() self.stack = Stack(3) self.queue = Queue(3)
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)
def setUp(self): self.screenPrinter = ScreenPrinter("test") self.linkedList = LinkedList() self.stack = Stack(1) self.queue = Queue(1)
def test_04_should_raise_two_empty_linkedList(self): self.assertRaises(ValueError, Calculator.union, LinkedList(), LinkedList())
def test_02_should_return_true_when_list_is_empty(self): self.assertTrue(LinkedList().isEmpty())
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_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))
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.assertEqual(len(listTest.list), 0)