auto_grader.py 1.84 KB
Newer Older
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
import random
import string

from bloom import *

def generate_random_string(seed=True):	
	chars = string.ascii_uppercase + string.digits
	size = 10
	return ''.join(random.choice(chars) for x in range(size))

def test_hash_generation():
	b = Bloom(5,10)

	try:
		assert(len(b.hashes) == 10)
	except:
		print('[#1] Failure the number of generated hashes is wrong')

	try:

		for h in b.hashes:
			h(generate_random_string())

	except:
		print('[#2] The hashes are not properly represented as a lambda')


	s = generate_random_string()

	try:
		for h in b.hashes:
			assert(h(s) == h(s))
	except:
		print('[#3] Hashes are not deterministic')


	try:
		b = Bloom(100,10)
		b1h = b.hashes[0](s)
		b = Bloom(100,10)
		b2h = b.hashes[0](s)

		assert(b1h == b2h)

	except:
		print('[#4] Seeds are not properly set')


	try:
		b = Bloom(100,10)

		for h in b.hashes:
			for i in range(10):
				assert( h(generate_random_string())< 100 )

	except:
		print('[#5] Hash exceeds range')


	try:
		b = Bloom(1000,2)
		s = generate_random_string()
		bh1 = b.hashes[0](s)
		bh2 = b.hashes[1](s)

		assert(bh1 != bh2)

	except:
		print('[#6] Hashes generated are not independent')

def test_put():
	b = Bloom(100,10,seed=0)
	b.put('the')
	b.put('university')
	b.put('of')
	b.put('chicago')

	try:
		assert(sum(b.array) == 30)
	except:
		print('[#7] Unexpected Put() Result')

def test_put_get():
	b = Bloom(100,5,seed=0)
	b.put('the')
	b.put('quick')
	b.put('brown')
	b.put('fox')
	b.put('jumped')
	b.put('over')
	b.put('the')
	b.put('lazy')
	b.put('dog')

	results = [b.contains('the'),\
			   b.contains('cow'), \
			   b.contains('jumped'), \
			   b.contains('over'),\
			    b.contains('the'), \
			    b.contains('moon')]

	try:
		assert(results == [True, False, True, True, True, False])
	except:
		print('[#8] Unexpected contains result')

test_hash_generation()
test_put()
test_put_get()