Question
Write test cases for the function below Use the test cases outline given. from __future__ import annotations from typing import Any, Optional class Node: def
Write test cases for the function below
Use the test cases outline given.
from __future__ import annotations
from typing import Any, Optional
class Node:
def __init__(self, value: Any, nxt: LinkedList):
self.value = value
self.next = nxt
LinkedList = Optional[Node]
def empty_list() -> LinkedList:
return None
def add(lst: LinkedList, index: int, value: Any) -> LinkedList:
if index length(lst):
raise IndexError("Invalid index")
if index == 0:
return Node(value, lst)
current = lst
for i in range(index - 1):
current = current.next
current.next = Node(value, current.next)
return lst
def length(lst: LinkedList) -> int:
current = lst
count = 0
while current:
count += 1
current = current.next
return count
def get(lst: LinkedList, index: int) -> Any:
if index = length(lst):
raise IndexError("Invalid index")
current = lst
for i in range(index):
current = current.next
return current.value
def setitem(lst: LinkedList, index: int, value: Any) -> LinkedList:
if index = length(lst):
raise IndexError("Invalid index")
current = lst
for i in range(index):
current = current.next
current.value = value
return lst
def remove(lst: LinkedList, index: int) -> tuple[Any, LinkedList]:
if index = length(lst):
raise IndexError("Invalid index")
if index == 0:
return (lst.value, lst.next)
current = lst
for i in range(index - 1):
current = current.next
removed_value = current.next.value
current.next = current.next.next
return (removed_value, lst)
#linked_list_test.py
import unittest
from linked_list import Node, add, empty_list, get, length, remove, setitem
class Tests(unittest.TestCase):
def test_length_empty_list(self) -> None:
self.assertEqual(
length(empty_list()),
0,
)
def test_add_empty_list(self) -> None:
# NOTE: You will need to define __eq__ in your Node class for
# this test to pass.
self.assertEqual(
add(empty_list(), 0, "hello"),
Node("hello", None),
)
# TODO: Add more tests!
if __name__ == "__main__":
unittest.main()
Additionally, implement the __eq_- function in your Node class. This will help with testing. Place your test cases in a file named linked_list_tests.py. Include type signatures and a docstring purpose statement as appropriate
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started