auto_grader.py 1.84 KB
Newer Older
1
import random
Sanjay Krishnan committed
2
from encoding import *
3

Sanjay Krishnan committed
4 5
def test_load():
	data = load_orig_file('data.txt')
6 7

	try:
Sanjay Krishnan committed
8 9 10
		assert(sum(data) == 1778744)
	except AssertionError:
		print('TODO 1. Failure check your load_orig_file function')
11

Sanjay Krishnan committed
12 13 14
def test_encoding():
	data = load_orig_file('data.txt')
	encoded = delta_encoding(data)
15 16

	try:
Sanjay Krishnan committed
17 18 19 20 21
		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')
22

Sanjay Krishnan committed
23 24 25 26
def test_shift():
	data = load_orig_file('data.txt')
	encoded = delta_encoding(data)
	N = len(data)
27 28

	try:
Sanjay Krishnan committed
29 30 31 32
		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')
33

Sanjay Krishnan committed
34 35 36 37 38
def test_decoding():
	data = load_orig_file('data.txt')
	encoded = delta_encoding(data)
	sencoded = shift(encoded ,4)
	data_p = delta_decoding(unshift(sencoded,4))
39 40

	try:
Sanjay Krishnan committed
41 42 43
		assert(data == data_p)
	except AssertionError:
		print('TODO 5. Cannot recover data with delta_decoding')
44

Sanjay Krishnan committed
45 46
def generate_file(size, seed):
	FILE_NAME = 'data.gen.txt'
47

Sanjay Krishnan committed
48
	f = open(FILE_NAME,'w')
49

Sanjay Krishnan committed
50 51 52 53
	initial = seed
	for i in range(size):
		f.write(str(initial) + '\n')
		initial += random.randint(-4, 4)
54

Sanjay Krishnan committed
55 56 57
def generate_random_tests():
	SIZES = (1,1000,16,99)
	SEEDS = (240,-3, 9, 1)
58

Sanjay Krishnan committed
59 60 61
	cnt = 0
	for trials in range(10):
		generate_file(random.choice(SIZES), random.choice(SEEDS))
62

Sanjay Krishnan committed
63 64 65 66
		data = load_orig_file('data.gen.txt')
		encoded = delta_encoding(data)
		sencoded = shift(encoded ,4)
		write_encoding(sencoded, 'data_out.txt')
67

Sanjay Krishnan committed
68 69 70 71
		loaded = unshift(read_encoding('data_out.txt'),4)
		decoded = delta_decoding(loaded)
		
		cnt += (decoded == data)
72 73

	try:
Sanjay Krishnan committed
74 75 76 77 78 79 80 81 82
		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()