2016-10-17 09:28:31 +08:00
|
|
|
from nose.tools import assert_equal, assert_raises
|
2015-07-04 07:56:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestHashMap(object):
|
2015-07-12 03:34:14 +08:00
|
|
|
|
2015-07-04 07:56:11 +08:00
|
|
|
# TODO: It would be better if we had unit tests for each
|
|
|
|
# method in addition to the following end-to-end test
|
|
|
|
def test_end_to_end(self):
|
|
|
|
hash_table = HashTable(10)
|
2015-07-12 03:34:14 +08:00
|
|
|
|
2015-07-04 07:56:11 +08:00
|
|
|
print("Test: get on an empty hash table index")
|
2016-10-17 09:28:31 +08:00
|
|
|
assert_raises(KeyError, hash_table.get, 0)
|
2015-07-12 03:34:14 +08:00
|
|
|
|
2015-07-04 07:56:11 +08:00
|
|
|
print("Test: set on an empty hash table index")
|
|
|
|
hash_table.set(0, 'foo')
|
|
|
|
assert_equal(hash_table.get(0), 'foo')
|
|
|
|
hash_table.set(1, 'bar')
|
|
|
|
assert_equal(hash_table.get(1), 'bar')
|
2015-07-12 03:34:14 +08:00
|
|
|
|
2015-07-04 07:56:11 +08:00
|
|
|
print("Test: set on a non empty hash table index")
|
|
|
|
hash_table.set(10, 'foo2')
|
|
|
|
assert_equal(hash_table.get(0), 'foo')
|
|
|
|
assert_equal(hash_table.get(10), 'foo2')
|
2015-07-12 03:34:14 +08:00
|
|
|
|
2015-07-04 07:56:11 +08:00
|
|
|
print("Test: set on a key that already exists")
|
|
|
|
hash_table.set(10, 'foo3')
|
|
|
|
assert_equal(hash_table.get(0), 'foo')
|
|
|
|
assert_equal(hash_table.get(10), 'foo3')
|
2015-07-12 03:34:14 +08:00
|
|
|
|
2015-07-04 07:56:11 +08:00
|
|
|
print("Test: remove on a key that already exists")
|
|
|
|
hash_table.remove(10)
|
|
|
|
assert_equal(hash_table.get(0), 'foo')
|
2016-10-17 09:28:31 +08:00
|
|
|
assert_raises(KeyError, hash_table.get, 10)
|
2015-07-12 03:34:14 +08:00
|
|
|
|
2015-07-04 07:56:11 +08:00
|
|
|
print("Test: remove on a key that doesn't exist")
|
2016-10-17 09:28:31 +08:00
|
|
|
assert_raises(KeyError, hash_table.remove, -1)
|
2015-07-12 03:34:14 +08:00
|
|
|
|
2015-07-04 07:56:11 +08:00
|
|
|
print('Success: test_end_to_end')
|
|
|
|
|
2015-07-12 03:34:14 +08:00
|
|
|
|
2015-07-04 07:56:11 +08:00
|
|
|
def main():
|
|
|
|
test = TestHashMap()
|
|
|
|
test.test_end_to_end()
|
2015-07-12 03:34:14 +08:00
|
|
|
|
|
|
|
|
2015-07-04 07:56:11 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|