-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[pytest] | ||
markers = | ||
functional: Functional tests for stuff that the user would do | ||
one_dimensional: Tests in 1D | ||
invalid_args: Tests for invalid argument handling | ||
default_args: Tests for default argument handling | ||
grid_spec: Marker to construct grid data by fixtures | ||
addopts = | ||
--strict-markers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
|
||
@pytest.fixture() | ||
def grid_data(request): | ||
m = request.node.get_closest_marker("grid_spec") | ||
if m is None: | ||
raise ValueError("Grid data marker not defined") | ||
shape = m.kwargs["shape"] | ||
edges = m.kwargs["edges"] | ||
|
||
if len(shape) == 1: | ||
x = np.linspace(*edges, shape[0]) | ||
dx = x[1] - x[0] | ||
return x, dx | ||
|
||
axes = tuple( | ||
[np.linspace(edges[k][0], edges[k][1], shape[k]) for k in range(len(shape))] | ||
) | ||
coords = np.meshgrid(*axes, indexing="ij") | ||
spacings = [axes[k][1] - axes[k][0] for k in range(len(shape))] | ||
return axes, spacings, coords |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import numpy as np | ||
import pytest | ||
from numpy.testing import assert_array_almost_equal | ||
|
||
from findiff import Diff | ||
|
||
pytestmark = pytest.mark.functional | ||
|
||
|
||
@pytest.mark.one_dimensional | ||
class TestDiff_1D: | ||
|
||
@pytest.mark.grid_spec(shape=(101,), edges=(0, 1)) | ||
def test_diff_1d(self, grid_data): | ||
x, dx = grid_data | ||
|
||
u = x**2 | ||
expected = 2 * x | ||
|
||
fd = Diff(0, dx) | ||
actual = fd(u) | ||
|
||
assert_array_almost_equal(expected, actual) | ||
|
||
@pytest.mark.grid_spec(shape=(101,), edges=(0, 1)) | ||
def test_diff_1d_deferred(self, grid_data): | ||
x, dx = grid_data | ||
|
||
u = x**2 | ||
expected = 2 * x | ||
|
||
fd = Diff(0) | ||
fd.set_grid({0: dx}) | ||
actual = fd(u) | ||
|
||
assert_array_almost_equal(expected, actual) | ||
|
||
def test_diff_1d_deferred_called_too_early(self): | ||
u = np.zeros(10) | ||
fd = Diff(0) | ||
with pytest.raises(TypeError, match="Unknown axis type."): | ||
fd(u) | ||
|
||
@pytest.mark.default_args | ||
@pytest.mark.grid_spec(shape=(101,), edges=(0, 1)) | ||
def test_diff_1d_defaults(self, grid_data): | ||
x, dx = grid_data | ||
|
||
u = x**2 | ||
expected = 2 * x | ||
|
||
fd = Diff() | ||
fd.set_grid({0: dx}) | ||
actual = fd(u) | ||
|
||
assert_array_almost_equal(expected, actual) | ||
|
||
@pytest.mark.invalid_args | ||
def test_diff_1d_invalid_args(self): | ||
|
||
with pytest.raises(ValueError, match="Dimension must be >= 0"): | ||
Diff(-1, 1) | ||
|
||
with pytest.raises(ValueError, match="Spacing must be > 0."): | ||
Diff(0, -1) |