def test_very_large_k(self): code = sec.SingleEditCode(512) for i in range(1000): x = sec.QaryString(4, np.random.randint(4, 1024)) x_enc, n, N, l = code.encode(x) x_enc_m, mtype, pos, symbol = x_enc.mutate(mtype="insert") x_pred = code.decode(x_enc_m, n, N, l) self.assertTrue(x_pred == x)
class TestSumBalancedCode(unittest.TestCase): k = 32 code = sec.SumBalancedCode(k) for i in range(1000): s = sec.QaryString(q=4, val=np.random.randint(low=0, high=4, size=100)) x, l = code.encode(s) assert sec.is_k_sum_balanced(x.val, k, q=4) s_pred = code.decode(x, l) assert s == s_pred
def test1_delete_random_short(self): for i in range(1000): y = sec.QaryString(2, np.random.randint(0, 2, 10)) code = sec.SVTCode() yp, _, pos, symbol = y.mutate(mtype="delete") u = np.random.randint(low=1, high=pos + 2) y_pred = code.decode_deletion(yp, y.syndrome, u, 10, symbol, verbose=False) self.assertTrue(y_pred == y)
def test1_delete_fixed_long(self): y = sec.QaryString(q=2, val=[0, 1] * 1000) symbol = 0 pos = 4 yp = y._delete(pos, idx_of_pos=0) code = sec.SVTCode() y_pred = code.decode_deletion(yp, y.syndrome, pos + 1, 3, symbol, verbose=False) self.assertEqual(y_pred, y)
def test1_insert_fixed_short(self): y = sec.QaryString(q=2, val=[0, 1, 0, 1, 0, 1, 0, 1]) symbol = 0 pos = 4 yp = y._insert(pos, symbol, idx_of_pos=0) code = sec.SVTCode() y_pred = code.decode_insertion(yp, y.syndrome, pos + 1, 3, symbol, verbose=False) self.assertEqual(y_pred, y)
# -*- coding: utf-8 -*- """ Created on Mon Mar 23 23:31:11 2020 @author: Daniel Tan """ import numpy as np import single_edit_code as sec # generate random quaternary string of length 40 x = sec.QaryString(4, np.random.randint(0, high=3, size=160)) # print quaternary string to visualize print(x) # the value of k determines the redundancy of the code, # it should be roughly 36*log_4(codeword_length) # Since codeword_length might not be known a priori, we can set to # roughly 36*log_4(message_length) which is 36*log_4(160) ~ 132 in our case. # It is possible to use lower values of k, but then the redundancy is higher. Note # that higher k = more precomputation and slower encoding/decoding. # this command constructs the code, this does some precomputation such # as building index for forbidden words, that reduces the encoding time later. code = sec.SingleEditCode(k=132) # perform the encoding, returning the encoded quaternary string string + parameters needed during decoding. x_enc, n, N, l = code.encode(x) # print quaternary string to visualize print('x_enc', x_enc)