Coverage for tests/test_envelope.py: 100%
42 statements
« prev ^ index » next coverage.py v7.15.3, created at 2026-09-03 17:23 +1200
« prev ^ index » next coverage.py v7.15.3, created at 2026-09-03 17:23 +1200
1from hypothesis import given, strategies as st
2import numpy as np
3import pytest
4from pytest_mock import MockerFixture
6from crispatt.crispatt import Ensemble, _EnvelopeManager
9@pytest.mark.parametrize("resolution", [None, 1.0])
10@pytest.mark.parametrize("n_samples", [None, 2])
11def test_get_x_branching(
12 n_samples: int | None, resolution: float | None, mocker: MockerFixture
13):
14 spy_n_samples = mocker.spy(_EnvelopeManager, "_x_from_n_samples")
15 spy_resolution = mocker.spy(_EnvelopeManager, "_x_from_resolution")
16 env_manager = _EnvelopeManager((0, 1))
18 # Explicitely write down the four cases.
19 if n_samples is None and resolution is None:
20 with pytest.raises(TypeError):
21 _ = env_manager._get_x(n_samples, resolution)
22 elif n_samples is not None and resolution is None:
23 x = env_manager._get_x(n_samples, resolution)
24 assert x.size == n_samples
25 spy_n_samples.assert_called_once_with(env_manager, n_samples)
26 spy_resolution.assert_not_called()
27 elif n_samples is not None and resolution is not None:
28 with pytest.warns(UserWarning):
29 _ = env_manager._get_x(n_samples, resolution)
30 spy_n_samples.assert_called_once_with(env_manager, n_samples)
31 spy_resolution.assert_not_called()
32 else:
33 _ = env_manager._get_x(n_samples, resolution)
34 spy_n_samples.assert_not_called()
35 spy_resolution.assert_called_once_with(env_manager, resolution)
38@given(n_samples=st.integers(1, 10))
39def test_x_from_n(n_samples: int):
40 env_manager = _EnvelopeManager((0, 1))
41 x = env_manager._x_from_n_samples(n_samples)
42 assert x.size == n_samples
43 assert x[0] == env_manager.support[0]
44 if n_samples > 1:
45 assert x[-1] == env_manager.support[-1]
48@given(resolution=st.floats(min_value=0.1, max_value=10))
49def test_x_from_res(resolution: float):
50 env_manager = _EnvelopeManager((0, 5))
51 x = env_manager._x_from_resolution(resolution)
52 assert x.size > 1
53 assert x[1] - x[0] <= resolution
54 assert np.all(x[[0, -1]] == env_manager.support)