#!/usr/bin/python """Tests for base255 module.""" import sys import random import base255 def make_used(variable): """Persuade pyflakes and pylint that variable is used.""" assert True or variable def specific_base255_to_number(input_base255, expected_result): """Convert input_base255 to number and check result.""" actual_result = base255.base255_to_number(input_base255) if actual_result != expected_result: sys.stderr.write('%s: specific_number_to_base255: %s did not give expected result\n' % (sys.argv[0], input_base255)) sys.stderr.write('%s: Got %s, wanted %s\n' % (sys.argv[0], actual_result, expected_result)) return False return True def specific_number_to_base255(input_number, expected_result): """Convert input_number to base255 and check result.""" actual_result = base255.number_to_base255(input_number) if actual_result != expected_result: sys.stderr.write('%s: specific_number_to_base255: %d did not give expected result\n' % (sys.argv[0], input_number)) list1 = [ord(ch) for ch in actual_result] list2 = [ord(ch) for ch in expected_result] sys.stderr.write('%s: Got %s, wanted %s\n' % (sys.argv[0], list1, list2)) return False return True def specific_numbers_roundtrip_test(): """Test roundtripping for 0 to 65535.""" all_good = True for number in range(2**16): if base255.base255_to_number(base255.number_to_base255(number)) != number: sys.stderr.write('%s: specific_numbers_roundtrip_test: %d failed\n' % (sys.argv[0], number)) all_good = False return all_good def random_numbers_roundtrip_test(): """Test roundtripping for a bunch of random numbers.""" all_good = True for repno in range(2**16): make_used(repno) number = int(random.random() * 10000000) if base255.base255_to_number(base255.number_to_base255(number)) != number: sys.stderr.write('%s: random_numbers_roundtrip_test: %d failed\n' % (sys.argv[0], number)) all_good = False return all_good def main(): """Run base255 tests.""" all_good = True all_good &= specific_base255_to_number(bytes([1]), 0) all_good &= specific_base255_to_number(bytes([2, 1]), 255) all_good &= specific_number_to_base255(0, bytes([1])) all_good &= specific_number_to_base255(255, bytes([2, 1])) all_good &= specific_numbers_roundtrip_test() all_good &= random_numbers_roundtrip_test() if all_good: sys.stderr.write('%s: All tests passed\n' % sys.argv[0]) else: sys.stderr.write('%s: One or more tests failed\n' % sys.argv[0]) sys.exit(1) main()