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
This commit is contained in:
Alexander Hess 2024-09-19 14:22:10 +02:00
parent 917c217ca0
commit 348cd53767
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
2 changed files with 3 additions and 74 deletions

View file

@ -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]

View file

@ -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`.