Skip to content

Commit

Permalink
Added some tests for hash table and made minor adjustments to hash.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
famavott committed Dec 3, 2017
1 parent 9500a56 commit 3cf3e59
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ def bst_right_imbalanced():
from bst import BST
test_bst = BST((1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
return test_bst


@pytest.fixture
def empty_hash():
"""Initialize empty hash table."""
from hash import HashTable
test_hash = HashTable()
return test_hash
6 changes: 4 additions & 2 deletions src/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ def __init__(self, size=10, hash_type='additive'):
self.hash_type = hash_type
self.buckets = []
if hash_type not in ('additive', 'oat'):
raise ValueError('Hash funciton unsupported')
raise NameError('Hash funciton unsupported')
for i in range(size):
self.buckets.append([])

def _hash(self, key):
"""Hash string from user on get method."""
if type(key) is not str:
raise TypeError('Key must be a string')
if self.hash_type == 'additive':
hash = 0
for char in key:
Expand Down Expand Up @@ -51,4 +53,4 @@ def get(self, key):
for item in self.buckets[idx]:
if item[0] == key:
return item[1]
raise KeyError('Key not found')
raise KeyError('Key not found')
62 changes: 62 additions & 0 deletions src/test_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Testing for hash table tree."""
import pytest


def test_hash_initialized(empty_hash):
"""Test if hash table initailized with attributes."""
assert empty_hash.size == 10
assert empty_hash.hash_type == 'additive'


def test_hash_initiailized_custom_size():
"""Test if hash table can be initailized with custom size."""
from hash import HashTable
cust_hash = HashTable(size=100)
cust_hash.size == 100


def test_hash_initialized_with_oat():
"""Test if hash table can be initialized using oat hashing function."""
from hash import HashTable
cust_hash = HashTable(hash_type='oat')
cust_hash.hash_type == 'oat'


def test_bad_hash_type_raises_error():
"""Test if ValueError raised if function other than additive or oat is passed."""
from hash import HashTable
with pytest.raises(NameError):
HashTable(hash_type='something')


def test_non_string_passed_in_set_raises_error(empty_hash):
"""Type error raised when non-string passed in set method."""
with pytest.raises(TypeError):
empty_hash.set(897, 'toast')


def test_set_adds_pair(empty_hash):
"""Test if set adds to key/val pair to bucket."""
empty_hash.set('potato', 8)
assert empty_hash.buckets[3] == [['potato', 8]]


def test_get_returns_correct_val(empty_hash):
"""Test if correct val returned for get method."""
empty_hash.set('jim', 39)
assert empty_hash.get('jim') == 39


def test_key_can_be_updated_with_new_val(empty_hash):
"""Test if key's value can be updated."""
empty_hash.set('jim', 39)
empty_hash.set('jim', 83)
assert empty_hash.get('jim') == 83


def test_key_not_found_error_raises(empty_hash):
"""Test if KeyError raised when get used for key not in table."""
empty_hash.set('jane', 34)
empty_hash.set('andy', 32)
with pytest.raises(KeyError):
empty_hash.get('bob')

0 comments on commit 3cf3e59

Please sign in to comment.