def test_str(): Key.set_random_seed(3331) Shuffle.set_random_seed(3332) key = Key.create_random_key(8) shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_KEEP_SAME) assert shuffle.__str__() == "0->0 1->1 2->2 3->3 4->4 5->5 6->6 7->7" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->0 1->4 2->2 3->6 4->7 5->3 6->1 7->5"
def test_create_shuffle_keep_same(): Key.set_random_seed(1111) Shuffle.set_random_seed(1112) # Empty shuffle. key = Key() assert key.__str__() == "" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_KEEP_SAME) assert shuffle.__str__() == "" # Non-empty shuffle. key = Key.create_random_key(8) assert key.__str__() == "00101100" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_KEEP_SAME) assert shuffle.__str__() == "0->0 1->1 2->2 3->3 4->4 5->5 6->6 7->7" for index in range(key.get_size()): assert shuffle.get_bit(key, index) == key.get_bit(index)
def test_create_shuffle_random(): Key.set_random_seed(1111) Shuffle.set_random_seed(1112) # Empty shuffle. key = Key() assert key.__str__() == "" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "" # Non-empty shuffle. key = Key.create_random_key(16) assert key.__str__() == "0010110001010010" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == ("0->2 1->9 2->8 3->7 4->3 5->12 6->1 7->5 8->13 " "9->6 10->0 11->14 12->10 13->4 14->11 15->15") key = Key() assert key.__str__() == ""
def test_str(): Key.set_random_seed(55511) Shuffle.set_random_seed(55522) key = Key.create_random_key(4) assert key.__str__() == "1010" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->1 1->2 2->3 3->0" blocks = Block.create_covering_blocks(key, shuffle, 5) assert len(blocks) == 1 assert blocks[0].__str__() == "0101"
def test_get_key_index(): Key.set_random_seed(77711) Shuffle.set_random_seed(77712) key = Key.create_random_key(6) assert key.__str__() == "110101" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->0 1->5 2->1 3->4 4->2 5->3" blocks = Block.create_covering_blocks(key, shuffle, 3) block = blocks[0] assert block.get_key_index(1) == 5
def test_calculate_parity(): Key.set_random_seed(8881) Shuffle.set_random_seed(8882) key = Key.create_random_key(10) assert key.__repr__() == "Key: 1011111100" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__repr__() == "Shuffle: 0->1 1->4 2->5 3->9 4->6 5->0 6->7 7->2 8->3 9->8" assert shuffle.__str__() == "0->1 1->4 2->5 3->9 4->6 5->0 6->7 7->2 8->3 9->8" assert shuffle.calculate_parity(key, 0, 10) == 1 assert shuffle.calculate_parity(key, 4, 8) == 0 assert shuffle.calculate_parity(key, 1, 2) == 1
def test_get_start_index(): Key.set_random_seed(55555) Shuffle.set_random_seed(55566) key = Key.create_random_key(6) assert key.__str__() == "001100" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->2 1->1 2->0 3->5 4->3 5->4" blocks = Block.create_covering_blocks(key, shuffle, 3) assert len(blocks) == 2 assert blocks[0].get_start_index() == 0 assert blocks[1].get_start_index() == 3
def test_get_key_indexes(): Key.set_random_seed(55593) Shuffle.set_random_seed(55594) key = Key.create_random_key(6) assert key.__str__() == "010011" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->4 1->5 2->2 3->3 4->0 5->1" blocks = Block.create_covering_blocks(key, shuffle, 3) assert len(blocks) == 2 assert blocks[0].get_key_indexes() == [4, 5, 2] assert blocks[1].get_key_indexes() == [3, 0, 1]
def test_get_shuffle(): Key.set_random_seed(55591) Shuffle.set_random_seed(55592) key = Key.create_random_key(6) assert key.__str__() == "001000" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->5 1->3 2->4 3->0 4->1 5->2" blocks = Block.create_covering_blocks(key, shuffle, 3) assert len(blocks) == 2 assert blocks[0].get_shuffle() == shuffle assert blocks[1].get_shuffle() == shuffle
def test_get_end_index(): Key.set_random_seed(55577) Shuffle.set_random_seed(55588) key = Key.create_random_key(6) assert key.__str__() == "100000" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->4 1->0 2->5 3->2 4->3 5->1" blocks = Block.create_covering_blocks(key, shuffle, 3) assert len(blocks) == 2 assert blocks[0].get_end_index() == 3 assert blocks[1].get_end_index() == 6
def test_get_bit(): Key.set_random_seed(5551) Shuffle.set_random_seed(5552) key = Key.create_random_key(13) assert key.__str__() == "1011010010010" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == ("0->5 1->9 2->3 3->12 4->10 5->6 6->11 7->7 8->0 9->4 " "10->1 11->2 12->8") assert shuffle.get_bit(key, 0) == 1 # Shuffle bit 0 -> Key bit 5 -> Bit value 1 assert shuffle.get_bit(key, 1) == 0 # Shuffle bit 1 -> Key bit 9 -> Bit value 0 assert shuffle.get_bit(key, 2) == 1 # Shuffle bit 2 -> Key bit 3 -> Bit value 1 assert shuffle.get_bit(key, 12) == 1 # Shuffle bit 12 -> Key bit 8 -> Bit value 1
def test_lt(): Key.set_random_seed(55533) Shuffle.set_random_seed(55544) key = Key.create_random_key(6) assert key.__str__() == "001101" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->3 1->2 2->4 3->5 4->1 5->0" blocks = Block.create_covering_blocks(key, shuffle, 3) assert len(blocks) == 2 assert (blocks[0] < blocks[1]) or (blocks[1] < blocks[0]) # pylint:disable=comparison-with-itself assert not blocks[0] < blocks[0]
def test_get_current_parity(): # Even parity block. Key.set_random_seed(6661) Shuffle.set_random_seed(6662) key = Key.create_random_key(10) assert key.__str__() == "0111101111" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__( ) == "0->1 1->6 2->7 3->8 4->4 5->2 6->0 7->9 8->3 9->5" blocks = Block.create_covering_blocks(key, shuffle, 10) assert len(blocks) == 1 block = blocks[0] assert block.__str__() == "1111110110" assert block.get_current_parity() == 0 # Odd parity block. key = Key.create_random_key(12) assert key.__str__() == "010100111101" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__( ) == "0->7 1->9 2->11 3->2 4->8 5->1 6->6 7->5 8->0 9->10 10->3 11->4" blocks = Block.create_covering_blocks(key, shuffle, 12) assert len(blocks) == 1 block = blocks[0] assert block.__str__() == "111011100010" assert block.get_current_parity() == 1 # Split block into sub-blocks. left_sub_block = block.create_left_sub_block() right_sub_block = block.create_right_sub_block() # Odd parity sub-block. assert left_sub_block.__str__() == "111011" assert left_sub_block.get_current_parity() == 1 # Even parity sub-block. assert right_sub_block.__str__() == "100010" assert right_sub_block.get_current_parity() == 0
def test_flip_bit(): Key.set_random_seed(77713) Shuffle.set_random_seed(77714) key = Key.create_random_key(6) assert key.__str__() == "111100" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->1 1->2 2->4 3->5 4->0 5->3" blocks = Block.create_covering_blocks(key, shuffle, 3) block = blocks[1] assert block.__str__() == "011" block.flip_bit(4) assert key.__str__() == "011100" assert block.__str__() == "001"
def test_get_and_set_correct_parity(): Key.set_random_seed(6663) Shuffle.set_random_seed(6664) key = Key.create_random_key(10) assert key.__str__() == "1010111100" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__( ) == "0->8 1->5 2->7 3->0 4->2 5->4 6->1 7->3 8->9 9->6" blocks = Block.create_covering_blocks(key, shuffle, 10) assert len(blocks) == 1 block = blocks[0] assert block.__str__() == "0111110001" assert block.get_correct_parity() is None block.set_correct_parity(0) assert block.get_correct_parity() == 0 block.set_correct_parity(1) assert block.get_correct_parity() == 1
def test_flip_parity(): Key.set_random_seed(77715) Shuffle.set_random_seed(77716) key = Key.create_random_key(6) assert key.__str__() == "001010" shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->2 1->5 2->0 3->4 4->1 5->3" blocks = Block.create_covering_blocks(key, shuffle, 3) block = blocks[1] assert block.__str__() == "100" assert block.get_error_parity() == Block.ERRORS_UNKNOWN block.set_correct_parity(0) assert block.get_error_parity() == Block.ERRORS_ODD block.flip_bit(4) assert key.__str__() == "011010" assert block.__str__() == "110" assert block.get_error_parity() == Block.ERRORS_ODD block.flip_parity() assert block.get_error_parity() == Block.ERRORS_EVEN
def test_set_random_seed(): Key.set_random_seed(4441) Shuffle.set_random_seed(4442) key = Key.create_random_key(8) shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM) assert shuffle.__str__() == "0->1 1->3 2->2 3->7 4->5 5->6 6->4 7->0"