Make gf2() cast values in non-strict mode

- so far, `gf2()` runs in a `strict` mode by default
  => `gf2(42)` results in a `ValueError`
- we adapt `gf2()` (a.k.a. `lalib.elements.galois.GF2Element`)
  such that it behaves more like the built-in `bool()`
  => `gf2(42)` returns `one`, like `bool(42)` returns `True`
- further, the `GF2Element` class is adjusted such that
  `one` and `zero` assume their counterparts to be
  `1`-like or `0`-like objects during binary operations;
  other values result in an error
  => for example, `one + 1` works but `one + 42` does not
  => so, when used in binary operations, `one` and `zero`
     continue to cast their counterparts in `strict` mode
- simplify the casting logic
- make `value` a positional-only argument in `gf2()`
  (like most of the built-in constructors)
- provide more usage examples in the docstrings
  clarifying when `strict` mode is used and when not
- propagate the above changes to the test suite:
  + adapt test cases with regard to the `strict` mode logic
  + add new test cases to `assert` how `gf2()`
    can process `str`ings directly
  + rename two test cases involving `complex` numbers
    to mimic the naming from the newly added test cases
This commit is contained in:
Alexander Hess 2024-10-16 14:23:59 +02:00
commit 06c26192ad
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
5 changed files with 162 additions and 21 deletions

View file

@ -115,16 +115,16 @@ class TestGF2Casting:
@pytest.mark.parametrize("value", one_like_values)
def test_cast_ones_not_strictly(self, cls, value):
"""`gf2(value, strict=False)` returns `one`."""
result = cls(value, strict=False)
assert result is one
result1 = cls(value)
assert result1 is one
result2 = cls(value, strict=False)
assert result2 is one
@pytest.mark.overlapping_test
@pytest.mark.parametrize("value", non_strict_one_like_values)
def test_cannot_cast_ones_strictly(self, cls, value):
"""`gf2(value, strict=False)` returns `1`."""
with pytest.raises(ValueError, match="`1`-like or `0`-like"):
cls(value)
"""`gf2(value, strict=True)` needs strict `1`-like values."""
with pytest.raises(ValueError, match="`1`-like or `0`-like"):
cls(value, strict=True)