Example #1
0
 def test_get(self):
     llist = UnoptimizedLinkedList()
     self.assertIsNone(llist.get(1))
     llist, first, second, third = self.create_simple_list()
     self.assertIs(llist.get(1), first)
     self.assertIs(llist.get(2), second)
     self.assertIs(llist.get(3), third)
Example #2
0
 def test_insert_empty_list(self):
     llist = UnoptimizedLinkedList()
     node = Node(1)
     with self.assertRaises(ValueError) as e:
         llist.insert(node, 2)
         self.assertEqual(e.exception.args[0],
                          "Cannot insert while the list is empty")
Example #3
0
    def test_with_comment(self):
        # Start with the empty list
        llist = UnoptimizedLinkedList()

        llist.head = Node(1)
        second = Node(2)
        third = Node(3)
        ''' 
		Three nodes have been created. 
		We have references to these three blocks as first, 
		second and third 

		llist.head        second              third 
			 |                |                  | 
			 |                |                  | 
		+----+------+     +----+------+     +----+------+ 
		| 1  | None |     | 2  | None |     |  3 | None | 
		+----+------+     +----+------+     +----+------+ 
		'''

        llist.head.next = second  # Link first node with second
        ''' 
		Now next of first Node refers to second.  So they 
		both are linked. 

		llist.head        second              third 
			 |                |                  | 
			 |                |                  | 
		+----+------+     +----+------+     +----+------+ 
		| 1  |  o-------->| 2  | null |     |  3 | null | 
		+----+------+     +----+------+     +----+------+  
		'''

        second.next = third  # Link second node with the third node
        ''' 
Example #4
0
 def test_len(self):
     llist = UnoptimizedLinkedList()
     self.assertEqual(len(llist), 0)
     node = Node(1)
     llist.head = node
     self.assertEqual(len(llist), 1)
     llist, first, second, third = self.create_simple_list()
     self.assertEqual(len(llist), 3)
Example #5
0
    def create_simple_list(self):
        llist = UnoptimizedLinkedList()

        first = Node(1)
        second = Node(2)
        third = Node(3)
        llist.head = first
        llist.head.next = second
        second.next = third
        return llist, first, second, third
Example #6
0
 def test_convert_from_tuple(self):
     input = (1, 2, 3)
     llist = UnoptimizedLinkedList(iterable=input)
     self.assertEqual(llist.head.data, 1)
     self.assertEqual(llist.head.next.data, 2)
     self.assertEqual(llist.head.next.next.data, 3)
     self.assertIsNone(llist.head.next.next.next)
Example #7
0
 def test_delete_first_node_one_element_list(self):
     llist = UnoptimizedLinkedList()
     llist.append(1)
     llist.delete(1)
     self.assertIsNone(llist.head)
     text_string = llist.__str__()
     expected_string = "Linked list <, ...>"
     self.assertEqual(text_string, expected_string)
Example #8
0
 def test_get_last_node(self):
     llist = UnoptimizedLinkedList()
     self.assertIsNone(llist.get_last_node())
     first = Node(1)
     llist.head = first
     self.assertIs(llist.get_last_node(), first)
     second = Node(2)
     llist.head = second
     self.assertIs(llist.get_last_node(), second)
Example #9
0
    def test_create_linked_list(self):
        llist = UnoptimizedLinkedList()

        first = Node(1)
        second = Node(2)
        third = Node(3)

        llist.head = first

        llist.head.next = second

        second.next = third

        self.assertIs(llist.head, first)

        # Both point to the same node.
        self.assertIs(llist.head.next, second)
        self.assertIs(first.next, second)

        # Same here.
        self.assertIs(llist.head.next.next, third)
        self.assertIs(second.next, third)
Example #10
0
 def test_insert_prev_node_is_none(self):
     llist = UnoptimizedLinkedList()
     llist.head = Node(2)
     with self.assertRaises(ValueError) as e:
         llist.insert(None, 1)
     self.assertEqual(e.exception.args[0], "Previous node must be Node")
Example #11
0
 def test_is_iterable(self):
     llist = UnoptimizedLinkedList()
     self.assertTrue(llist.is_iterable([]))
     self.assertTrue(llist.is_iterable(()))
     self.assertTrue(llist.is_iterable(''))
     self.assertFalse(llist.is_iterable(1))
Example #12
0
 def test_get_prev_node_param_is_none(self):
     llist = UnoptimizedLinkedList()
     llist.head = Node(1)
     with self.assertRaises(ValueError) as e:
         llist.get_prev_node(None)
     self.assertEqual(e.exception.args[0], "node argument must be Node")
Example #13
0
 def test_get_default(self):
     llist = UnoptimizedLinkedList()
     self.assertEqual(llist.get(1, 5), 5)
Example #14
0
 def test_iterate_empty_list(self):
     llist = UnoptimizedLinkedList()
     with self.assertRaises(StopIteration):
         next(llist)
Example #15
0
 def test_get_prev_node_empty_list(self):
     llist = UnoptimizedLinkedList()
     node = Node(1)
     with self.assertRaises(ValueError) as e:
         llist.get_prev_node(node)
     self.assertEqual(e.exception.args[0], "the list is empty")
Example #16
0
 def create_list(self, data):
     nodes = []
     for x in data:
         nodes.append(Node(x))
     return UnoptimizedLinkedList(nodes)
Example #17
0
 def test_convert_from_empty_list(self):
     llist = UnoptimizedLinkedList(iterable=[])
     self.assertIsNone(llist.head)
Example #18
0
 def test_convert_from_list(self):
     input = [1, 2, 3]
     llist = UnoptimizedLinkedList(iterable=input)
     self.assertEqual(llist.head.data, 1)
     self.assertEqual(llist.head.next.data, 2)
     self.assertEqual(llist.head.next.next.data, 3)
Example #19
0
 def test_append_empty_list(self):
     llist = UnoptimizedLinkedList()
     llist.append(1)
     self.assertEqual(llist.head.data, 1)
Example #20
0
 def test_delete_at_index_from_empty_list(self):
     # Empty list
     llist = UnoptimizedLinkedList()
     with self.assertRaises(IndexError) as e:
         llist.delete_at_index(0)
     self.assertEqual(e.exception.args[0], "\delete from empty list")