auto_grader.py 1.84 KB
Newer Older
Sanjay Krishnan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
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()