Example #1
0
	def test_list_have_tail_node(self):
		n1 = Node(12)
		n2 = Node(55)
		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		self.assertIs(linked_list.tail, n2)
		self.assertIsNone(n2.next)
Example #2
0
	def setUp(self):
		n1, n2, n3 = Node(1), Node(2), Node(3)	

		linked_list = List();
		linked_list.add_in_tail(n1)	
		linked_list.add_in_tail(n2)	
		linked_list.add_in_tail(n3)

		self.n1, self.n2, self.n3, self.linked_list = n1, n2, n3, linked_list
Example #3
0
	def test_list_can_find_node_by_value(self):
		n1 = Node(12)
		n2 = Node(55)
		n3 = Node(128)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)

		result = linked_list.find(55)
		self.assertIs(result, n2)	
Example #4
0
	def test_sum_equal_lists(self):
		n1 = Node(12)
		n2 = Node(55)
		n3 = Node(128)

		linked_list1 = List()
		linked_list1.add_in_tail(n1)
		linked_list1.add_in_tail(n2)
		linked_list1.add_in_tail(n3)

		n4 = Node(12)
		n5 = Node(55)
		n6 = Node(128)

		linked_list2 = List()
		linked_list2.add_in_tail(n4)
		linked_list2.add_in_tail(n5)
		linked_list2.add_in_tail(n6)

		result = List.sum_lists(linked_list1, linked_list2)

		self.assertEqual(result, [24, 110, 256])
Example #5
0
	def test_sum_not_equal_lists(self):
		n1 = Node(12)
		n2 = Node(55)
		n3 = Node(128)

		linked_list1 = List()
		linked_list1.add_in_tail(n1)
		linked_list1.add_in_tail(n2)
		linked_list1.add_in_tail(n3)

		n4 = Node(12)
		n5 = Node(55)
		n6 = Node(128)
		n7 = Node(200)

		linked_list2 = List()
		linked_list2.add_in_tail(n4)
		linked_list2.add_in_tail(n5)
		linked_list2.add_in_tail(n6)
		linked_list2.add_in_tail(n7)

		result = List.sum_lists(linked_list1, linked_list2)

		self.assertIsNone(result)
Example #6
0
	def test_list_can_delete_all_nodes_by_value(self): # 1.2
		n1 = Node(55)
		n2 = Node(12)
		n3 = Node(55)
		n4 = Node(128)
		n5 = Node(55)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)
		linked_list.add_in_tail(n4)
		linked_list.add_in_tail(n5)

		linked_list.delete_by_value(55)
		self.assertIsNone(linked_list.find(55))
Example #7
0
    def test_list_can_append_node_in_head(self):
        n1 = Node(1)
        n2 = Node(2)
        n3 = Node(3)
        n4 = Node(4)

        linked_list = List()
        linked_list.add_in_tail(n2)
        linked_list.add_in_tail(n3)
        linked_list.add_in_tail(n4)

        linked_list.append_in_head(n1)

        self.assertIs(linked_list.head, n1)
        self.assertIsNone(n1.prev)
        self.assertIs(n1.next, n2)
        self.assertIs(n2.prev, n1)
Example #8
0
	def test_list_can_count_itself_length(self): # 1.5
		n1 = Node(55)
		n2 = Node(12)
		n3 = Node(55)
		n4 = Node(128)	
		n5 = Node(55)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)
		linked_list.add_in_tail(n4)
		linked_list.add_in_tail(n5)

		list_length = linked_list.length()

		self.assertEqual(list_length, 5)
Example #9
0
	def test_list_can_delete_all_nodes(self): # 1.3
		n1 = Node(12)
		n2 = Node(55)
		n3 = Node(128)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)

		linked_list.flush_all()

		self.assertIsNone(linked_list.head)
		self.assertIsNone(linked_list.tail)

		self.assertIsNone(linked_list.find(12))
		self.assertIsNone(linked_list.find(55))
		self.assertIsNone(linked_list.find(128))
Example #10
0
    def test_list_can_append_node_after_item(self):
        n1 = Node(1)
        n2 = Node(2)
        n3 = Node(3)
        n4 = Node(4)

        linked_list = List()
        linked_list.add_in_tail(n1)
        linked_list.add_in_tail(n2)
        linked_list.add_in_tail(n4)

        linked_list.append_after(node=n3, after=n2)

        self.assertIsNone(n1.prev)
        self.assertIs(n1.next, n2)
        self.assertIs(n2.prev, n1)
        self.assertIs(n2.next, n3)
        self.assertIs(n3.prev, n2)
        self.assertIs(n3.next, n4)
        self.assertIs(n4.prev, n3)
        self.assertIsNone(n4.next)
Example #11
0
	def test_list_can_delete_one_node_by_value(self): # 1.1
		n1 = Node(12)
		n2 = Node(55)
		n3 = Node(128)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)

		linked_list.delete_first_by_value(55)

		self.assertIsNone(linked_list.find(55))
		self.assertTrue(linked_list.find(12))
		self.assertTrue(linked_list.find(128))

		self.assertIs(linked_list.head, n1)
		self.assertIs(linked_list.tail, n3)

		self.assertIsNone(n2.next) 
		self.assertIs(n1.next, n3)
		self.assertIsNone(n3.next)
Example #12
0
	def test_list_can_append_node_after_specified_node(self): # 1.6
		n1 = Node(55)
		n2 = Node(12)
		n3 = Node(55)
		n4 = Node(128)	
		n5 = Node(55)

		new = Node(99)	

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)
		linked_list.add_in_tail(n4)
		linked_list.add_in_tail(n5)

		linked_list.append_after(newNode=new, afterNode=n2)

		self.assertTrue(linked_list.find(99))
		self.assertEqual(linked_list.length(), 6)
		self.assertIs(n2.next, new)
		self.assertIs(new.next, n3)
Example #13
0
    def test_list_can_delete_one_node_by_value(self):
        n1 = Node(1)
        n2 = Node(2)
        n3 = Node(3)
        n4 = Node(4)

        linked_list = List()
        linked_list.add_in_tail(n1)
        linked_list.add_in_tail(n2)
        linked_list.add_in_tail(n3)
        linked_list.add_in_tail(n4)

        linked_list.delete_first_by_value(3)

        self.assertIsNone(n3.prev)
        self.assertIsNone(n3.next)

        self.assertIsNone(n1.prev)
        self.assertIs(n1.next, n2)
        self.assertIs(n2.prev, n1)
        self.assertIs(n2.next, n4)
        self.assertIs(n4.prev, n2)
        self.assertIsNone(n4.next)
Example #14
0
	def test_list_can_find_all_nodes_by_value(self): # 1.4
		n1 = Node(55)
		n2 = Node(12)
		n3 = Node(55)
		n4 = Node(128)	
		n5 = Node(55)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)
		linked_list.add_in_tail(n4)
		linked_list.add_in_tail(n5)

		result = linked_list.find_all(55)		

		self.assertEqual(len(result), 3)

		self.assertIn(n1, result)
		self.assertIn(n3, result)
		self.assertIn(n5, result)

		self.assertNotIn(n2, result)
		self.assertNotIn(n4, result)
Example #15
0
	def test_list_have_head_node(self):
		n1 = Node(12)
		linked_list = List()
		linked_list.add_in_tail(n1)		
		self.assertIs(linked_list.head, n1)
Example #16
0
	def test_new_list_have_not_head_and_tail_nodes(self):
		linked_list = List()
		self.assertIsNone(linked_list.head)
		self.assertIsNone(linked_list.tail)