Exemple #1
0
 def test_prune_cardinality(self):
     ht = HashTable(buckets=8)
     ht.update({'a': 3, 'b': 2, 'c': 4, 'd': 1, 'e': 5, 'f': 6})
     ht.update("xbgbbd")
     self.assertEqual(ht.cardinality(), 8)
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)
Exemple #3
0
class HashTableItemsTest(unittest.TestCase):
    """
    Functional tests for determining size (cardinality) of hashtable and iterations.
    """
    def setUp(self):
        self.ht = HashTable(buckets=64)

    def test_simple_length_test(self):
        self.assertEqual(len(self.ht), 0)

        self.ht.update("boss")
        self.assertEqual(len(self.ht), 3)

        self.ht.update("sad")
        self.assertEqual(len(self.ht), 5)

    def test_simple_cardinality_test(self):
        self.assertEqual(self.ht.cardinality(), 0)

        self.ht.update("boss")
        self.assertEqual(self.ht.cardinality(), 3)

        self.ht.update("sad")
        self.assertEqual(self.ht.cardinality(), 5)

    def test_delete_length_test(self):
        self.ht.update("boss")
        self.assertEqual(len(self.ht), 3)

        del self.ht['s']
        self.assertEqual(len(self.ht), 2)
        del self.ht['s']
        self.assertEqual(len(self.ht), 2)

    def test_delete_prune_length_iteration_test(self):
        self.ht = HashTable(buckets=8)
        self.ht.update("bbboss")
        self.assertEqual(len(self.ht), 3)

        del self.ht['s']
        self.assertEqual(len(self.ht), 2)
        self.ht.update("122333")

        self.assertEqual(len(self.ht), 5)
        # should iterate over 5 elements
        self.assertEqual(set(self.ht.items()),
                         set({
                             'b': 3,
                             'o': 1,
                             '1': 1,
                             '2': 2,
                             '3': 3
                         }.items()))

        # the next add will overflow because the deleted bucket is still physically there so the 'real' size is 5
        self.ht.update("!")  # overflow removes '1', 'o', and the empty bucket
        self.assertEqual(len(self.ht), 4)
        # should iterate over 4 elements
        self.assertEqual(set(self.ht.items()),
                         set({
                             'b': 3,
                             '!': 1,
                             '2': 2,
                             '3': 3
                         }.items()))

        self.assertEqual(self.ht.cardinality(), 7)