Beispiel #1
0
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)
Beispiel #3
0
 def test_prune_total(self):
     ht = HashTable(buckets=8)
     ht.update({'a': 3, 'b': 2, 'c': 4, 'd': 1, 'e': 5, 'f': 6})
     ht.update("xbbbd")
     self.assertEqual(ht.total(), 26)