<s> import unittest <EOL> import random <EOL> from groundstation . ue_encoder import UnambiguousEncoder <EOL> class TestUnambiguousEncapsulation ( unittest . TestCase ) : <EOL> test_bytes = [ <NUM_LIT> , <NUM_LIT> ] <EOL> test_message = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> test_header = [ <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> def test_successfully_encoded ( self ) : <EOL> encoder = UnambiguousEncoder ( ) <EOL> message = encoder . encode ( "<STR_LIT:message>" , self . test_bytes ) <EOL> header = encoder . encode ( "<STR_LIT>" , self . test_bytes ) <EOL> self . assertEqual ( message , self . test_message ) <EOL> self . assertEqual ( header , self . test_header ) <EOL> def test_successfully_decoded ( self ) : <EOL> encoder = UnambiguousEncoder ( ) <EOL> message = encoder . decode ( "<STR_LIT:message>" , self . test_message ) <EOL> header = encoder . decode ( "<STR_LIT>" , self . test_header ) <EOL> self . assertEqual ( message , self . test_bytes ) <EOL> self . assertEqual ( header , self . test_bytes ) <EOL> @ staticmethod <EOL> def flip_n_bits ( n , msg ) : <EOL> out = [ ] <EOL> for byte in msg : <EOL> bits = list ( range ( <NUM_LIT:8> ) ) <EOL> for _ in range ( n ) : <EOL> bit = bits . pop ( random . randint ( <NUM_LIT:0> , len ( bits ) - <NUM_LIT:1> ) ) <EOL> byte = byte ^ <NUM_LIT:1> << bit <EOL> out . append ( byte ) <EOL> return out <EOL> def test_1_flipped_bit ( self ) : <EOL> encoder = UnambiguousEncoder ( ) <EOL> flipped_message = self . flip_n_bits ( <NUM_LIT:1> , self . test_message ) <EOL> message = encoder . decode ( "<STR_LIT:message>" , flipped_message ) <EOL> self . assertEqual ( message , self . test_bytes ) <EOL> def test_decode_weird_lengths ( self ) : <EOL> encoder = UnambiguousEncoder ( ) <EOL> message = encoder . decode ( "<STR_LIT:message>" , self . test_message + [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> test_bytes = self . test_bytes + [ <NUM_LIT> ] <EOL> self . assertEqual ( message , test_bytes ) </s>
