Repository: scipy/scipy
File: scipy/special/tests/test_cdflib.py (Python)

Code before:
        k = 5
        p = sp.pdtr(k, m)
        assert_allclose(sp.pdtrik(p, m), k, rtol=1e-15)

    @pytest.mark.parametrize("p, m, k",
                             [(1.8976107553682285e-40, 100, 2),
                              (0.48670120172085135, 100, 99),
                              (8.30383406699052e-69, 1000, 500),
                              (2.252837804125894e-227, 100_000, 90_000)])
    def test_accuracy(self, p, m, k):
        # Reference values for p were computed with mpmath using
        # mp.gammainc(k+1, a=m, regularized=True)
        assert_allclose(sp.pdtrik(p, m), k, rtol=1e-15)

@pytest.mark.parametrize("a, b, p, ref", [
    (0, 0, 0, np.nan),
    (0, 0, 1, np.nan),
    (0, np.inf, 0, np.nan),
    (0, np.inf, 1, np.nan),
    (np.inf, 0, 0, np.nan),
    (np.inf, 0, 1, np.nan),
    (np.inf, np.inf, 0, np.nan),
    (np.inf, np.inf, 1, np.nan)
])
def test_gdtrix_edge_cases(a, b, p, ref):
    assert_allclose(sp.gdtrix(a, b, p), ref, rtol=0)

@pytest.mark.parametrize("p, b, x, ref", [
    (0, 0, 0, np.nan),
    (0, 0, np.inf, np.nan),
    (0, 1, 0, np.nan),
    (0, 1, np.inf, 0),
    (np.inf, 0, 0, np.nan),
    (np.inf, 0, np.inf, np.nan),
    (np.inf, 1, 0, np.nan),
    (np.inf, 1, np.inf, np.nan)
])
def test_gdtria_edge_cases(p, b, x, ref):
    assert_allclose(sp.gdtria(p, b, x), ref, rtol=0)


Review comment (refactor):
```suggestion
    assert_equal(sp.gdtrix(a, b, p), ref)
```
`rtol=0` makes this the same as `assert_equal()`. `assert_equal()` handles `nan` correctly here.

If you want, you could simplify the test by removing the reference value from the parametrized inputs and hard-code `nan` as the desired value in `assert_equal()`.  In that case, the test should be renamed to something like `test_gdtrix_nan_cases`.  Or just go with the suggested change.

Code after:
        k = 5
        p = sp.pdtr(k, m)
        assert_allclose(sp.pdtrik(p, m), k, rtol=1e-15)

    @pytest.mark.parametrize("p, m, k",
                             [(1.8976107553682285e-40, 100, 2),
                              (0.48670120172085135, 100, 99),
                              (8.30383406699052e-69, 1000, 500),
                              (2.252837804125894e-227, 100_000, 90_000)])
    def test_accuracy(self, p, m, k):
        # Reference values for p were computed with mpmath using
        # mp.gammainc(k+1, a=m, regularized=True)
        assert_allclose(sp.pdtrik(p, m), k, rtol=1e-15)

@pytest.mark.parametrize("a, b, p, ref", [
    (0, 0, 0, np.nan),
    (0, 0, 1, np.nan),
    (0, np.inf, 0, np.nan),
    (0, np.inf, 1, np.nan),
    (np.inf, 0, 0, np.nan),
    (np.inf, 0, 1, np.nan),
    (np.inf, np.inf, 0, np.nan),
    (np.inf, np.inf, 1, np.nan)
])
def test_gdtrix_edge_cases(a, b, p, ref):
    assert_equal(sp.gdtrix(a, b, p), ref)

@pytest.mark.parametrize("p, b, x, ref", [
    (0, 0, 0, np.nan),
    (0, 0, np.inf, np.nan),
    (0, 1, 0, np.nan),
    (0, 1, np.inf, 0),
    (np.inf, 0, 0, np.nan),
    (np.inf, 0, np.inf, np.nan),
    (np.inf, 1, 0, np.nan),
    (np.inf, 1, np.inf, np.nan)
])
def test_gdtria_edge_cases(p, b, x, ref):
    assert_equal(sp.gdtria(p, b, x), ref)