def test_prune_with_four_buckets(self): """ Tests prune with a stupid table of 4 buckets, this can store at most 3 elements at once. """ ht = HashTable(buckets=4) # Adds elements one by one. d replaces 'e', then 'e' replaces 'b' ht.update({'e': 1, 'a': 3, 'b': 2}) ht.update({'d': 5}) ht.update({'e': 4}) self.assertEqual(set(ht.items()), set({ 'a': 3, 'd': 5, 'e': 4 }.items())) self.assertEqual(len(ht), 3, "The number of elements after pruning should be 3") # Increment existing 'a', then add 'b' which evicts 2 elements with the lowest value of 4 ht.increment('a') self.assertEqual(len(ht), 3) self.assertEqual(set(ht.items()), set({ 'a': 4, 'd': 5, 'e': 4 }.items())) ht.increment('b') self.assertEqual(len(ht), 2, "The number of elements after pruning should be 3") self.assertEqual(set(ht.items()), set({'b': 1, 'd': 5}.items()))
def test_increment_by_big_number(self): ht = HashTable(buckets=64) # increment by big number big_number = 68728041949 ht.increment('big number', big_number) self.assertEqual(ht['big number'], big_number) ht.increment('big number', 1) self.assertEqual(ht['big number'], big_number + 1)
def test_increment_by_number_greater_than_long_long_max(self): """ Negative test: increment fails on a number which is larger than long long's max """ ht = HashTable(buckets=64) with self.assertRaises(OverflowError): ht.increment('toomuch', long_long_max + 1) self.assertEqual(ht['toomuch'], 0, 'Should be unaffected')
def test_increment_by_zero(self): """ Tests that increment by zero does not affect the counter """ ht = HashTable(buckets=64) ht['bar'] = 2 ht.increment('foo', 0) self.assertEqual(ht['foo'], 0) ht.increment('bar', 0) self.assertEqual(ht['bar'], 2)
def test_increment_overflow(self): """ Negative test for overflowing on max counter value (long long max) """ ht = HashTable(buckets=64) ht['max'] = long_long_max with self.assertRaises(OverflowError): ht.increment('max', 1) three_quarters_of_long_long_max = int(long_long_max * 3 / 4) ht['foo'] = three_quarters_of_long_long_max with self.assertRaises(OverflowError): ht.increment('max', three_quarters_of_long_long_max)
def test_quality_default(self): ht = HashTable(buckets=1024) """ Uses the default structure """ self.assertEqual(ht.quality(), 0) for i in range(512): ht.increment(str(i), 1 + (i % 13)) self.assertAlmostEqual(ht.quality(), 2.0 / 3) for i in range(1024): ht.increment(str(1024 + i), 1 + (i % 17)) self.assertAlmostEqual(ht.quality(), 2.0, delta=0.015)
def test_basic_increments(self): """ Tests increment operation """ ht = HashTable(buckets=64) # new element ht.increment('New element by default') self.assertEqual(ht['New element by default'], 1) # new element by X ht.increment('New element by 3', 3) self.assertEqual(ht['New element by 3'], 3) # repeated increments ht.increment('2 repeated increments') ht.increment('2 repeated increments') self.assertEqual(ht['2 repeated increments'], 2) # repeated increments by X ht.increment('3 repeated increments by 4', 4) ht.increment('3 repeated increments by 4', 4) ht.increment('3 repeated increments by 4', 4) self.assertEqual(ht['3 repeated increments by 4'], 12)
def test_increment_negative(self): ht = HashTable(buckets=64) ht.increment('foo', 3) # new value with self.assertRaises(ValueError): ht.increment('bar', -1) # existing value with self.assertRaises(ValueError): ht.increment('foo', -2)
class HashTableTotalTest(unittest.TestCase): """ Functional tests for HashTable.total method """ def setUp(self): self.ht = HashTable(buckets=64) def test_simple_total(self): self.assertEqual(self.ht.total(), 0) self.ht.update("foo") self.assertEqual(self.ht.total(), 3) def test_set_reset_total(self): self.ht.update("foo") self.assertEqual(self.ht.total(), 3) self.ht['o'] += 2 self.assertEqual(self.ht.total(), 5) self.ht['f'] = 0 self.assertEqual(self.ht.total(), 4) def test_increment_total(self): self.ht.update("foo") self.ht.increment("f", 5) self.assertEqual(self.ht.total(), 8) self.ht.increment("a", 2) self.assertEqual(self.ht.total(), 10) self.ht.increment("r", 0) self.assertEqual(self.ht.total(), 10) def test_delete_total(self): self.ht.update("foo") del self.ht['o'] self.assertEqual(self.ht.total(), 1) def test_prune_total(self): self.ht = HashTable(buckets=4) self.ht.update("223334444") self.assertEqual(self.ht.total(), 9) self.ht.update("1") self.assertEqual(self.ht.total(), 10)
class HashTablePickleTest(unittest.TestCase): """ Functional tests for determining size (cardinality) of hashtable and iterations. """ def setUp(self): self.ht = HashTable(buckets=64) def tearDown(self): if os.path.isfile(filename): os.remove(filename) def check_hashtable(self, reloaded): self.assertEqual(len(reloaded), len(self.ht)) self.assertEqual(reloaded.buckets(), self.ht.buckets()) self.assertEqual(reloaded.total(), self.ht.total()) self.assertEqual(set(reloaded.items()), set(self.ht.items())) self.assertEqual(reloaded.quality(), self.ht.quality()) self.assertEqual(reloaded.cardinality(), self.ht.cardinality()) def store_and_load(self): with open(filename, 'wb') as outfile: pickle.dump(self.ht, outfile) with open(filename, 'rb') as outfile: reloaded = pickle.load(outfile) return reloaded def test_pickle_empty(self): reloaded = self.store_and_load() self.check_hashtable(reloaded) def test_pickle_simple(self): self.ht.update("boss") self.ht.update("pickling") reloaded = self.store_and_load() self.check_hashtable(reloaded) def test_pickle_deleted(self): self.ht.update("boss") self.ht.update("pickling") del self.ht['g'] del self.ht['s'] reloaded = self.store_and_load() self.check_hashtable(reloaded) def test_pickle_pruned(self): for i in range(120): self.ht.increment(str(i), 1 + ((i * 27) % 17)) reloaded = self.store_and_load() self.check_hashtable(reloaded) def test_pickle_large(self): self.ht = HashTable(buckets=2 ** 25) self.ht.update("boss") self.ht.update("pickling") self.ht.update("verylargetable") reloaded = self.store_and_load() self.check_hashtable(reloaded)
def test_increment_by_long_long_max(self): ht = HashTable(buckets=64) ht.increment('max', long_long_max) self.assertEqual(ht['max'], long_long_max)