Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
add: __repr__ to ElementInGFpn.
Browse files Browse the repository at this point in the history
  • Loading branch information
syakoo committed Nov 13, 2020
1 parent 0e504ee commit 807563f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions galois_field/ElementInGFpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def __str__(self):

return "".join(result[::-1])

def __repr__(self) -> str:
def c2str(array): return str(list(array))
mod_str = c2str(self.mod_poly.coeffs)
return f'ElementInGFpn({c2str(self.coeffs)}, {self.p}, {mod_str})'

def __add__(self, other: ElementInGFpn) -> ElementInGFpn:
result = self.poly + other.poly

Expand Down
12 changes: 12 additions & 0 deletions tests/test_ElementInGFpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ def test_ElementInGFpn_str(coeffs, p, mod_coeffs, expected):
assert str(result) == expected


@pytest.mark.parametrize('coeffs, p, mod_coeffs, expected', [
(np.array([4, 3, 2, 1]), 5, np.array([1, 0, 2]),
'ElementInGFpn([4, 0], 5, [1, 0, 2])'),
(np.array([2, 1]), 11, np.array([1, 0, 1]),
'ElementInGFpn([2, 1], 11, [1, 0, 1])'),
])
def test_ElementInGFpn_repr(coeffs, p, mod_coeffs, expected):
result = ElementInGFpn(coeffs, p, np.poly1d(mod_coeffs))

assert repr(result) == expected


@pytest.mark.parametrize('coeffs1, coeffs2, p, mod_coeffs, expected_coeffs', [
(np.array([1, 2, 3, 4]), np.array([1, 2, 3, 4]), 5,
np.array([1, 0, 0, 0, 1]), np.array([2, 4, 1, 3])),
Expand Down

0 comments on commit 807563f

Please sign in to comment.