import random from encoding import * def test_load(): data = load_orig_file('data.txt') try: assert(sum(data) == 1778744) except AssertionError: print('TODO 1. Failure check your load_orig_file function') def test_encoding(): data = load_orig_file('data.txt') encoded = delta_encoding(data) try: assert(sum(encoded) == data[-1]) assert(sum(encoded) == 26) assert(len(data) == len(encoded)) except AssertionError: print('TODO 2. Failure check your delta_encoding function') def test_shift(): data = load_orig_file('data.txt') encoded = delta_encoding(data) N = len(data) try: assert(sum(shift(data, 10)) == N*10 + sum(data)) assert(all([d >=0 for d in shift(encoded,4)])) except AssertionError: print('TODO 3. Failure check your shift function') def test_decoding(): data = load_orig_file('data.txt') encoded = delta_encoding(data) sencoded = shift(encoded ,4) data_p = delta_decoding(unshift(sencoded,4)) try: assert(data == data_p) except AssertionError: print('TODO 5. Cannot recover data with delta_decoding') def generate_file(size, seed): FILE_NAME = 'data.gen.txt' f = open(FILE_NAME,'w') initial = seed for i in range(size): f.write(str(initial) + '\n') initial += random.randint(-4, 4) def generate_random_tests(): SIZES = (1,1000,16,99) SEEDS = (240,-3, 9, 1) cnt = 0 for trials in range(10): generate_file(random.choice(SIZES), random.choice(SEEDS)) data = load_orig_file('data.gen.txt') encoded = delta_encoding(data) sencoded = shift(encoded ,4) write_encoding(sencoded, 'data_out.txt') loaded = unshift(read_encoding('data_out.txt'),4) decoded = delta_decoding(loaded) cnt += (decoded == data) try: assert(cnt == 10) except AssertionError: print('Failed Random Tests', str(10-cnt), 'out of 10') test_load() test_encoding() test_shift() test_decoding() generate_random_tests()