示例#1
0
 def test_hash_table_insertion_and_retrieval(self):
   ht = HashTable(8)
   
   hash_table_insert(ht, "key-0", "val-0")
   hash_table_insert(ht, "key-1", "val-1")
   hash_table_insert(ht, "key-2", "val-2")
   hash_table_insert(ht, "key-3", "val-3")
   hash_table_insert(ht, "key-4", "val-4")
   hash_table_insert(ht, "key-5", "val-5")
   hash_table_insert(ht, "key-6", "val-6")
   hash_table_insert(ht, "key-7", "val-7")
   hash_table_insert(ht, "key-8", "val-8")
   hash_table_insert(ht, "key-9", "val-9")
   
   return_value = hash_table_retrieve(ht, "key-0")
   self.assertTrue(return_value == "val-0")
   return_value = hash_table_retrieve(ht, "key-1")
   self.assertTrue(return_value == "val-1")
   return_value = hash_table_retrieve(ht, "key-2")
   self.assertTrue(return_value == "val-2")
   return_value = hash_table_retrieve(ht, "key-3")
   self.assertTrue(return_value == "val-3")
   return_value = hash_table_retrieve(ht, "key-4")
   self.assertTrue(return_value == "val-4")
   return_value = hash_table_retrieve(ht, "key-5")
   self.assertTrue(return_value == "val-5")
   return_value = hash_table_retrieve(ht, "key-6")
   self.assertTrue(return_value == "val-6")
   return_value = hash_table_retrieve(ht, "key-7")
   self.assertTrue(return_value == "val-7")
   return_value = hash_table_retrieve(ht, "key-8")
   self.assertTrue(return_value == "val-8")
   return_value = hash_table_retrieve(ht, "key-9")
   self.assertTrue(return_value == "val-9")
 def test_auto_resizing(self):
     ht = HashTable(1)
     hash_table_insert(ht, 'first', 'test')
     self.assertEqual(ht.capacity, 2)
     hash_table_insert(ht, 'second', 'test2')
     self.assertEqual(ht.capacity, 4)
     hash_table_insert(ht, 'third', 'test3')
     self.assertEqual(ht.capacity, 8)
     hash_table_remove(ht, 'third')
     hash_table_remove(ht, "second")
     self.assertEqual(ht.capacity, 4)
    def test_hash_table_removes_correctly(self):
        ht = HashTable(8)

        hash_table_insert(ht, "key-0", "val-0")
        hash_table_insert(ht, "key-1", "val-1")
        hash_table_insert(ht, "key-2", "val-2")
        hash_table_insert(ht, "key-3", "val-3")
        hash_table_insert(ht, "key-4", "val-4")
        hash_table_insert(ht, "key-5", "val-5")
        hash_table_insert(ht, "key-6", "val-6")
        hash_table_insert(ht, "key-7", "val-7")
        hash_table_insert(ht, "key-8", "val-8")
        hash_table_insert(ht, "key-9", "val-9")

        print(ht.storage)
        hash_table_remove(ht, "key-9")
        hash_table_remove(ht, "key-8")
        hash_table_remove(ht, "key-7")
        hash_table_remove(ht, "key-6")
        hash_table_remove(ht, "key-5")
        hash_table_remove(ht, "key-4")
        hash_table_remove(ht, "key-3")
        hash_table_remove(ht, "key-2")
        hash_table_remove(ht, "key-1")
        hash_table_remove(ht, "key-0")

        return_value = hash_table_retrieve(ht, "key-0")
        self.assertTrue(return_value is None)
        return_value = hash_table_retrieve(ht, "key-1")
        self.assertTrue(return_value is None)
        return_value = hash_table_retrieve(ht, "key-2")
        self.assertTrue(return_value is None)
        return_value = hash_table_retrieve(ht, "key-3")
        self.assertTrue(return_value is None)
        return_value = hash_table_retrieve(ht, "key-4")
        self.assertTrue(return_value is None)
        return_value = hash_table_retrieve(ht, "key-5")
        self.assertTrue(return_value is None)
        return_value = hash_table_retrieve(ht, "key-6")
        self.assertTrue(return_value is None)
        return_value = hash_table_retrieve(ht, "key-7")
        self.assertTrue(return_value is None)
        return_value = hash_table_retrieve(ht, "key-8")
        self.assertTrue(return_value is None)
        return_value = hash_table_retrieve(ht, "key-9")
        self.assertTrue(return_value is None)
示例#4
0
  def test_hash_table_resize(self):
    ht = HashTable(8)
    
    hash_table_insert(ht, "key-0", "val-0")
    hash_table_insert(ht, "key-1", "val-1")
    hash_table_insert(ht, "key-2", "val-2")
    hash_table_insert(ht, "key-3", "val-3")
    hash_table_insert(ht, "key-4", "val-4")
    hash_table_insert(ht, "key-5", "val-5")
    hash_table_insert(ht, "key-6", "val-6")
    hash_table_insert(ht, "key-7", "val-7")
    hash_table_insert(ht, "key-8", "val-8")
    hash_table_insert(ht, "key-9", "val-9")

    ht = hash_table_resize(ht)
    
    self.assertTrue(len(ht.storage) == 16)

    return_value = hash_table_retrieve(ht, "key-0")
    self.assertTrue(return_value == "val-0")
    return_value = hash_table_retrieve(ht, "key-1")
    self.assertTrue(return_value == "val-1")
    return_value = hash_table_retrieve(ht, "key-2")
    self.assertTrue(return_value == "val-2")
    return_value = hash_table_retrieve(ht, "key-3")
    self.assertTrue(return_value == "val-3")
    return_value = hash_table_retrieve(ht, "key-4")
    self.assertTrue(return_value == "val-4")
    return_value = hash_table_retrieve(ht, "key-5")
    self.assertTrue(return_value == "val-5")
    return_value = hash_table_retrieve(ht, "key-6")
    self.assertTrue(return_value == "val-6")
    return_value = hash_table_retrieve(ht, "key-7")
    self.assertTrue(return_value == "val-7")
    return_value = hash_table_retrieve(ht, "key-8")
    self.assertTrue(return_value == "val-8")
    return_value = hash_table_retrieve(ht, "key-9")
    self.assertTrue(return_value == "val-9")
 def test_doesnt_shrink_if_still_initial_size(self):
     ht = HashTable(32)
     hash_table_insert(ht, 'first', 'test')
     self.assertEqual(ht.capacity, 32)
     hash_table_remove(ht, 'first')
     self.assertEqual(ht.capacity, 32)