ba-thesis/sw/test/test_soft_decision.py

60 lines
2.8 KiB
Python

import unittest
import numpy as np
from decoders import naive_soft_decision
class CodewordGenerationTestCase(unittest.TestCase):
def test_codeword_generation(self):
"""Test case for data word and code word generation."""
# Hamming(7,4) code
G = np.array([[1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 0, 0],
[0, 1, 0, 1, 0, 1, 0],
[1, 1, 0, 1, 0, 0, 1]])
H = np.array([[1, 0, 1, 0, 1, 0, 1],
[0, 1, 1, 0, 0, 1, 1],
[0, 0, 0, 1, 1, 1, 1]])
R = np.array([[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1]])
decoder = naive_soft_decision.SoftDecisionDecoder(G, H, R)
expected_datawords = np.array([[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 1, 0, 0],
[0, 1, 0, 1],
[0, 1, 1, 0],
[0, 1, 1, 1],
[1, 0, 0, 0],
[1, 0, 0, 1],
[1, 0, 1, 0],
[1, 0, 1, 1],
[1, 1, 0, 0],
[1, 1, 0, 1],
[1, 1, 1, 0],
[1, 1, 1, 1]])
expected_codewords = np.array([[0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 1, 0, 0, 1],
[0, 1, 0, 1, 0, 1, 0],
[1, 0, 0, 0, 0, 1, 1],
[1, 0, 0, 1, 1, 0, 0],
[0, 1, 0, 0, 1, 0, 1],
[1, 1, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 0],
[0, 1, 1, 0, 0, 1, 1],
[0, 1, 1, 1, 1, 0, 0],
[1, 0, 1, 0, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1]])
self.assertEqual(np.array_equal(decoder._datawords, expected_datawords), True)
self.assertEqual(np.array_equal(decoder._codewords, expected_codewords), True)