diff --git a/galois/_codes/_bch.py b/galois/_codes/_bch.py index 24d24b150..0f24fbb56 100644 --- a/galois/_codes/_bch.py +++ b/galois/_codes/_bch.py @@ -18,7 +18,7 @@ from .._polys import Poly, matlab_primitive_poly from .._polys._dense import roots_jit, divmod_jit from .._prime import factors -from ..typing import PolyLike +from ..typing import ArrayLike, PolyLike from ._cyclic import poly_to_generator_matrix, roots_to_parity_check_matrix @@ -297,7 +297,7 @@ def __str__(self) -> str: return string - def encode(self, message: Union[np.ndarray, GF2], parity_only: bool = False) -> GF2: + def encode(self, message: ArrayLike, parity_only: bool = False) -> GF2: r""" Encodes the message :math:`\mathbf{m}` into the BCH codeword :math:`\mathbf{c}`. @@ -405,8 +405,7 @@ def encode(self, message: Union[np.ndarray, GF2], parity_only: bool = False) -> p = bch.encode(m, parity_only=True); p """ - if not isinstance(message, (np.ndarray, GF2)): - raise TypeError(f"Argument `message` must be a subclass of np.ndarray (or a galois.GF2 array), not {type(message)}.") + message = GF2(message) # This performs type/value checking if parity_only and not self.is_systematic: raise ValueError("Argument `parity_only=True` only applies to systematic codes.") if self.is_systematic: @@ -419,17 +418,17 @@ def encode(self, message: Union[np.ndarray, GF2], parity_only: bool = False) -> ks = message.shape[-1] # The number of input message bits (could be less than self.k for shortened codes) if parity_only: - parity = message.view(GF2) @ self.G[-ks:, self.k:] + parity = message @ self.G[-ks:, self.k:] return parity elif self.is_systematic: - parity = message.view(GF2) @ self.G[-ks:, self.k:] + parity = message @ self.G[-ks:, self.k:] codeword = np.hstack((message, parity)) return codeword else: - codeword = message.view(GF2) @ self.G + codeword = message @ self.G return codeword - def detect(self, codeword: Union[np.ndarray, GF2]) -> Union[np.bool_, np.ndarray]: + def detect(self, codeword: ArrayLike) -> Union[np.bool_, np.ndarray]: r""" Detects if errors are present in the BCH codeword :math:`\mathbf{c}`. @@ -558,8 +557,7 @@ def detect(self, codeword: Union[np.ndarray, GF2]) -> Union[np.bool_, np.ndarray c bch.detect(c) """ - if not isinstance(codeword, np.ndarray): - raise TypeError(f"Argument `codeword` must be a subclass of np.ndarray (or a galois.GF2 array), not {type(codeword)}.") + codeword = GF2(codeword) # This performs type/value checking if self.is_systematic: if not codeword.shape[-1] <= self.n: raise ValueError(f"For a systematic code, argument `codeword` must be a 1-D or 2-D array with last dimension less than or equal to {self.n}, not shape {codeword.shape}.") @@ -584,10 +582,10 @@ def detect(self, codeword: Union[np.ndarray, GF2]) -> Union[np.bool_, np.ndarray return detected @overload - def decode(self, codeword: Union[np.ndarray, GF2], errors: Literal[False] = False) -> GF2: + def decode(self, codeword: ArrayLike, errors: Literal[False] = False) -> GF2: ... @overload - def decode(self, codeword: Union[np.ndarray, GF2], errors: Literal[True]) -> Tuple[GF2, Union[np.integer, np.ndarray]]: + def decode(self, codeword: ArrayLike, errors: Literal[True]) -> Tuple[GF2, Union[np.integer, np.ndarray]]: ... def decode(self, codeword, errors=False): r""" @@ -760,8 +758,7 @@ def decode(self, codeword, errors=False): d, e = bch.decode(c, errors=True); d, e np.array_equal(d, m) """ - if not isinstance(codeword, (np.ndarray, GF2)): - raise TypeError(f"Argument `codeword` must be a subclass of np.ndarray (or a galois.GF2 array), not {type(codeword)}.") + codeword = GF2(codeword) # This performs type/value checking if self.is_systematic: if not codeword.shape[-1] <= self.n: raise ValueError(f"For a systematic code, argument `codeword` must be a 1-D or 2-D array with last dimension less than or equal to {self.n}, not shape {codeword.shape}.") diff --git a/galois/_codes/_reed_solomon.py b/galois/_codes/_reed_solomon.py index 5072375cc..1f45ec81d 100644 --- a/galois/_codes/_reed_solomon.py +++ b/galois/_codes/_reed_solomon.py @@ -17,7 +17,7 @@ from .._polys import Poly, matlab_primitive_poly from .._polys._dense import divmod_jit, roots_jit, evaluate_elementwise_jit from .._prime import factors -from ..typing import PolyLike +from ..typing import ArrayLike, PolyLike from ._cyclic import poly_to_generator_matrix, roots_to_parity_check_matrix @@ -182,7 +182,7 @@ def __str__(self) -> str: return string - def encode(self, message: Union[np.ndarray, FieldArray], parity_only: bool = False) -> FieldArray: + def encode(self, message: ArrayLike, parity_only: bool = False) -> FieldArray: r""" Encodes the message :math:`\mathbf{m}` into the Reed-Solomon codeword :math:`\mathbf{c}`. @@ -290,8 +290,7 @@ def encode(self, message: Union[np.ndarray, FieldArray], parity_only: bool = Fal p = rs.encode(m, parity_only=True); p """ - if not isinstance(message, np.ndarray): - raise TypeError(f"Argument `message` must be a subclass of np.ndarray (or a galois.GF2 array), not {type(message)}.") + message = self.field(message) # This performs type/value checking if parity_only and not self.is_systematic: raise ValueError("Argument `parity_only=True` only applies to systematic codes.") if self.is_systematic: @@ -304,17 +303,17 @@ def encode(self, message: Union[np.ndarray, FieldArray], parity_only: bool = Fal ks = message.shape[-1] # The number of input message symbols (could be less than self.k for shortened codes) if parity_only: - parity = message.view(self.field) @ self.G[-ks:, self.k:] + parity = message @ self.G[-ks:, self.k:] return parity elif self.is_systematic: - parity = message.view(self.field) @ self.G[-ks:, self.k:] + parity = message @ self.G[-ks:, self.k:] codeword = np.hstack((message, parity)) return codeword else: - codeword = message.view(self.field) @ self.G + codeword = message @ self.G return codeword - def detect(self, codeword: Union[np.ndarray, FieldArray]) -> Union[np.bool_, np.ndarray]: + def detect(self, codeword: ArrayLike) -> Union[np.bool_, np.ndarray]: r""" Detects if errors are present in the Reed-Solomon codeword :math:`\mathbf{c}`. @@ -445,8 +444,7 @@ def detect(self, codeword: Union[np.ndarray, FieldArray]) -> Union[np.bool_, np. c rs.detect(c) """ - if not isinstance(codeword, np.ndarray): - raise TypeError(f"Argument `codeword` must be a subclass of np.ndarray (or a galois.GF2 array), not {type(codeword)}.") + codeword = self.field(codeword) # This performs type/value checking if self.is_systematic: if not codeword.shape[-1] <= self.n: raise ValueError(f"For a systematic code, argument `codeword` must be a 1-D or 2-D array with last dimension less than or equal to {self.n}, not shape {codeword.shape}.") @@ -471,10 +469,10 @@ def detect(self, codeword: Union[np.ndarray, FieldArray]) -> Union[np.bool_, np. return detected @overload - def decode(self, codeword: Union[np.ndarray, FieldArray], errors: Literal[False] = False) -> FieldArray: + def decode(self, codeword: ArrayLike, errors: Literal[False] = False) -> FieldArray: ... @overload - def decode(self, codeword: Union[np.ndarray, FieldArray], errors: Literal[True]) -> Tuple[FieldArray, Union[np.integer, np.ndarray]]: + def decode(self, codeword: ArrayLike, errors: Literal[True]) -> Tuple[FieldArray, Union[np.integer, np.ndarray]]: ... def decode(self, codeword, errors=False): r""" @@ -650,8 +648,7 @@ def decode(self, codeword, errors=False): d, e = rs.decode(c, errors=True); d, e np.array_equal(d, m) """ - if not isinstance(codeword, np.ndarray): - raise TypeError(f"Argument `codeword` must be a subclass of np.ndarray (or a galois.FieldArray), not {type(codeword)}.") + codeword = self.field(codeword) # This performs type/value checking if self.is_systematic: if not codeword.shape[-1] <= self.n: raise ValueError(f"For a systematic code, argument `codeword` must be a 1-D or 2-D array with last dimension less than or equal to {self.n}, not shape {codeword.shape}.") diff --git a/tests/codes/helper.py b/tests/codes/helper.py index 0229c24c2..44cebe50a 100644 --- a/tests/codes/helper.py +++ b/tests/codes/helper.py @@ -13,3 +13,18 @@ def random_errors(GF, N, n, max_errors): E[i, random.sample(list(range(n)), N_errors[i])] = GF.Random(N_errors[i], low=1) return E, N_errors + + +def random_type(array): + """ + Randomly vary the input type to encode()/decode() across various ArrayLike inputs. + """ + x = random.randint(0, 2) + if x == 0: + # A FieldArray instance + return array + elif x == 1: + # A np.ndarray instance + return array.view(np.ndarray) + else: + return array.tolist() diff --git a/tests/codes/test_bch_decode.py b/tests/codes/test_bch_decode.py index 65546708f..1c52b3b78 100644 --- a/tests/codes/test_bch_decode.py +++ b/tests/codes/test_bch_decode.py @@ -6,7 +6,7 @@ import galois -from .helper import random_errors +from .helper import random_errors, random_type CODES = [ (15, 11), # GF(2^4) with t=1 @@ -32,8 +32,6 @@ def test_exceptions(): n, k = 15, 7 bch = galois.BCH(n, k) GF = galois.GF2 - with pytest.raises(TypeError): - bch.decode(GF.Random(n).tolist()) with pytest.raises(ValueError): bch.decode(GF.Random(n + 1)) @@ -41,8 +39,6 @@ def test_exceptions(): n, k = 15, 7 bch = galois.BCH(n, k, systematic=False) GF = galois.GF2 - with pytest.raises(TypeError): - bch.decode(GF.Random(n).tolist()) with pytest.raises(ValueError): bch.decode(GF.Random(n - 1)) @@ -58,20 +54,13 @@ def test_all_correctable(self, size): E, N_errors = random_errors(galois.GF2, N, n, bch.t) R = C + E - DEC_M = bch.decode(R) + RR = random_type(R) + DEC_M = bch.decode(RR) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M, M) - DEC_M, N_corr = bch.decode(R, errors=True) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M, M) - assert np.array_equal(N_corr, N_errors) - - DEC_M = bch.decode(R.view(np.ndarray)) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M, M) - - DEC_M, N_corr = bch.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = bch.decode(RR, errors=True) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M, M) assert np.array_equal(N_corr, N_errors) @@ -88,20 +77,13 @@ def test_some_uncorrectable(self, size): corr_idxs = np.where(N_errors <= bch.t)[0] - DEC_M = bch.decode(R) + RR = random_type(R) + DEC_M = bch.decode(RR) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - DEC_M, N_corr = bch.decode(R, errors=True) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) - - DEC_M = bch.decode(R.view(np.ndarray)) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - - DEC_M, N_corr = bch.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = bch.decode(RR, errors=True) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) @@ -120,20 +102,13 @@ def test_all_correctable(self, size): E, N_errors = random_errors(galois.GF2, N, ns, bch.t) R = C + E - DEC_M = bch.decode(R) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M, M) - - DEC_M, N_corr = bch.decode(R, errors=True) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M, M) - assert np.array_equal(N_corr, N_errors) - - DEC_M = bch.decode(R.view(np.ndarray)) + RR = random_type(R) + DEC_M = bch.decode(RR) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M, M) - DEC_M, N_corr = bch.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = bch.decode(RR, errors=True) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M, M) assert np.array_equal(N_corr, N_errors) @@ -152,20 +127,13 @@ def test_some_uncorrectable(self, size): corr_idxs = np.where(N_errors <= bch.t)[0] - DEC_M = bch.decode(R) + RR = random_type(R) + DEC_M = bch.decode(RR) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - DEC_M, N_corr = bch.decode(R, errors=True) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) - - DEC_M = bch.decode(R.view(np.ndarray)) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - - DEC_M, N_corr = bch.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = bch.decode(RR, errors=True) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) @@ -182,20 +150,13 @@ def test_all_correctable(self, size): E, N_errors = random_errors(galois.GF2, N, n, bch.t) R = C + E - DEC_M = bch.decode(R) + RR = random_type(R) + DEC_M = bch.decode(RR) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M, M) - DEC_M, N_corr = bch.decode(R, errors=True) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M, M) - assert np.array_equal(N_corr, N_errors) - - DEC_M = bch.decode(R.view(np.ndarray)) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M, M) - - DEC_M, N_corr = bch.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = bch.decode(RR, errors=True) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M, M) assert np.array_equal(N_corr, N_errors) @@ -212,20 +173,13 @@ def test_some_uncorrectable(self, size): corr_idxs = np.where(N_errors <= bch.t)[0] - DEC_M = bch.decode(R) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - - DEC_M, N_corr = bch.decode(R, errors=True) - assert type(DEC_M) is galois.GF2 - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) - - DEC_M = bch.decode(R.view(np.ndarray)) + RR = random_type(R) + DEC_M = bch.decode(RR) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - DEC_M, N_corr = bch.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = bch.decode(RR, errors=True) assert type(DEC_M) is galois.GF2 assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) diff --git a/tests/codes/test_bch_detect.py b/tests/codes/test_bch_detect.py index de9a8aa9c..16db32d8e 100644 --- a/tests/codes/test_bch_detect.py +++ b/tests/codes/test_bch_detect.py @@ -6,7 +6,7 @@ import galois -from .helper import random_errors +from .helper import random_errors, random_type CODES = [ (15, 11), # GF(2^4) with t=1 @@ -32,8 +32,6 @@ def test_exceptions(): n, k = 15, 7 bch = galois.BCH(n, k) GF = galois.GF2 - with pytest.raises(TypeError): - bch.detect(GF.Random(n).tolist()) with pytest.raises(ValueError): bch.detect(GF.Random(n + 1)) @@ -41,8 +39,6 @@ def test_exceptions(): n, k = 15, 7 bch = galois.BCH(n, k, systematic=False) GF = galois.GF2 - with pytest.raises(TypeError): - bch.detect(GF.Random(n).tolist()) with pytest.raises(ValueError): bch.detect(GF.Random(n - 1)) @@ -57,7 +53,8 @@ def test_no_errors(self, size): C = bch.encode(M) R = C - detected = bch.detect(R) + RR = random_type(R) + detected = bch.detect(RR) assert type(detected) is np.ndarray assert np.all(detected == False) @@ -74,7 +71,8 @@ def test_all_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= bch.d - 1))[0] - detected = bch.detect(R) + RR = random_type(R) + detected = bch.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) @@ -92,7 +90,8 @@ def test_some_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= bch.d - 1))[0] - detected = bch.detect(R) + RR = random_type(R) + detected = bch.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) @@ -110,7 +109,8 @@ def test_no_errors(self, size): C = bch.encode(M) R = C - detected = bch.detect(R) + RR = random_type(R) + detected = bch.detect(RR) assert type(detected) is np.ndarray assert np.all(detected == False) @@ -129,7 +129,8 @@ def test_all_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= bch.d - 1))[0] - detected = bch.detect(R) + RR = random_type(R) + detected = bch.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) @@ -149,7 +150,8 @@ def test_some_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= bch.d - 1))[0] - detected = bch.detect(R) + RR = random_type(R) + detected = bch.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) @@ -165,7 +167,8 @@ def test_no_errors(self, size): C = bch.encode(M) R = C - detected = bch.detect(R) + RR = random_type(R) + detected = bch.detect(RR) assert type(detected) is np.ndarray assert np.all(detected == False) @@ -182,7 +185,8 @@ def test_all_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= bch.d - 1))[0] - detected = bch.detect(R) + RR = random_type(R) + detected = bch.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) @@ -200,7 +204,8 @@ def test_some_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= bch.d - 1))[0] - detected = bch.detect(R) + RR = random_type(R) + detected = bch.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) diff --git a/tests/codes/test_bch_encode.py b/tests/codes/test_bch_encode.py index 921c8ffd9..112899054 100644 --- a/tests/codes/test_bch_encode.py +++ b/tests/codes/test_bch_encode.py @@ -12,6 +12,8 @@ import galois +from .helper import random_type + CODES = [ (15, 11), # GF(2^4) with t=1 (15, 7), # GF(2^4) with t=2 @@ -36,8 +38,6 @@ def test_exceptions(): n, k = 15, 7 bch = galois.BCH(n, k) GF = galois.GF2 - with pytest.raises(TypeError): - bch.encode(GF.Random(k).tolist()) with pytest.raises(ValueError): bch.encode(GF.Random(k + 1)) @@ -45,8 +45,6 @@ def test_exceptions(): n, k = 15, 7 bch = galois.BCH(n, k, systematic=False) GF = galois.GF2 - with pytest.raises(TypeError): - bch.encode(GF.Random(k).tolist()) with pytest.raises(ValueError): bch.encode(GF.Random(k - 1)) @@ -61,19 +59,13 @@ def test_systematic(size): r_poly = (galois.Poly(m) * galois.Poly.Degrees([n-k])) % bch.generator_poly c_truth[-r_poly.coeffs.size:] = -r_poly.coeffs - c = bch.encode(m) - assert type(c) is galois.GF2 - assert np.array_equal(c, c_truth) - - c = bch.encode(m, parity_only=True) - assert type(c) is galois.GF2 - assert np.array_equal(c, c_truth[k:]) - - c = bch.encode(m.view(np.ndarray)) + mm = random_type(m) + c = bch.encode(mm) assert type(c) is galois.GF2 assert np.array_equal(c, c_truth) - c = bch.encode(m.view(np.ndarray), parity_only=True) + mm = random_type(m) + c = bch.encode(mm, parity_only=True) assert type(c) is galois.GF2 assert np.array_equal(c, c_truth[k:]) @@ -87,20 +79,14 @@ def test_non_systematic(size): c_truth = galois.GF2.Zeros(n) c_truth[-c_poly.coeffs.size:] = c_poly.coeffs - c = bch.encode(m) + mm = random_type(m) + c = bch.encode(mm) assert type(c) is galois.GF2 assert np.array_equal(c, c_truth) with pytest.raises(ValueError): c = bch.encode(m, parity_only=True) - c = bch.encode(m.view(np.ndarray)) - assert type(c) is galois.GF2 - assert np.array_equal(c, c_truth) - - with pytest.raises(ValueError): - c = bch.encode(m.view(np.ndarray), parity_only=True) - class Test_n15_k7: n, k = 15, 7 @@ -136,19 +122,13 @@ def test_default(self): [1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1], ]) - C = bch.encode(self.M) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth) - - C = bch.encode(self.M, parity_only=True) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth[:, self.k:]) - - C = bch.encode(self.M.view(np.ndarray)) + MM = random_type(self.M) + C = bch.encode(MM) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth) - C = bch.encode(self.M.view(np.ndarray), parity_only=True) + MM = random_type(self.M) + C = bch.encode(MM, parity_only=True) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth[:, self.k:]) @@ -172,19 +152,13 @@ def test_diff_primitive_poly(self): [1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1], ]) - C = bch.encode(self.M) + MM = random_type(self.M) + C = bch.encode(MM) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth) - C = bch.encode(self.M, parity_only=True) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth[:, self.k:]) - - C = bch.encode(self.M.view(np.ndarray)) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth) - - C = bch.encode(self.M.view(np.ndarray), parity_only=True) + MM = random_type(self.M) + C = bch.encode(MM, parity_only=True) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth[:, self.k:]) @@ -224,19 +198,13 @@ def test_default(self): [0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1], ]) - C = bch.encode(self.M) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth) - - C = bch.encode(self.M, parity_only=True) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) - - C = bch.encode(self.M.view(np.ndarray)) + MM = random_type(self.M) + C = bch.encode(MM) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth) - C = bch.encode(self.M.view(np.ndarray), parity_only=True) + MM = random_type(self.M) + C = bch.encode(MM, parity_only=True) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) @@ -260,19 +228,13 @@ def test_diff_primitive_poly(self): [0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0], ]) - C = bch.encode(self.M) + MM = random_type(self.M) + C = bch.encode(MM) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth) - C = bch.encode(self.M, parity_only=True) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) - - C = bch.encode(self.M.view(np.ndarray)) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth) - - C = bch.encode(self.M.view(np.ndarray), parity_only=True) + MM = random_type(self.M) + C = bch.encode(MM, parity_only=True) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) @@ -311,19 +273,13 @@ def test_default(self): [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1], ]) - C = bch.encode(self.M) + MM = random_type(self.M) + C = bch.encode(MM) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth) - C = bch.encode(self.M, parity_only=True) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth[:, self.k:]) - - C = bch.encode(self.M.view(np.ndarray)) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth) - - C = bch.encode(self.M.view(np.ndarray), parity_only=True) + MM = random_type(self.M) + C = bch.encode(MM, parity_only=True) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth[:, self.k:]) @@ -347,19 +303,13 @@ def test_diff_primitive_poly(self): [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1], ]) - C = bch.encode(self.M) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth) - - C = bch.encode(self.M, parity_only=True) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth[:, self.k:]) - - C = bch.encode(self.M.view(np.ndarray)) + MM = random_type(self.M) + C = bch.encode(MM) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth) - C = bch.encode(self.M.view(np.ndarray), parity_only=True) + MM = random_type(self.M) + C = bch.encode(MM, parity_only=True) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth[:, self.k:]) @@ -399,19 +349,13 @@ def test_default(self): [1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1], ]) - C = bch.encode(self.M) + MM = random_type(self.M) + C = bch.encode(MM) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth) - C = bch.encode(self.M, parity_only=True) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) - - C = bch.encode(self.M.view(np.ndarray)) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth) - - C = bch.encode(self.M.view(np.ndarray), parity_only=True) + MM = random_type(self.M) + C = bch.encode(MM, parity_only=True) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) @@ -435,18 +379,12 @@ def test_diff_primitive_poly(self): [1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1], ]) - C = bch.encode(self.M) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth) - - C = bch.encode(self.M, parity_only=True) - assert type(C) is galois.GF2 - assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) - - C = bch.encode(self.M.view(np.ndarray)) + MM = random_type(self.M) + C = bch.encode(MM) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth) - C = bch.encode(self.M.view(np.ndarray), parity_only=True) + MM = random_type(self.M) + C = bch.encode(MM, parity_only=True) assert type(C) is galois.GF2 assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) diff --git a/tests/codes/test_rs_decode.py b/tests/codes/test_rs_decode.py index b54fbf585..91000bc58 100644 --- a/tests/codes/test_rs_decode.py +++ b/tests/codes/test_rs_decode.py @@ -6,7 +6,7 @@ import galois -from .helper import random_errors +from .helper import random_errors, random_type CODES = [ (15, 13, 1), # GF(2^4) with t=1 @@ -30,8 +30,6 @@ def test_exceptions(): n, k = 15, 11 rs = galois.ReedSolomon(n, k) GF = rs.field - with pytest.raises(TypeError): - rs.decode(GF.Random(n).tolist()) with pytest.raises(ValueError): rs.decode(GF.Random(n + 1)) @@ -39,8 +37,6 @@ def test_exceptions(): n, k = 15, 11 rs = galois.ReedSolomon(n, k, systematic=False) GF = rs.field - with pytest.raises(TypeError): - rs.decode(GF.Random(n).tolist()) with pytest.raises(ValueError): rs.decode(GF.Random(n - 1)) @@ -57,20 +53,13 @@ def test_all_correctable(self, size): E, N_errors = random_errors(GF, N, n, rs.t) R = C + E - DEC_M = rs.decode(R) + RR = random_type(R) + DEC_M = rs.decode(RR) assert type(DEC_M) is GF assert np.array_equal(DEC_M, M) - DEC_M, N_corr = rs.decode(R, errors=True) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M, M) - assert np.array_equal(N_corr, N_errors) - - DEC_M = rs.decode(R.view(np.ndarray)) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M, M) - - DEC_M, N_corr = rs.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = rs.decode(RR, errors=True) assert type(DEC_M) is GF assert np.array_equal(DEC_M, M) assert np.array_equal(N_corr, N_errors) @@ -88,20 +77,13 @@ def test_some_uncorrectable(self, size): corr_idxs = np.where(N_errors <= rs.t)[0] - DEC_M = rs.decode(R) + RR = random_type(R) + DEC_M = rs.decode(RR) assert type(DEC_M) is GF assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - DEC_M, N_corr = rs.decode(R, errors=True) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) - - DEC_M = rs.decode(R.view(np.ndarray)) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - - DEC_M, N_corr = rs.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = rs.decode(RR, errors=True) assert type(DEC_M) is GF assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) @@ -123,20 +105,13 @@ def test_all_correctable(self, size): E, N_errors = random_errors(GF, N, ns, rs.t) R = C + E - DEC_M = rs.decode(R) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M, M) - - DEC_M, N_corr = rs.decode(R, errors=True) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M, M) - assert np.array_equal(N_corr, N_errors) - - DEC_M = rs.decode(R.view(np.ndarray)) + RR = random_type(R) + DEC_M = rs.decode(RR) assert type(DEC_M) is GF assert np.array_equal(DEC_M, M) - DEC_M, N_corr = rs.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = rs.decode(RR, errors=True) assert type(DEC_M) is GF assert np.array_equal(DEC_M, M) assert np.array_equal(N_corr, N_errors) @@ -158,20 +133,13 @@ def test_some_uncorrectable(self, size): corr_idxs = np.where(N_errors <= rs.t)[0] - DEC_M = rs.decode(R) + RR = random_type(R) + DEC_M = rs.decode(RR) assert type(DEC_M) is GF assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - DEC_M, N_corr = rs.decode(R, errors=True) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) - - DEC_M = rs.decode(R.view(np.ndarray)) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - - DEC_M, N_corr = rs.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = rs.decode(RR, errors=True) assert type(DEC_M) is GF assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) @@ -189,20 +157,13 @@ def test_all_correctable(self, size): E, N_errors = random_errors(GF, N, n, rs.t) R = C + E - DEC_M = rs.decode(R) + RR = random_type(R) + DEC_M = rs.decode(RR) assert type(DEC_M) is GF assert np.array_equal(DEC_M, M) - DEC_M, N_corr = rs.decode(R, errors=True) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M, M) - assert np.array_equal(N_corr, N_errors) - - DEC_M = rs.decode(R.view(np.ndarray)) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M, M) - - DEC_M, N_corr = rs.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = rs.decode(RR, errors=True) assert type(DEC_M) is GF assert np.array_equal(DEC_M, M) assert np.array_equal(N_corr, N_errors) @@ -220,20 +181,13 @@ def test_some_uncorrectable(self, size): corr_idxs = np.where(N_errors <= rs.t)[0] - DEC_M = rs.decode(R) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - - DEC_M, N_corr = rs.decode(R, errors=True) - assert type(DEC_M) is GF - assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) - - DEC_M = rs.decode(R.view(np.ndarray)) + RR = random_type(R) + DEC_M = rs.decode(RR) assert type(DEC_M) is GF assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) - DEC_M, N_corr = rs.decode(R.view(np.ndarray), errors=True) + RR = random_type(R) + DEC_M, N_corr = rs.decode(RR, errors=True) assert type(DEC_M) is GF assert np.array_equal(DEC_M[corr_idxs,:], M[corr_idxs,:]) assert np.array_equal(N_corr[corr_idxs], N_errors[corr_idxs]) diff --git a/tests/codes/test_rs_detect.py b/tests/codes/test_rs_detect.py index 2f23fe6c1..40f6671db 100644 --- a/tests/codes/test_rs_detect.py +++ b/tests/codes/test_rs_detect.py @@ -6,7 +6,7 @@ import galois -from .helper import random_errors +from .helper import random_errors, random_type CODES = [ (15, 13), # GF(2^4) with t=1 @@ -30,8 +30,6 @@ def test_exceptions(): n, k = 15, 11 rs = galois.ReedSolomon(n, k) GF = rs.field - with pytest.raises(TypeError): - rs.detect(GF.Random(n).tolist()) with pytest.raises(ValueError): rs.detect(GF.Random(n + 1)) @@ -39,8 +37,6 @@ def test_exceptions(): n, k = 15, 11 rs = galois.ReedSolomon(n, k, systematic=False) GF = rs.field - with pytest.raises(TypeError): - rs.detect(GF.Random(n).tolist()) with pytest.raises(ValueError): rs.detect(GF.Random(n - 1)) @@ -56,7 +52,8 @@ def test_no_errors(self, size): C = rs.encode(M) R = C - detected = rs.detect(R) + RR = random_type(R) + detected = rs.detect(RR) assert type(detected) is np.ndarray assert np.all(detected == False) @@ -74,7 +71,8 @@ def test_all_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= rs.d - 1))[0] - detected = rs.detect(R) + RR = random_type(R) + detected = rs.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) @@ -93,7 +91,8 @@ def test_some_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= rs.d - 1))[0] - detected = rs.detect(R) + RR = random_type(R) + detected = rs.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) @@ -114,7 +113,8 @@ def test_no_errors(self, size): C = rs.encode(M) R = C - detected = rs.detect(R) + RR = random_type(R) + detected = rs.detect(RR) assert type(detected) is np.ndarray assert np.all(detected == False) @@ -136,7 +136,8 @@ def test_all_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= rs.d - 1))[0] - detected = rs.detect(R) + RR = random_type(R) + detected = rs.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) @@ -159,7 +160,8 @@ def test_some_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= rs.d - 1))[0] - detected = rs.detect(R) + RR = random_type(R) + detected = rs.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) @@ -176,7 +178,8 @@ def test_no_errors(self, size): C = rs.encode(M) R = C - detected = rs.detect(R) + RR = random_type(R) + detected = rs.detect(RR) assert type(detected) is np.ndarray assert np.all(detected == False) @@ -194,7 +197,8 @@ def test_all_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= rs.d - 1))[0] - detected = rs.detect(R) + RR = random_type(R) + detected = rs.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) @@ -213,7 +217,8 @@ def test_some_detectable(self, size): corr_idxs = np.where(N_errors == 0)[0] det_idxs = np.where(np.logical_and(0 < N_errors, N_errors <= rs.d - 1))[0] - detected = rs.detect(R) + RR = random_type(R) + detected = rs.detect(RR) assert type(detected) is np.ndarray assert np.all(detected[corr_idxs] == False) assert np.all(detected[det_idxs] == True) diff --git a/tests/codes/test_rs_encode.py b/tests/codes/test_rs_encode.py index 4e700cc1c..68eca4bb5 100644 --- a/tests/codes/test_rs_encode.py +++ b/tests/codes/test_rs_encode.py @@ -12,6 +12,8 @@ import galois +from .helper import random_type + CODES = [ (15, 13, 1), # GF(2^4) with t=1 (15, 11, 2), # GF(2^4) with t=2 @@ -34,8 +36,6 @@ def test_exceptions(): n, k = 15, 11 rs = galois.ReedSolomon(n, k) GF = rs.field - with pytest.raises(TypeError): - rs.encode(GF.Random(k).tolist()) with pytest.raises(ValueError): rs.encode(GF.Random(k + 1)) @@ -43,8 +43,6 @@ def test_exceptions(): n, k = 15, 11 rs = galois.ReedSolomon(n, k, systematic=False) GF = rs.field - with pytest.raises(TypeError): - rs.encode(GF.Random(k).tolist()) with pytest.raises(ValueError): rs.encode(GF.Random(k - 1)) @@ -60,19 +58,13 @@ def test_systematic(size): r_poly = (galois.Poly(m) * galois.Poly.Identity(GF)**(n-k)) % rs.generator_poly c_truth[-r_poly.coeffs.size:] = -r_poly.coeffs - c = rs.encode(m) + mm = random_type(m) + c = rs.encode(mm) assert type(c) is GF assert np.array_equal(c, c_truth) - c = rs.encode(m, parity_only=True) - assert type(c) is GF - assert np.array_equal(c, c_truth[k:]) - - c = rs.encode(m.view(np.ndarray)) - assert type(c) is GF - assert np.array_equal(c, c_truth) - - c = rs.encode(m.view(np.ndarray), parity_only=True) + mm = random_type(m) + c = rs.encode(mm, parity_only=True) assert type(c) is GF assert np.array_equal(c, c_truth[k:]) @@ -87,20 +79,14 @@ def test_non_systematic(size): c_truth = GF.Zeros(n) c_truth[-c_poly.coeffs.size:] = c_poly.coeffs - c = rs.encode(m) + mm = random_type(m) + c = rs.encode(mm) assert type(c) is GF assert np.array_equal(c, c_truth) with pytest.raises(ValueError): c = rs.encode(m, parity_only=True) - c = rs.encode(m.view(np.ndarray)) - assert type(c) is GF - assert np.array_equal(c, c_truth) - - with pytest.raises(ValueError): - c = rs.encode(m.view(np.ndarray), parity_only=True) - class Test_n15_k9: n, k = 15, 9 @@ -137,19 +123,13 @@ def test_default(self): [ 9, 3, 4, 7, 3, 7, 6, 11, 15, 13, 11, 7, 11, 3, 13], ]) - C = rs.encode(M) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, self.k:]) - - C = rs.encode(M.view(np.ndarray)) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, self.k:]) @@ -174,19 +154,13 @@ def test_diff_primitive_poly(self): [ 9, 3, 4, 7, 3, 7, 6, 11, 15, 4, 1, 7, 1, 1, 14], ]) - C = rs.encode(M) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, self.k:]) - - C = rs.encode(M.view(np.ndarray)) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, self.k:]) @@ -211,19 +185,13 @@ def test_diff_c(self): [ 9, 3, 4, 7, 3, 7, 6, 11, 15, 7, 14, 11, 8, 0, 12], ]) - C = rs.encode(M) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, self.k:]) - - C = rs.encode(M.view(np.ndarray)) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, self.k:]) @@ -267,19 +235,13 @@ def test_default(self): [14, 11, 15, 12, 8, 8, 14, 3, 8, 4, 14], ]) - C = rs.encode(M) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) - - C = rs.encode(M.view(np.ndarray)) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) @@ -307,19 +269,13 @@ def test_diff_primitive_poly(self): [14, 11, 15, 12, 8, 1, 1, 10, 8, 9, 6], ]) - C = rs.encode(M) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) - - C = rs.encode(M.view(np.ndarray)) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) @@ -347,19 +303,13 @@ def test_diff_c(self): [14, 11, 15, 12, 8, 11, 3, 4, 9, 9, 7], ]) - C = rs.encode(M) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) - - C = rs.encode(M.view(np.ndarray)) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) @@ -399,19 +349,13 @@ def test_default(self): [31, 24, 26, 21, 15, 30, 19, 14, 17, 7, 13, 20, 13, 7, 14, 1, 17, 20, 28, 13, 13, 30, 19, 16, 2, 15, 2, 15, 6, 2, 2], ]) - C = rs.encode(M) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, self.k:]) - - C = rs.encode(M.view(np.ndarray)) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, self.k:]) @@ -436,19 +380,13 @@ def test_diff_primitive_poly(self): [31, 24, 26, 21, 15, 30, 19, 14, 17, 7, 13, 20, 13, 7, 14, 1, 17, 20, 28, 13, 13, 30, 19, 26, 31, 27, 7, 17, 26, 21, 16], ]) - C = rs.encode(M) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, self.k:]) - - C = rs.encode(M.view(np.ndarray)) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, self.k:]) @@ -473,19 +411,13 @@ def test_diff_c(self): [31, 24, 26, 21, 15, 30, 19, 14, 17, 7, 13, 20, 13, 7, 14, 1, 17, 20, 28, 13, 13, 30, 19, 27, 3, 23, 17, 5, 8, 4, 23], ]) - C = rs.encode(M) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, self.k:]) - - C = rs.encode(M.view(np.ndarray)) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, self.k:]) @@ -529,19 +461,13 @@ def test_default(self): [17, 6, 17, 21, 15, 10, 31, 8, 27, 27, 21, 9, 15, 12, 24, 30, 8, 7, 12, 11, 19], ]) - C = rs.encode(M) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) - - C = rs.encode(M.view(np.ndarray)) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) @@ -569,19 +495,13 @@ def test_diff_primitive_poly(self): [17, 6, 17, 21, 15, 10, 31, 8, 27, 27, 21, 9, 15, 18, 30, 17, 6, 17, 5, 20, 15], ]) - C = rs.encode(M) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) - - C = rs.encode(M.view(np.ndarray)) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) @@ -609,18 +529,12 @@ def test_diff_c(self): [17, 6, 17, 21, 15, 10, 31, 8, 27, 27, 21, 9, 15, 8, 22, 8, 24, 11, 24, 23, 21], ]) - C = rs.encode(M) - assert type(C) is GF - assert np.array_equal(C, C_truth) - - C = rs.encode(M, parity_only=True) - assert type(C) is GF - assert np.array_equal(C, C_truth[:, -(self.n - self.k):]) - - C = rs.encode(M.view(np.ndarray)) + MM = random_type(M) + C = rs.encode(MM) assert type(C) is GF assert np.array_equal(C, C_truth) - C = rs.encode(M.view(np.ndarray), parity_only=True) + MM = random_type(M) + C = rs.encode(MM, parity_only=True) assert type(C) is GF assert np.array_equal(C, C_truth[:, -(self.n - self.k):])