Skip to content

Simplify tests generated by erfa_generator.py#247

Merged
avalentino merged 1 commit into
liberfa:mainfrom
eerovaher:simpler-generated-tests
May 12, 2026
Merged

Simplify tests generated by erfa_generator.py#247
avalentino merged 1 commit into
liberfa:mainfrom
eerovaher:simpler-generated-tests

Conversation

@eerovaher
Copy link
Copy Markdown
Contributor

@eerovaher eerovaher commented May 12, 2026

Among other things erfa_generator.py translates the erfa tests in t_erfa_c.c into a Python module that can be tested with pytest. So far the translation has tried to change the code as little as possible, but writing Python as if it is ANSI C is not taking advantage of the features Python and pytest provide. A less literal translation removes the need for the viv() and vvd() test helpers and improves tracebacks if a test should fail. For example, with the following change to the erfa/tests/test_ufunc.py generated by current main

@@ -3729,7 +3729,7 @@ def test_xys06a():
     x, y, s = erfa_ufunc.xys06a(2400000.5, 53736.0)
     vvd(x,  0.5791308482835292617e-3, 1e-14, "xys06a", "x", status)
     vvd(y,  0.4020580099454020310e-4, 1e-15, "xys06a", "y", status)
-    vvd(s, -0.1220032294164579896e-7, 1e-18, "xys06a", "s", status)
+    vvd(s, -0.1220032294164579896e-7, 0.0, "xys06a", "s", status)
 
 
 @pytest.mark.xfail(np.__version__ < '1.24', reason='numpy < 1.24 do not support no-input ufuncs') 

the resulting traceback is

_____________________________________________ test_xys06a _____________________________________________

    def test_xys06a():
        x, y, s = erfa_ufunc.xys06a(2400000.5, 53736.0)
        vvd(x,  0.5791308482835292617e-3, 1e-14, "xys06a", "x", status)
        vvd(y,  0.4020580099454020310e-4, 1e-15, "xys06a", "y", status)
>       vvd(s, -0.1220032294164579896e-7, 0.0, "xys06a", "s", status)

erfa/tests/test_ufunc.py:3732: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

val = np.float64(-1.22003229416848e-08), valok = -1.22003229416458e-08, dval = 0.0, func = 'xys06a'
test = 's', _ = array(0)

    def vvd(val, valok, dval, func, test, _):
        """Validate a double result."""
        a = val - valok
>       assert a == 0.0 or abs(a) <= abs(dval), (
            f"{func} failed: {test} want {valok:.20g} got {val:.20g} "
            f"(1/{abs(valok / a):.3g})")
E       AssertionError: xys06a failed: s want -1.2200322941645799674e-08 got -1.2200322941684799586e-08 (1/3.13e+11)
E       assert (np.float64(-3.899991152065015e-20) == 0.0 or np.float64(3.899991152065015e-20) <= 0.0)
E        +  where np.float64(3.899991152065015e-20) = abs(np.float64(-3.899991152065015e-20))
E        +  and   0.0 = abs(0.0)

erfa/tests/test_ufunc.py:41: AssertionError

but if an analogous change is made to the file generated by this PR

@@ -3713,7 +3713,7 @@ def test_xys06a():
     x, y, s = erfa_ufunc.xys06a(2400000.5, 53736.0)
     assert x == pytest.approx(0.5791308482835292617e-3, abs=1e-14)
     assert y == pytest.approx(0.4020580099454020310e-4, abs=1e-15)
-    assert s == pytest.approx(-0.1220032294164579896e-7, abs=1e-18)
+    assert s == pytest.approx(-0.1220032294164579896e-7, abs=0.0)
 
 
 @pytest.mark.xfail(np.__version__ < '1.24', reason='numpy < 1.24 do not support no-input ufuncs')

then the traceback is much shorter and much clearer:

_____________________________________________ test_xys06a _____________________________________________

    def test_xys06a():
        x, y, s = erfa_ufunc.xys06a(2400000.5, 53736.0)
        assert x == pytest.approx(0.5791308482835292617e-3, abs=1e-14)
        assert y == pytest.approx(0.4020580099454020310e-4, abs=1e-15)
>       assert s == pytest.approx(-0.1220032294164579896e-7, abs=0.0)
E       assert np.float64(-1...229416848e-08) == -1.2200322941...e-08 ± 0.0e+00
E         
E         comparison failed
E         Obtained: -1.22003229416848e-08
E         Expected: -1.22003229416458e-08 ± 0.0e+00

erfa/tests/test_ufunc.py:3716: AssertionError

Among other things `erfa_generator.py` translates the `erfa` tests in
`t_erfa_c.c` into a Python module that can be tested with `pytest`. So
far the translation has tried to change the code as little as possible,
but writing Python as if it is ANSI C is not taking advantage of the
features Python and `pytest` provide. A less literal translation removes
the need for the `viv()` and `vvd()` test helpers and improves
tracebacks if a test should fail.
@eerovaher eerovaher force-pushed the simpler-generated-tests branch from cf38fa8 to e610876 Compare May 12, 2026 17:29
@avalentino avalentino merged commit 46029c9 into liberfa:main May 12, 2026
28 checks passed
@eerovaher eerovaher deleted the simpler-generated-tests branch May 12, 2026 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants