-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathtest_abs.py
More file actions
145 lines (112 loc) · 4.37 KB
/
test_abs.py
File metadata and controls
145 lines (112 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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__])