|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.13" |
| 3 | +# dependencies = [ |
| 4 | +# "pytest", |
| 5 | +# ] |
| 6 | +# /// |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from maths.abs import abs_val, abs_min, abs_max, abs_max_sort |
| 11 | + |
| 12 | + |
| 13 | +class TestAbsVal: |
| 14 | + """Test cases for abs_val function.""" |
| 15 | + |
| 16 | + def test_positive_numbers(self): |
| 17 | + """Test abs_val with positive numbers.""" |
| 18 | + assert abs_val(5) == 5 |
| 19 | + assert abs_val(10.5) == 10.5 |
| 20 | + assert abs_val(0.1) == 0.1 |
| 21 | + |
| 22 | + def test_negative_numbers(self): |
| 23 | + """Test abs_val with negative numbers.""" |
| 24 | + assert abs_val(-5) == 5 |
| 25 | + assert abs_val(-10.5) == 10.5 |
| 26 | + assert abs_val(-0.1) == 0.1 |
| 27 | + |
| 28 | + def test_zero(self): |
| 29 | + """Test abs_val with zero.""" |
| 30 | + assert abs_val(0) == 0 |
| 31 | + assert abs_val(0.0) == 0.0 |
| 32 | + |
| 33 | + def test_large_numbers(self): |
| 34 | + """Test abs_val with large numbers.""" |
| 35 | + assert abs_val(-100000000000) == 100000000000 |
| 36 | + assert abs_val(100000000000) == 100000000000 |
| 37 | + |
| 38 | + |
| 39 | +class TestAbsMin: |
| 40 | + """Test cases for abs_min function.""" |
| 41 | + |
| 42 | + def test_positive_numbers(self): |
| 43 | + """Test abs_min with positive numbers.""" |
| 44 | + assert abs_min([1, 2, 3, 4, 5]) == 1 |
| 45 | + assert abs_min([5, 1, 3, 4, 2]) == 1 |
| 46 | + |
| 47 | + def test_negative_numbers(self): |
| 48 | + """Test abs_min with negative numbers.""" |
| 49 | + assert abs_min([-5, -1, -3, -4, -2]) == -1 |
| 50 | + assert abs_min([-10, -2, -8]) == -2 |
| 51 | + |
| 52 | + def test_mixed_numbers(self): |
| 53 | + """Test abs_min with mixed positive and negative numbers.""" |
| 54 | + assert abs_min([3, -10, -2]) == -2 |
| 55 | + assert abs_min([0, 5, 1, 11]) == 0 |
| 56 | + assert abs_min([-3, -1, 2, -11]) == -1 |
| 57 | + |
| 58 | + def test_single_element(self): |
| 59 | + """Test abs_min with single element list.""" |
| 60 | + assert abs_min([5]) == 5 |
| 61 | + assert abs_min([-5]) == -5 |
| 62 | + |
| 63 | + def test_empty_list(self): |
| 64 | + """Test abs_min with empty list.""" |
| 65 | + with pytest.raises(ValueError, match="abs_min\\(\\) arg is an empty sequence"): |
| 66 | + abs_min([]) |
| 67 | + |
| 68 | + |
| 69 | +class TestAbsMax: |
| 70 | + """Test cases for abs_max function.""" |
| 71 | + |
| 72 | + def test_positive_numbers(self): |
| 73 | + """Test abs_max with positive numbers.""" |
| 74 | + assert abs_max([1, 2, 3, 4, 5]) == 5 |
| 75 | + assert abs_max([0, 5, 1, 11]) == 11 |
| 76 | + |
| 77 | + def test_negative_numbers(self): |
| 78 | + """Test abs_max with negative numbers.""" |
| 79 | + assert abs_max([-5, -1, -3, -4, -2]) == -5 |
| 80 | + assert abs_max([-10, -2, -8]) == -10 |
| 81 | + |
| 82 | + def test_mixed_numbers(self): |
| 83 | + """Test abs_max with mixed positive and negative numbers.""" |
| 84 | + assert abs_max([3, -10, -2]) == -10 |
| 85 | + assert abs_max([-3, -1, 2, -11]) == -11 |
| 86 | + |
| 87 | + def test_single_element(self): |
| 88 | + """Test abs_max with single element list.""" |
| 89 | + assert abs_max([5]) == 5 |
| 90 | + assert abs_max([-5]) == -5 |
| 91 | + |
| 92 | + def test_empty_list(self): |
| 93 | + """Test abs_max with empty list.""" |
| 94 | + with pytest.raises(ValueError, match="abs_max\\(\\) arg is an empty sequence"): |
| 95 | + abs_max([]) |
| 96 | + |
| 97 | + |
| 98 | +class TestAbsMaxSort: |
| 99 | + """Test cases for abs_max_sort function.""" |
| 100 | + |
| 101 | + def test_positive_numbers(self): |
| 102 | + """Test abs_max_sort with positive numbers.""" |
| 103 | + assert abs_max_sort([1, 2, 3, 4, 5]) == 5 |
| 104 | + assert abs_max_sort([0, 5, 1, 11]) == 11 |
| 105 | + |
| 106 | + def test_negative_numbers(self): |
| 107 | + """Test abs_max_sort with negative numbers.""" |
| 108 | + assert abs_max_sort([-5, -1, -3, -4, -2]) == -5 |
| 109 | + assert abs_max_sort([-10, -2, -8]) == -10 |
| 110 | + |
| 111 | + def test_mixed_numbers(self): |
| 112 | + """Test abs_max_sort with mixed positive and negative numbers.""" |
| 113 | + assert abs_max_sort([3, -10, -2]) == -10 |
| 114 | + assert abs_max_sort([-3, -1, 2, -11]) == -11 |
| 115 | + |
| 116 | + def test_single_element(self): |
| 117 | + """Test abs_max_sort with single element list.""" |
| 118 | + assert abs_max_sort([5]) == 5 |
| 119 | + assert abs_max_sort([-5]) == -5 |
| 120 | + |
| 121 | + def test_empty_list(self): |
| 122 | + """Test abs_max_sort with empty list.""" |
| 123 | + with pytest.raises(ValueError, match="abs_max_sort\\(\\) arg is an empty sequence"): |
| 124 | + abs_max_sort([]) |
| 125 | + |
| 126 | + def test_consistency_with_abs_max(self): |
| 127 | + """Test that abs_max_sort gives same results as abs_max.""" |
| 128 | + test_cases = [ |
| 129 | + [1, 2, 3, 4, 5], |
| 130 | + [-5, -1, -3, -4, -2], |
| 131 | + [3, -10, -2], |
| 132 | + [0, 5, 1, 11], |
| 133 | + [-3, -1, 2, -11], |
| 134 | + [42], |
| 135 | + [-42] |
| 136 | + ] |
| 137 | + |
| 138 | + for test_case in test_cases: |
| 139 | + assert abs_max_sort(test_case) == abs_max(test_case) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + pytest.main(["-v", __file__]) |
0 commit comments