- add `lalib.fields.base.Field`, a blueprint for all concrete fields, providing a unified interface to be used outside of the `lalib.fields` sub-package - implement `lalib.fields.complex_.ComplexField`, or `C` for short, the field over the complex numbers (modeled as `complex` numbers) - implement `lalib.fields.galois.GaloisField2`, or `GF2` for short, the (finite) field over the two elements `one` and `zero` + adapt `lalib.elements.galois.GF2Element.__eq__()` to return `NotImplemented` instead of `False` for non-castable `other`s => this fixes a minor issue with `pytest.approx()` - implement `lalib.fields.rational.RationalField`, or `Q` for short, the field over the rational numbers (modeled as `fractions.Fraction`s) - implement `lalib.fields.real.RealField`, or `R` for short, the field over the real numbers (modeled as `float`s) - organize top-level imports for `lalib.fields`, making `Q`, `R`, `C`, and `GF2` importable with `from lalib.fields import *` - provide extensive unit and integration tests for the new objects: + test generic and common behavior in `tests.fields.test_base` + test specific behavior is other modules + test the well-known math axioms for all fields (integration tests) + test the new objects' docstrings + add "pytest-repeat" to run randomized tests many times
29 lines
697 B
Python
29 lines
697 B
Python
"""Tests for the `lalib.fields.rational.RationalField` only."""
|
|
|
|
import fractions
|
|
|
|
import pytest
|
|
|
|
from lalib import fields
|
|
|
|
|
|
Q = fields.Q
|
|
|
|
|
|
class TestCastAndValidateFieldElements:
|
|
"""Test specifics for `Q.cast()` and `Q.validate()`."""
|
|
|
|
@pytest.mark.parametrize(
|
|
"value",
|
|
["1", "0", "1/1", "0/1", "+42", "-42", "+42/1", "-42/1"],
|
|
)
|
|
def test_str_is_field_element(self, value):
|
|
"""`fractions.Fraction()` also accepts `str`ings.
|
|
|
|
Source: https://docs.python.org/3/library/fractions.html#fractions.Fraction
|
|
"""
|
|
left = Q.cast(value)
|
|
right = fractions.Fraction(value)
|
|
|
|
assert left == right
|
|
assert Q.validate(value)
|