Skip to content

Commit 2024ab5

Browse files
committed
TST: Improve Function.integral tests
1 parent 217741e commit 2024ab5

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

tests/conftest.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,23 @@ def linear_func():
200200

201201

202202
@pytest.fixture
203-
def spline_func():
203+
def linearly_interpolated_func():
204+
"""Create a linearly interpolated function based on a list of points.
205+
206+
Returns
207+
-------
208+
Function
209+
Piece-wise linearly interpolated, with constant extrapolation
210+
"""
211+
return Function(
212+
[[0, 0], [1, 7], [2, -3], [3, -1], [4, 3]],
213+
interpolation="spline",
214+
extrapolation="constant",
215+
)
216+
217+
218+
@pytest.fixture
219+
def spline_interpolated_func():
204220
"""Create a spline interpolated function based on a list of points.
205221
206222
Returns

tests/test_function.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ def test_extrapolation_methods(linear_func):
134134
assert np.isclose(linear_func.getValue(-1), -1, atol=1e-6)
135135

136136

137-
def test_integral_linear_interpolation(linear_func):
137+
@pytest.mark.parametrize("a", [-1, 0, 0.5, 1, 2, 2.5, 3.5, 4, 5])
138+
@pytest.mark.parametrize("b", [-1, 0, 0.5, 1, 2, 2.5, 3.5, 4, 5])
139+
def test_integral_linear_interpolation(linearly_interpolated_func, a, b):
138140
"""Test the integral method of the Function class.
139141
140142
Parameters
@@ -143,12 +145,17 @@ def test_integral_linear_interpolation(linear_func):
143145
A Function object created from a list of values.
144146
"""
145147
# Test integral
146-
assert np.isclose(linear_func.integral(0, 1), 0.5, atol=1e-6)
148+
assert np.isclose(
149+
linearly_interpolated_func.integral(a, b, numerical=False),
150+
linearly_interpolated_func.integral(a, b, numerical=True),
151+
atol=1e-3,
152+
)
147153

148154

149-
@pytest.mark.parametrize("a", [-1, 0, 0.5, 1, 2, 2.5, 3.5, 4, 5])
150-
@pytest.mark.parametrize("b", [-1, 0, 0.5, 1, 2, 2.5, 3.5, 4, 5])
151-
def test_integral_spline_interpolation(spline_func, a, b):
155+
@pytest.mark.parametrize("func", ["linear_func", "spline_interpolated_func"])
156+
@pytest.mark.parametrize("a", [-1, -0.5, 0, 0.5, 1, 2, 2.5, 3.5, 4, 5])
157+
@pytest.mark.parametrize("b", [-1, -0.5, 0, 0.5, 1, 2, 2.5, 3.5, 4, 5])
158+
def test_integral_spline_interpolation(request, func, a, b):
152159
"""Test the integral method of the Function class.
153160
154161
Parameters
@@ -161,8 +168,10 @@ def test_integral_spline_interpolation(spline_func, a, b):
161168
Upper limit of the integral.
162169
"""
163170
# Test integral
171+
# Get the function from the fixture
172+
func = request.getfixturevalue(func)
164173
assert np.isclose(
165-
spline_func.integral(a, b, numerical=False),
166-
spline_func.integral(a, b, numerical=True),
174+
func.integral(a, b, numerical=False),
175+
func.integral(a, b, numerical=True),
167176
atol=1e-3,
168177
)

0 commit comments

Comments
 (0)