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

Commit

Permalink
Merge pull request #7 from syakoo/develop
Browse files Browse the repository at this point in the history
Add __repr__ to ElementInFp and ElementInFpn.
  • Loading branch information
syakoo authored Nov 13, 2020
2 parents 0e504ee + 315eee0 commit 3a31de8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions galois_field/ElementInGFp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def inverse(self) -> ElementInGFp:
def __str__(self) -> str:
return f'{str(self.value)} (mod {self.p})'

def __repr__(self) -> str:
return f'ElementInGFp({self.value}, {self.p})'

def __add__(self, other: ElementInGFp) -> ElementInGFp:
return ElementInGFp(self.value + other.value, self.p)

Expand Down
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
11 changes: 11 additions & 0 deletions tests/test_ElementInGFp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ def test_GFElementInGFp_str(value, p, expected):
assert str(result) == expected


@pytest.mark.parametrize('value, p, expected', [
(1, 5, 'ElementInGFp(1, 5)'),
(222, 11, 'ElementInGFp(2, 11)'),
(-2, 123456791, 'ElementInGFp(123456789, 123456791)')
])
def test_GFElementInGFp_repr(value, p, expected):
result = ElementInGFp(value, p)

assert repr(result) == expected


@pytest.mark.parametrize('value1, value2, p, expected_value', [
(1, 1, 5, 2),
(111, 111, 11, 2),
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 3a31de8

Please sign in to comment.