def test_encode_binary(): assert encode(0, 0, "01") == [] # zero length assert encode(1, 0, "01") == [] # zero length assert encode(0, 1, "01") == ['0'] # length match assert encode(1, 1, "01") == ['1'] # length match assert encode(0, 2, "01") == ['0', '0'] # leading zeroes assert encode(1, 2, "01") == ['0', '1'] # leading zeroes assert encode(2, 2, "01") == ['1', '0'] # length match assert encode(3, 2, "01") == ['1', '1'] # length match assert encode(4, 2, "01") == ['0', '0'] # overflow truncated
def test_encode_errors(): with pytest.raises(ValueError): # ValueError: alphabet must be at least 2 elements long encode(1, 1, "0") with pytest.raises(ValueError): # ValueError: length must be >= 0 encode(1, -1, "0") with pytest.raises(ValueError): # ValueError: negative values are not supported encode(-1, 1, "0")
def test_encode_hex(): assert encode(31, 2, "0123456789ABCDEF") == ['1', 'F']
def test_encode_decimal(): assert encode(123, 3, "0123456789") == ['1', '2', '3'] # length match assert encode(456, 4, "0123456789") == ['0', '4', '5', '6'] # leading zeroes assert encode(7890, 3, "0123456789") == ['8', '9', '0'] # overflow truncated
def test_encode_special(): # equivalent to binary, but we see the code is rather flexible assert encode(0, 2, ".+") == ['.', '.'] assert encode(1, 2, ".+") == ['.', '+'] assert encode(2, 2, ".+") == ['+', '.'] assert encode(3, 2, ".+") == ['+', '+']
def test_encode_length(): length = 42 assert len(encode(12345, length)) == length