diff --git a/bosco/benchmark.py b/bosco/benchmark.py index cb7f986..18c7335 100644 --- a/bosco/benchmark.py +++ b/bosco/benchmark.py @@ -3,11 +3,11 @@ import os import sys from timeit import repeat -from typing import List, Tuple +from typing import Any, List, Tuple def run_sorting_algorithm( - file_path: str, algorithm: str, array: List[int] + file_path: str, algorithm: str, array: List[Any] ) -> Tuple[float, float, float]: """Run a sorting algorithm and profile it with the timeit package.""" directory, file_name = os.path.split(file_path) diff --git a/bosco/generate.py b/bosco/generate.py index a52e34b..a858b09 100644 --- a/bosco/generate.py +++ b/bosco/generate.py @@ -8,12 +8,16 @@ def generate_random_container( type_: str = "int", ) -> List[Union[int, float, str]]: """Generate a random list defined by the size and element type.""" + random_list: List[Union[int, float, str]] = [] + if type_ == "int": # Generate a list of random integers within the range [1, size*size] random_list = [random.randint(1, size * size) for _ in range(size)] elif type_ == "float": # Generate a list of random floating-point numbers within the range [1, size*size] - random_list = [random.uniform(1, size * size) for _ in range(size)] + random_list = [ + float(random.uniform(1, size * size)) for _ in range(size) + ] elif type_ == "str": # Generate a list of random strings of length 10 composed of letters and digits random_list = [ diff --git a/bosco/main.py b/bosco/main.py index b5852c5..15bf745 100644 --- a/bosco/main.py +++ b/bosco/main.py @@ -1,10 +1,10 @@ """Bosco runs benchmarks to assess the performance of Python functions.""" -import plotly.graph_objs as go +import plotly.graph_objs as go # type: ignore import typer -from plotly.subplots import make_subplots +from plotly.subplots import make_subplots # type: ignore from rich.console import Console -from tabulate import tabulate +from tabulate import tabulate # type: ignore from bosco import benchmark, generate diff --git a/poetry.lock b/poetry.lock index 47165aa..9bc812e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -537,18 +537,18 @@ pytest = "*" [[package]] name = "pytest-xdist" -version = "3.5.0" +version = "3.6.1" description = "pytest xdist plugin for distributed testing, most importantly across multiple CPUs" optional = false -python-versions = ">=3.7" +python-versions = ">=3.8" files = [ - {file = "pytest-xdist-3.5.0.tar.gz", hash = "sha256:cbb36f3d67e0c478baa57fa4edc8843887e0f6cfc42d677530a36d7472b32d8a"}, - {file = "pytest_xdist-3.5.0-py3-none-any.whl", hash = "sha256:d075629c7e00b611df89f490a5063944bee7a4362a5ff11c7cc7824a03dfce24"}, + {file = "pytest_xdist-3.6.1-py3-none-any.whl", hash = "sha256:9ed4adfb68a016610848639bb7e02c9352d5d9f03d04809919e2dafc3be4cca7"}, + {file = "pytest_xdist-3.6.1.tar.gz", hash = "sha256:ead156a4db231eec769737f57668ef58a2084a34b2e55c4a8fa20d861107300d"}, ] [package.dependencies] -execnet = ">=1.1" -pytest = ">=6.2.0" +execnet = ">=2.1" +pytest = ">=7.0.0" [package.extras] psutil = ["psutil (>=3.0)"] diff --git a/poetry.toml b/poetry.toml deleted file mode 100644 index 351b7ba..0000000 --- a/poetry.toml +++ /dev/null @@ -1,2 +0,0 @@ -[installer] -no-binary = ["ruff"] diff --git a/tests/test_sorting.py b/tests/test_sorting.py new file mode 100644 index 0000000..19dc876 --- /dev/null +++ b/tests/test_sorting.py @@ -0,0 +1,209 @@ +import pytest + +from bosco import sorting + +class TestBubbleSort: + + # The function sorts a list of integers in ascending order. + def test_sort_ascending_order(self): + # Arrange + input_list = [4, 2, 1, 3] + expected_output = [1, 2, 3, 4] + + # Act + result = sorting.bubble_sort(input_list) + + # Assert + assert result == expected_output + + # The function sorts an empty list and returns an empty list. + def test_sort_empty_list(self): + # Arrange + input_list = [] + expected_output = [] + + # Act + result = sorting.bubble_sort(input_list) + + # Assert + assert result == expected_output + +class TestInsertionSort: + + # The function sorts a list of integers in ascending order. + def test_sorts_list_in_ascending_order(self): + # Arrange + input_list = [4, 2, 1, 3] + expected_output = [1, 2, 3, 4] + + # Act + result = sorting.insertion_sort(input_list) + + # Assert + assert result == expected_output + + # The function works correctly with an empty list. + def test_works_correctly_with_empty_list(self): + # Arrange + input_list = [] + expected_output = [] + + # Act + result = sorting.insertion_sort(input_list) + + # Assert + assert result == expected_output + +class TestMerge: + + # Merging two non-empty arrays of equal length + def test_merge_equal_length(self): + # Arrange + left = [1, 3, 5] + right = [2, 4, 6] + expected_result = [1, 2, 3, 4, 5, 6] + + # Act + result = sorting.merge(left, right) + + # Assert + assert result == expected_result + + # Merging an empty array with a non-empty array + def test_merge_empty_array(self): + # Arrange + left = [] + right = [1, 2, 3] + expected_result = [1, 2, 3] + + # Act + result = sorting.merge(left, right) + + # Assert + assert result == expected_result + +class TestMergeSort: + + # The function correctly sorts a list of integers in ascending order. + def test_sort_list_of_integers(self): + # Arrange + array = [5, 2, 8, 1, 9, 3] + expected_result = [1, 2, 3, 5, 8, 9] + + # Act + result = sorting.merge_sort(array) + + # Assert + assert result == expected_result + + # The function correctly sorts an empty list. + def test_sort_empty_list(self): + # Arrange + array = [] + expected_result = [] + + # Act + result = sorting.merge_sort(array) + + # Assert + assert result == expected_result + +class TestSelectionSort: + + # The function sorts a list of integers in ascending order. + def test_sort_ascending_order(self): + # Arrange + array = [4, 2, 1, 3] + expected_result = [1, 2, 3, 4] + + # Act + sorting.selection_sort(array) + + # Assert + assert array == expected_result + + # The function sorts an empty list. + def test_sort_empty_list(self): + # Arrange + array = [] + expected_result = [] + + # Act + sorting.selection_sort(array) + + # Assert + assert array == expected_result + +class TestHeapify: + + # Given an array with one element, the function should return the same array + def test_array_with_one_element(self): + # Arrange + array = [5] + + # Act + sorting.heapify(array, len(array), 0) + + # Assert + assert array == [5] + + # Given an empty array, the function should return an empty array + def test_empty_array(self): + # Arrange + array = [] + + # Act + sorting.heapify(array, len(array), 0) + + # Assert + assert array == [] + +class TestShellSort: + + # The function sorts a list of integers in ascending order. + def test_sorts_list_in_ascending_order(self): + # Arrange + array = [4, 2, 7, 1, 5] + + # Act + result = sorting.shell_sort(array) + + # Assert + assert result == [1, 2, 4, 5, 7] + + # The function sorts an empty list. + def test_sorts_empty_list(self): + # Arrange + array = [] + + # Act + result = sorting.shell_sort(array) + + # Assert + assert result == [] + +class TestBucketSort: + + # sorts a list of positive integers in ascending order + def test_sort_positive_integers(self): + # Arrange + arr = [5, 2, 8, 1, 9] + expected = [1, 2, 5, 8, 9] + + # Act + result = sorting.bucket_sort(arr) + + # Assert + assert result == expected + + # sorts an empty list + def test_sort_empty_list(self): + # Arrange + arr = [] + expected = [] + + # Act + result = sorting.bucket_sort(arr) + + # Assert + assert result == expected \ No newline at end of file