From 348cd5376748391c39d14394d520d97e1d9ee6c9 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Thu, 19 Sep 2024 14:22:10 +0200 Subject: [PATCH] Make `lalib.elements.galois.to_gf2()` a detail - `to_gf2()` is only to be used within `GF2Element.__new__()` => rename it into `_to_gf2()` - the test cases in `TestGF2ConstructorWithCastedValue` cover everything in `TestGF2Casting` => retire the redundant test cases and make the test suite run faster --- src/lalib/elements/galois.py | 4 +- tests/elements/test_galois.py | 73 +---------------------------------- 2 files changed, 3 insertions(+), 74 deletions(-) diff --git a/src/lalib/elements/galois.py b/src/lalib/elements/galois.py index 7a60c37..979c9e0 100644 --- a/src/lalib/elements/galois.py +++ b/src/lalib/elements/galois.py @@ -46,7 +46,7 @@ except ImportError: # pragma: no cover to support Python 3.9 & 3.10 THRESHOLD = 1e-12 -def to_gf2( +def _to_gf2( value: complex, # `mypy` reads `complex | float | int` *, strict: bool = True, @@ -148,7 +148,7 @@ class GF2Element(metaclass=GF2Meta): msg = "Must create `one` and `zero` first (internal error)" raise RuntimeError(msg) from None else: - value = to_gf2(value, strict=strict, threshold=threshold) # type: ignore[arg-type] + value = _to_gf2(value, strict=strict, threshold=threshold) # type: ignore[arg-type] try: return cls._instances[value] diff --git a/tests/elements/test_galois.py b/tests/elements/test_galois.py index 5e82714..b3efa7a 100644 --- a/tests/elements/test_galois.py +++ b/tests/elements/test_galois.py @@ -19,8 +19,6 @@ one, zero = ( galois.zero, ) -to_gf2 = galois.to_gf2 - gf2, GF2One, GF2Zero = ( galois.gf2, galois.GF2One, @@ -89,77 +87,8 @@ def test_thresholds(): assert within_threshold < default_threshold < not_within_threshold -class TestGF2Casting: - """Test the `to_gf2()` function. - - `to_gf2(...)` casts numbers into either `1` or `0`. - """ - - @pytest.mark.parametrize("value", strict_one_like_values) - def test_cast_ones_strictly(self, value): - """`to_gf2(value, strict=True)` returns `1`.""" - result1 = to_gf2(value) # `strict=True` by default - assert result1 == 1 - - result2 = to_gf2(value, strict=True) - assert result2 == 1 - - @pytest.mark.parametrize("value", one_like_values) - def test_cast_ones_not_strictly(self, value): - """`to_gf2(value, strict=False)` returns `1`.""" - result = to_gf2(value, strict=False) - assert result == 1 - - @pytest.mark.parametrize("value", non_strict_one_like_values) - def test_cannot_cast_ones_strictly(self, value): - """`to_gf2(value, strict=False)` returns `1`.""" - with pytest.raises(ValueError, match="`1`-like or `0`-like"): - to_gf2(value) - - with pytest.raises(ValueError, match="`1`-like or `0`-like"): - to_gf2(value, strict=True) - - @pytest.mark.parametrize("value", zero_like_values) - def test_cast_zeros(self, value): - """`to_gf2(value, strict=...)` returns `0`.""" - result1 = to_gf2(value) # `strict=True` by default - assert result1 == 0 - - result2 = to_gf2(value, strict=True) - assert result2 == 0 - - result3 = to_gf2(value, strict=False) - assert result3 == 0 - - @pytest.mark.parametrize( - "value", - [ - complex(1, not_within_threshold), - complex(0, not_within_threshold), - ], - ) - @pytest.mark.parametrize("strict", [True, False]) - def test_cannot_cast_with_non_zero_imag_part(self, value, strict): - """Cannot create `1` or `0` if `.imag != 0`.""" - with pytest.raises(ValueError, match="`1`-like or `0`-like"): - to_gf2(value, strict=strict) - - @pytest.mark.parametrize("value", ["abc", (1,), [1]]) - @pytest.mark.parametrize("strict", [True, False]) - def test_cannot_cast_from_wrong_type(self, value, strict): - """Cannot create `1` or `0` from a non-numeric value.""" - with pytest.raises(TypeError): - to_gf2(value, strict=strict) - - @pytest.mark.parametrize("strict", [True, False]) - def test_cannot_cast_from_nan_value(self, strict): - """Cannot create `1` or `0` from undefined value.""" - with pytest.raises(ValueError, match="`1`-like or `0`-like"): - to_gf2(float("NaN"), strict=strict) - - @pytest.mark.parametrize("cls", [gf2, GF2One, GF2Zero]) -class TestGF2ConstructorWithCastedValue: +class TestGF2Casting: """Test the `gf2` class's constructor. `gf2(value, ...)` returns either `one` or `zero`.