Пример #1
0
 def test_list_insert_index_not_int(self):
     '''
     When trying to insert when index is not an int value.
     1. Pass insert as a string.
     2. TypeError is expected
     '''
     ll = LinkedList()
     with self.assertRaises(TypeError):
         ll.insert_at("0", "15")
Пример #2
0
 def test_list_insert_negative_index(self):
     '''
     When trying to insert below zero index.
     1. Insert at a negative index.
     2. LookUpException is expected.
     '''
     ll = LinkedList()
     with self.assertRaises(LookupError):
         ll.insert_at(-1, "15")
Пример #3
0
 def test_list_insert_above_last_index(self):
     '''
     When trying to insert above the last index.
     1. Insert at a higher than size index.
     2. LookUpException is expected.
     '''
     ll = LinkedList()
     with self.assertRaises(LookupError):
         ll.append("10")
         ll.append("20")
         ll.insert_at(ll.size + 1, "15")
Пример #4
0
 def test_list_insert_not_head_nor_tail(self):
     '''
     When trying to insert at a position between head and tail.
     1. List size must be 2.
     2. New node should not be head, or tail.
     3. New node should be inserted at index 1 (not 0).
     '''
     ll = LinkedList()
     a_value = "Hai, domo!"
     ll.append("Kizuna AI Desu!")
     ll.append("fokiu!")
     ll.insert_at(1, a_value)
     self.assertEqual(ll.head.next.data, a_value)
Пример #5
0
 def test_list_insert_at_head_list_not_empty(self):
     '''
     When trying to insert an element at the head position, and the list is
     not empty.
     1. The element should be inserted at position 0.
     2. The new head should be updated to be the new node.
     3. The old head should be the next element to new head.
     4. the new head should be previous element to old head.
     '''
     ll = LinkedList()
     a_value = "Primer Valor"
     ll.append(a_value)
     some_value = "Segundo valor"
     ll.insert_at(0, some_value)
     self.assertEqual(ll.head.data, some_value)
     self.assertEqual(ll.head.next.data, a_value)
     self.assertEqual(ll.head.next.previous.data, some_value)
Пример #6
0
 def test_list_insert_at_position_list_is_empty(self):
     '''
     When trying to insert an element at a given index, and the list is
     empty.
     1. The element should be inserted at index 0.
     2. Inserted element should be treated as head.
     3. Inserted element should be treated as tail.
     4. Inserted element should be linked to None, forward and backward.
     '''
     ll = LinkedList()
     some_value = "holis"
     ll.insert_at(0, some_value)
     self.assertEqual(ll.head.data, some_value)
     self.assertEqual(ll.head.previous, None)
     self.assertEqual(ll.head.next, None)
     self.assertEqual(ll.tail.data, some_value)
     self.assertEqual(ll.tail.previous, None)
     self.assertEqual(ll.tail.next, None)
Пример #7
0
 def test_list_insert_at_tail_list_not_empty(self):
     '''
     When trying to insert an element at the tail position and the
     list is not empty.
     1. The list must be size > 1.
     2. The element should be inserted at position 'size'
     That means, the new element should be inserted after the old tail, and
     become the new one.
     3. The new node must be new tail.
     4. the old tail next node should be new node.
     5. the new node previous node must be old tail.
     '''
     ll = LinkedList()
     a_value = "Hai!"
     other_value = "Gutentag"
     ll.append("konnichiwa")
     ll.append(a_value)
     ll.insert_at(ll.size, other_value)
     self.assertTrue(ll.size > 1)
     self.assertEqual(ll.tail.data, other_value)
     self.assertEqual(ll.tail.previous.next.data, other_value)
     self.assertEqual(ll.tail.previous.data, a_value)