Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write test cases for the code below, also an outline for the test case has been given at the bottom. please use it from __future__

Write test cases for the code below, also an outline for the test case has been given at the bottom. please use it

from __future__ import annotations

from typing import Any

class ArrayList:

# NOTE: Initial capacity is somewhat arbitrary. To making testing

# resizing easier, you'll probably want a small initial capacity.

# In practice, you'd probably have an initial capacity of 510.

def __init__(self, capacity: int = 1):

self.array: list[Any] = [None] * capacity

self.capacity = capacity

self.size = 0

def empty_list() -> ArrayList:

return ArrayList()

def add(lst: ArrayList, index: int, value: Any) -> ArrayList:

if index < 0 or index > lst.size:

raise IndexError("Invalid index")

if lst.size == lst.capacity:

new_array = [None] * (2 * lst.capacity)

for i in range(lst.size):

new_array[i] = lst.array[i]

lst.array = new_array

lst.capacity *= 2

for i in range(lst.size, index, -1):

lst.array[i] = lst.array[i - 1]

lst.array[index] = value

lst.size += 1

return lst

def length(lst: ArrayList) -> int:

return lst.size

def get(lst: ArrayList, index: int) -> Any:

if index < 0 or index >= lst.size:

raise IndexError("Invalid index")

return lst.array[index]

def setitem(lst: ArrayList, index: int, value: Any) -> ArrayList:

if index < 0 or index >= lst.size:

raise IndexError("Invalid index")

lst.array[index] = value

return lst

def remove(lst: ArrayList, index: int) -> tuple[Any, ArrayList]:

if index < 0 or index >= lst.size:

raise IndexError("Invalid index")

value = lst.array[index]

for i in range(index, lst.size - 1):

lst.array[i] = lst.array[i + 1]

lst.array[lst.size - 1] = None

lst.size -= 1

return value, lst

#test code outline

import unittest

from array_list import 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_to_list1(self) -> None:

my_list = empty_list()

add(my_list, 0, "hello")

self.assertEqual(my_list.array, ["hello"])

def test_add_to_list2(self) -> None:

my_list = empty_list()

add(my_list, 0, "a")

add(my_list, 1, "b")

add(my_list, 2, "c")

self.assertEqual(my_list.array, ["a", "b", "c", None])

# TODO: Add more tests!

if __name__ == "__main__":

unittest.main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Step: 3

blur-text-image

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

Sybase Database Administrators Handbook

Authors: Brian Hitchcock

1st Edition

0133574776, 978-0133574777

More Books

Students also viewed these Databases questions

Question

Language in Context?

Answered: 1 week ago