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)
def test_c_UnionStack(self): stack1 = Stack(3) stack2 = Stack(3) numbers = [1, 2, 3, 4, 5, 6] numbersstack1 = [1, 2, 3] numbersstack2 = [4, 5, 6] for i in numbersstack1: stack1.push(i) for i in numbersstack2: stack2.push(i) unitedStack = Calculator.union(stack1, stack2) numbersContained = 0 for i in numbers: number = unitedStack.pop().value if number in numbers: numbersContained += 1 numbers.remove(number) self.assertEqual(numbersContained, len(numbers)) self.assertEqual(unitedStack.max_size, stack1.max_size + stack2.max_size)
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)
def testUnionTwoStacks(self): for i in range(5): for j in range(5): stack1 = Stack(20) for k in range(10): stack1.push(k+i*100) stack2 = Stack(20) for k in range(10): stack2.push(k+j*1000) resultat = Calculator.union(stack1, stack2) self.assertIsInstance(resultat, Stack) self.assertEqual(resultat.max_size, stack1.max_size + stack2.max_size) self.assertEqual(resultat.size(), stack1.size() + stack2.size()) for k in range(9, -1, -1): self.assertEqual(resultat.pop(), k+j*1000) for k in range(9, -1, -1): self.assertEqual(resultat.pop(), k+i*100)
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))
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
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 test_04_should_return_first_element_of_merged_stack(self): unionList = Calculator.union(self.stack, self.stack) self.assertEqual(unionList.list[0], 15)
def test_04_should_return_new_size_of_merged_queue(self): unionList = Calculator.union(self.stack, self.stack) self.assertEqual(unionList.size(), 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)