Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Demonstrate a inkedlistadt.py that passes the following test. The tests will rely on using the `__name__` methods (referred to as magic methods). The magic methods

Demonstrate a inkedlistadt.py that passes the following test. The tests will rely on using the `__name__` methods (referred to as magic methods). The magic methods define some of the basic functions to operate on the class.

For instance, the `__eq__` function defines how equality is evaluated, for instance, that function determines what the following would output:
```
linklist1 == linklist2

The test code: 

import unittest
from linkedlistadt import LinkedList


class LinkedListTest(unittest.TestCase):

   def setUp(self):
       self._linked_list: LinkedList = LinkedList()

       for i in range(10):
           self._linked_list.append(i)
   
   @staticmethod
   def check_data_integrity(linked_list: LinkedList) -> bool: # pragma: no cover
       count = 0
       for item in linked_list:
           if item != count:
               return False
           count += 1

       count = len(linked_list) - 1
       node = linked_list.tail
       while node is not None:
           if node.item != count:
               return False
           node = node.previous
           count -= 1

       return True

   
   def test_01_clone_with_valid_instance(self):
       """1. Cloning a LinkedList with a valid instance should deep copy the instance"""
       linked_list = LinkedList.clone(self._linked_list)

       self.assertEqual(len(self._linked_list), len(linked_list))
       self.assertTrue(LinkedListTest.check_data_integrity(linked_list), 'Linked List data integrity issue')
       self.assertEqual(self._linked_list, linked_list)

   
   def test_02_clone_with_invalid_instance_should_raise_exception(self):
       """2. Cloning a LinkedList with an invalid instance type should raise a TypeError"""
       with self.assertRaises(TypeError):
           linked_list = LinkedList.clone('This is a string')

   
   def test_03_init(self):
       """3. A newly instantiated LinkedList should be empty"""
       linked_list = LinkedList()
       self.assertEqual(0, len(linked_list))

   
   def test_04_init_empty(self):
       """4. A newly instantiated LinkedList should have head and tail with None value"""
       linked_list = LinkedList()
       self.assertIsNone(linked_list.head)
       self.assertIsNone(linked_list.tail)

   
   def test_05_append(self):
       """5. Appending to a LinkedList should add items to the end"""
       linked_list = LinkedList()
       for i in range(50):
           linked_list.append(i)

       self.assertEqual(50, len(linked_list))
       self.assertTrue(LinkedListTest.check_data_integrity(linked_list))

   
   def test_06_prepend(self):
       """6. Prepending to a LinkedList should add items to the front"""
       linked_list = LinkedList()
       for i in range(49, -1, -1):
           linked_list.prepend(i)

       self.assertEqual(50, len(linked_list))
       self.assertTrue(LinkedListTest.check_data_integrity(linked_list))

   
   def test_07_insert_before(self):
       """7. Inserting before a value should insert the item before that value"""
       values = [5, 15, 25, 35, 45]
       linked_list = LinkedList()

       linked_list.append(values[4])
       for i in range(3, -1, -1):
           linked_list.insert_before(values[i + 1], values[i])

       i = 0

       for item in linked_list:
           self.assertEqual(values[i], item)
           i += 1

   
   def test_08_insert_before_middle(self):
       """8. Inserting after a value should insert the item before that value"""
       values = [5, 15, 25, 35, 45]
       linked_list = LinkedList()

       for i in values:
           linked_list.append(i)

       linked_list.insert_before(25, 20)

       new_values = [5, 15, 20, 25, 35, 45]
       i = 0
       for item in linked_list:
           self.assertEqual(new_values[i], item)
           i += 1

   
   def test_09_insert_before_not_found(self):
       """9. Inserting before a value that does not exist should raise KeyError exception"""
       values = [5, 15, 25, 35, 45]
       linked_list = LinkedList()

       linked_list.append(values[0])
       for i in values:
           linked_list.append(i)

       with self.assertRaises(KeyError):
           linked_list.insert_before(500, 1000)

   
   def test_10_insert_after(self):
       """10. Inserting after a value should insert the item after that value"""
       values = [5, 15, 25, 35, 45]
       linked_list = LinkedList()

       linked_list.append(values[0])
       for i in range(1, 5):
           linked_list.insert_after(values[i - 1], values[i])

       i = 0
       for item in linked_list:
           self.assertEqual(values[i], item)
           i += 1

   
   def test_11_insert_after_middle(self):
       """11. Inserting after a value should after insert the item before that value"""
       values = [5, 15, 25, 35, 45]
       linked_list = LinkedList()

       for i in values:
           linked_list.append(i)

       linked_list.insert_after(15, 20)

       new_values = [5, 15, 20, 25, 35, 45]
       i = 0
       for item in linked_list:
           self.assertEqual(new_values[i], item)
           i += 1

   
   def test_12_insert_after_not_found(self):
       """12. Inserting after a value that does not exist should raise KeyError exception"""
       values = [5, 15, 25, 35, 45]
       linked_list = LinkedList()

       linked_list.append(values[0])
       for i in values:
           linked_list.append(i)

       with self.assertRaises(KeyError):
           linked_list.insert_after(500, 1000)

   
   def test_13_extract_not_found(self):
       """13. Extracting an item not present in the linked list should raise a TypeError"""
       with self.assertRaises(KeyError):
           self._linked_list.extract(100)

   
   def test_14_extract_head(self):
       """14. Extracting the head should make the second node the new head"""
       self._linked_list.extract(0)

       i = 1
       for item in self._linked_list:
           self.assertEqual(i, item)
           i += 1

       i = 9
       travel = self._linked_list.tail
       while travel is not None:
           self.assertEqual(i, travel.item)
           travel = travel.previous
           i -= 1

       self.assertEqual(9, len(self._linked_list))

   
   def test_15_extract_tail(self):
       """15. Extracting the tail should make the second to last node the new tail"""
       self._linked_list.extract(9)

       i = 0
       for item in self._linked_list:
           self.assertEqual(i, item)
           i += 1

       i = 8
       travel = self._linked_list.tail
       while travel is not None:
           self.assertEqual(i, travel.item)
           travel = travel.previous
           i -= 1

       self.assertEqual(9, len(self._linked_list))

   
   def test_16_extract_middle(self):
       """16. Extracting an item in the middle should reconfigure the list accordingly"""
       self._linked_list.extract(5)

       i = 0
       for item in self._linked_list:
           if i == 5:
               i += 1
           self.assertEqual(i, item)
           i += 1

       tail = self._linked_list.tail
       i = 9
       while tail is not None:
           if i == 5:
               i -= 1
           self.assertEqual(i, tail.item)
           tail = tail.previous
           i -= 1

       self.assertEqual(9, len(self._linked_list))

   
   def test_17_get_head(self):
       """17. Getting the head should return the head"""
       self.assertEqual(0, self._linked_list.head.item)

   
   def test_18_get_tail(self):
       """18. Getting the tail should return the tail"""
       self.assertEqual(9, self._linked_list.tail.item)

   
   def test_19_get_first_item(self):
       """19. Getting the first item should return the first item"""
       self.assertEqual(0, self._linked_list.first)

   
   def test_20_get_last_item(self):
       """20. Getting the last item should return the last item"""
       self.assertEqual(9, self._linked_list.last)

   
   def test_21_get_first_item_empty_list(self):
       """21. Getting the first item in an empty list should raise an IndexError"""
       with self.assertRaises(IndexError):
           linked_list = LinkedList()
           item = linked_list.first()

   
   def test_22_get_last_item_empty_list(self):
       """22. Getting the last item in an empty list should raise an IndexError"""
       with self.assertRaises(IndexError):
           linked_list = LinkedList()
           item = linked_list.last()

   
   def test_23_clear(self):
       """23. Clearing the list should remove all items"""
       self._linked_list.clear()

       self.assertIsNone(self._linked_list.head)
       self.assertIsNone(self._linked_list.tail)
       self.assertEqual(0, len(self._linked_list))

   
   def test_24_eq_valid(self):
       """24. Equality operator should return true for identical linked lists"""
       linked_list2 = LinkedList()
       for i in range(10):
           linked_list2.append(i)

       self.assertTrue(self._linked_list == linked_list2)

   
   def test_25_eq_invalid(self):
       """25. Equality operator should return false for non-identical linked lists"""
       linked_list2 = LinkedList()
       for i in range(10):
           linked_list2.append(i * i)

       self.assertFalse(self._linked_list == linked_list2)

   
   def test_26_eq_diff_sizes(self):
       """26. Equality operator should return false for lists of different sizes"""
       linked_list2 = LinkedList()
       for i in range(15):
           linked_list2.append(i)

       self.assertFalse(self._linked_list == linked_list2)

   
   def test_27_iter(self):
       """27. Iter should yield the next item in the linked list each iteration"""
       i = 0

       for item in self._linked_list:
           self.assertEqual(i, item)
           i += 1

   
   def test_28_empty_when_empty(self):
       """28. Empty property should return true when the list is empty"""
       empty_list = LinkedList()
       self.assertTrue(empty_list.empty)

   
   def test_29_empty_when_not_empty(self):
       """29. Empty property should return true when the list is empty"""
       self._linked_list.append(5)
       self.assertFalse(self._linked_list.empty)

   
   def test_30_remove_first_with_populated_list(self):
       """30. remove_first should remove the first element in a populated list"""

       self._linked_list.remove_first()

       self.assertEqual(1, self._linked_list.first)

       expected = 1
       for item in self._linked_list:
           self.assertEqual(expected, item)
           expected += 1

   
   def test_31_remove_first_when_list_is_empty(self):
       """31. remove_first should raise an IndexError exception if the list is empty"""

       with self.assertRaises(IndexError):
           linked_list = LinkedList()
           linked_list.remove_first()

   
   def test_32_remove_last_when_list_is_empty(self):
       """32. remove_last should raise an IndexError exception if the list is empty"""

       with self.assertRaises(IndexError):
           linked_list = LinkedList()
           linked_list.remove_last()

   
   def test_33_remove_first_only_thing_on_list(self):
       """33. remove_first should make an empty list if it removes the only item on the list"""

       linked_list = LinkedList()
       linked_list.append(5)
       linked_list.remove_first()

       self.assertTrue(linked_list.empty)
       self.assertEqual(0, len(linked_list))

   
   def test_34_remove_last_with_populated_list(self):
       """34. remove_first should remove the first element in a populated list"""

       self._linked_list.remove_last()

       self.assertEqual(8, self._linked_list.last)

       expected = 0
       for item in self._linked_list:
           self.assertEqual(expected, item)
           expected += 1

   
   def test_35_eq_invalid_instance_type(self):
       """35. Equality operator should return false if the object to compare is not the right instance type"""

       self.assertFalse(self._linked_list == 'String instance')

   
   def test_36_reversed_iter(self):
       """36. Iter should yield the next item in the linked list each iteration"""
       i = 9

       for item in reversed(self._linked_list):
           self.assertEqual(i, item)
           i -= 1

   
   def test_37_getitem_with_index_out_of_bounds_should_raise_exception(self):
       """37. Accessing a LinkedList by index that is out of bounds should raise an IndexError exception"""
       with self.assertRaises(IndexError):
           self._linked_list[100]

   
   def test_38_setitem_with_index_out_of_bounds_should_raise_exception(self):
       """20. Setting a LinkedList by index that is out of bounds should raise an IndexError exception"""
       with self.assertRaises(IndexError):
           self._linked_list[100] = 0

   
   def test_39_getitem_with_valid_index_should_return_item(self):
       """37. Accessing a LinkedList with a valid index should return that item at that index"""
       self._linked_list[5]

       self.assertEqual(4, self._linked_list[5])

   
   def test_40_setitem_with_valid_index_should_set_item(self):
       """40. Setting a LinkedList item with a valid index should set that item to the new value at that index"""
       self._linked_list[5] = 10

       self.assertEqual(10, self._linked_list[5])

   
   def test_41_extract_all_should_raise_exception_if_at_least_one_instance_of_item_not_found(self):
       """41. Extract All on an item should raise a KeyError exception if at least one instance of the item is not found"""
       with self.assertRaises(KeyError):
           self._linked_list.extract_all(100)
   
   
   def test_42_extract_all_should_extract_all_instances_of_an_item(self):
       """42. Extract All should extract all instances of an item from the LinkedList"""
       linked_list = LinkedList(['extract', 'leave', 'leave', 'extract', 'extract', 'extract', 'leave', 'leave', 'leave', 'extract'])

       linked_list.extract_all('extract')

       self.assertEqual(5, len(linked_list))
       self.assertNotIn('extract', linked_list)
   
   
   def test_43_init_with_list_instance_should_instantiate_list_with_those_items(self):
       """43. Initializing a LinkedList with a Python list should instantite list with those items"""
       linked_list = LinkedList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

       self.assertEqual(self._linked_list, linked_list)

   
   def test_44_init_with_invalid_python_list_instance_should_raise_exception(self):
       """44. Initializing a LinkedList with an instance that is not a Python list should raise a TypeError"""
       with self.assertRaises(TypeError):
           linked_list = LinkedList('This is a string')
   
   
   def test_45_insert_at_index_with_index_out_bounds_should_raise_exception(self):
       """45. Insert At Index with an index that is out of bounds should raise an IndexError"""
       with self.assertRaises(IndexError):
           self._linked_list.insert_at_index(100, 'new item')
   
   
   def test_46_insert_at_index_should_insert_new_item_at_that_index(self):
       """46. Insert At Index should insert the new item at that index and shift the item currently
              at that index over to the right of the new item"""
       linked_list = LinkedList([0, 1, 2, 3, 5, 6, 7, 8, 9])

       linked_list.insert_at_index(4, 4)

       self.assertEqual(10, len(linked_list))
       self.assertTrue(self.check_data_integrity(linked_list))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Create the python class name LinkedList class Node def initself item selfitem item selfnext None selfprevious None class LinkedList def initself itemsNone selfhead None selftail None selflength 0 if i... blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Principles Of Information Security

Authors: Michael E. Whitman, Herbert J. Mattord

7th Edition

035750643X, 978-0357506431

More Books

Students explore these related Programming questions