auto_grader.py 1.84 KB
Newer Older
1
import random
Sanjay Krishnan committed
2
import string
3

Sanjay Krishnan committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
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')
18 19 20

	try:

Sanjay Krishnan committed
21 22 23 24 25 26 27 28
		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()
29 30

	try:
Sanjay Krishnan committed
31 32 33 34
		for h in b.hashes:
			assert(h(s) == h(s))
	except:
		print('[#3] Hashes are not deterministic')
35 36 37


	try:
Sanjay Krishnan committed
38 39 40 41 42 43 44 45 46
		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')
47 48 49


	try:
Sanjay Krishnan committed
50 51 52 53 54
		b = Bloom(100,10)

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

Sanjay Krishnan committed
56 57
	except:
		print('[#5] Hash exceeds range')
58 59


Sanjay Krishnan committed
60 61 62 63 64
	try:
		b = Bloom(1000,2)
		s = generate_random_string()
		bh1 = b.hashes[0](s)
		bh2 = b.hashes[1](s)
65

Sanjay Krishnan committed
66
		assert(bh1 != bh2)
67

Sanjay Krishnan committed
68 69
	except:
		print('[#6] Hashes generated are not independent')
70

Sanjay Krishnan committed
71 72 73 74 75 76
def test_put():
	b = Bloom(100,10,seed=0)
	b.put('the')
	b.put('university')
	b.put('of')
	b.put('chicago')
77

Sanjay Krishnan committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	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')]
101 102

	try:
Sanjay Krishnan committed
103 104 105 106 107 108 109 110 111 112
		assert(results == [True, False, True, True, True, False])
	except:
		print('[#8] Unexpected contains result')

test_hash_generation()
test_put()
test_put_get()