mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-09-29 17:08:49 +08:00
Add comprehensive test coverage for maths.abs module (#13588)
* Add comprehensive test coverage for maths.abs module - Added test_abs.py with pytest-compatible tests- Tests cover abs_val, abs_min, abs_max, and abs_max_sort functions- Includes edge cases, error conditions, and consistency checks - Follows existing test patterns in the repository- Improves test coverage for issue #9943 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix import formatting for ruff linter compliance * updating DIRECTORY.md --------- Co-authored-by: rvasikarla <rvasikarla@splunk.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
This commit is contained in:
co-authored by
rvasikarla
pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Christian Clauss
cclauss
parent
dca13f6ff9
commit
ccc4b19ea4
@@ -0,0 +1,145 @@
|
||||
# /// script
|
||||
# requires-python = ">=3.13"
|
||||
# dependencies = [
|
||||
# "pytest",
|
||||
# ]
|
||||
# ///
|
||||
|
||||
import pytest
|
||||
|
||||
from maths.abs import abs_max, abs_max_sort, abs_min, abs_val
|
||||
|
||||
|
||||
class TestAbsVal:
|
||||
"""Test cases for abs_val function."""
|
||||
|
||||
def test_positive_numbers(self):
|
||||
"""Test abs_val with positive numbers."""
|
||||
assert abs_val(5) == 5
|
||||
assert abs_val(10.5) == 10.5
|
||||
assert abs_val(0.1) == 0.1
|
||||
|
||||
def test_negative_numbers(self):
|
||||
"""Test abs_val with negative numbers."""
|
||||
assert abs_val(-5) == 5
|
||||
assert abs_val(-10.5) == 10.5
|
||||
assert abs_val(-0.1) == 0.1
|
||||
|
||||
def test_zero(self):
|
||||
"""Test abs_val with zero."""
|
||||
assert abs_val(0) == 0
|
||||
assert abs_val(0.0) == 0.0
|
||||
|
||||
def test_large_numbers(self):
|
||||
"""Test abs_val with large numbers."""
|
||||
assert abs_val(-100000000000) == 100000000000
|
||||
assert abs_val(100000000000) == 100000000000
|
||||
|
||||
|
||||
class TestAbsMin:
|
||||
"""Test cases for abs_min function."""
|
||||
|
||||
def test_positive_numbers(self):
|
||||
"""Test abs_min with positive numbers."""
|
||||
assert abs_min([1, 2, 3, 4, 5]) == 1
|
||||
assert abs_min([5, 1, 3, 4, 2]) == 1
|
||||
|
||||
def test_negative_numbers(self):
|
||||
"""Test abs_min with negative numbers."""
|
||||
assert abs_min([-5, -1, -3, -4, -2]) == -1
|
||||
assert abs_min([-10, -2, -8]) == -2
|
||||
|
||||
def test_mixed_numbers(self):
|
||||
"""Test abs_min with mixed positive and negative numbers."""
|
||||
assert abs_min([3, -10, -2]) == -2
|
||||
assert abs_min([0, 5, 1, 11]) == 0
|
||||
assert abs_min([-3, -1, 2, -11]) == -1
|
||||
|
||||
def test_single_element(self):
|
||||
"""Test abs_min with single element list."""
|
||||
assert abs_min([5]) == 5
|
||||
assert abs_min([-5]) == -5
|
||||
|
||||
def test_empty_list(self):
|
||||
"""Test abs_min with empty list."""
|
||||
with pytest.raises(ValueError, match="abs_min\\(\\) arg is an empty sequence"):
|
||||
abs_min([])
|
||||
|
||||
|
||||
class TestAbsMax:
|
||||
"""Test cases for abs_max function."""
|
||||
|
||||
def test_positive_numbers(self):
|
||||
"""Test abs_max with positive numbers."""
|
||||
assert abs_max([1, 2, 3, 4, 5]) == 5
|
||||
assert abs_max([0, 5, 1, 11]) == 11
|
||||
|
||||
def test_negative_numbers(self):
|
||||
"""Test abs_max with negative numbers."""
|
||||
assert abs_max([-5, -1, -3, -4, -2]) == -5
|
||||
assert abs_max([-10, -2, -8]) == -10
|
||||
|
||||
def test_mixed_numbers(self):
|
||||
"""Test abs_max with mixed positive and negative numbers."""
|
||||
assert abs_max([3, -10, -2]) == -10
|
||||
assert abs_max([-3, -1, 2, -11]) == -11
|
||||
|
||||
def test_single_element(self):
|
||||
"""Test abs_max with single element list."""
|
||||
assert abs_max([5]) == 5
|
||||
assert abs_max([-5]) == -5
|
||||
|
||||
def test_empty_list(self):
|
||||
"""Test abs_max with empty list."""
|
||||
with pytest.raises(ValueError, match="abs_max\\(\\) arg is an empty sequence"):
|
||||
abs_max([])
|
||||
|
||||
|
||||
class TestAbsMaxSort:
|
||||
"""Test cases for abs_max_sort function."""
|
||||
|
||||
def test_positive_numbers(self):
|
||||
"""Test abs_max_sort with positive numbers."""
|
||||
assert abs_max_sort([1, 2, 3, 4, 5]) == 5
|
||||
assert abs_max_sort([0, 5, 1, 11]) == 11
|
||||
|
||||
def test_negative_numbers(self):
|
||||
"""Test abs_max_sort with negative numbers."""
|
||||
assert abs_max_sort([-5, -1, -3, -4, -2]) == -5
|
||||
assert abs_max_sort([-10, -2, -8]) == -10
|
||||
|
||||
def test_mixed_numbers(self):
|
||||
"""Test abs_max_sort with mixed positive and negative numbers."""
|
||||
assert abs_max_sort([3, -10, -2]) == -10
|
||||
assert abs_max_sort([-3, -1, 2, -11]) == -11
|
||||
|
||||
def test_single_element(self):
|
||||
"""Test abs_max_sort with single element list."""
|
||||
assert abs_max_sort([5]) == 5
|
||||
assert abs_max_sort([-5]) == -5
|
||||
|
||||
def test_empty_list(self):
|
||||
"""Test abs_max_sort with empty list."""
|
||||
with pytest.raises(
|
||||
ValueError, match="abs_max_sort\\(\\) arg is an empty sequence"
|
||||
):
|
||||
abs_max_sort([])
|
||||
|
||||
def test_consistency_with_abs_max(self):
|
||||
"""Test that abs_max_sort gives same results as abs_max."""
|
||||
test_cases = [
|
||||
[1, 2, 3, 4, 5],
|
||||
[-5, -1, -3, -4, -2],
|
||||
[3, -10, -2],
|
||||
[0, 5, 1, 11],
|
||||
[-3, -1, 2, -11],
|
||||
[42],
|
||||
[-42],
|
||||
]
|
||||
|
||||
for test_case in test_cases:
|
||||
assert abs_max_sort(test_case) == abs_max(test_case)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(["-v", __file__])
|
||||
Reference in New Issue
Block a user