ba-thesis/sw/python/utility/test/test_utility.py

49 lines
1.4 KiB
Python

import unittest
import numpy as np
from utility import noise, codes
# TODO: Rewrite tests for new SNR calculation
class NoiseAmpFromSNRTestCase(unittest.TestCase):
"""Test case for noise amplitude calculation."""
def test_get_noise_amp_from_SNR(self):
SNR1 = 0
SNR2 = 3
SNR3 = 20
SNR4 = -20
var1 = noise.get_noise_variance_from_SNR(SNR1, n=8, k=8)
var2 = noise.get_noise_variance_from_SNR(SNR2, n=8, k=8)
var3 = noise.get_noise_variance_from_SNR(SNR3, n=8, k=8)
var4 = noise.get_noise_variance_from_SNR(SNR4, n=8, k=8)
self.assertEqual(var1, 1 * 0.5)
self.assertAlmostEqual(var2, 0.5 * 0.5, places=2)
self.assertEqual(var3, 0.01 * 0.5)
self.assertEqual(var4, 100 * 0.5)
class CodesTestCase(unittest.TestCase):
"""Tests relating to the 'codes' utilities."""
def test_get_systematic_H(self):
# Hamming(7,4) code
G = np.array([[1, 0, 0, 0, 0, 1, 1],
[0, 1, 0, 0, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1]])
expected_H = np.array([[0, 1, 1, 1, 1, 0, 0],
[1, 0, 1, 1, 0, 1, 0],
[1, 1, 0, 1, 0, 0, 1]])
H = codes.get_systematic_H(G)
self.assertEqual(np.array_equal(expected_H, H), True)
if __name__ == '__main__':
unittest.main()