コード例 #1
0
 def test_items(self):
     test_input = StringIO(30)
     sys.stdin = test_input
     sys.stdout = StringIO()
     ht = OAHashTable()
     self.assertTrue(ht.size, 31)
     self.assertEqual(ht.occupancy, 0)
     self.assertEqual(len(ht), 0)
     for i in range(5, 26):
         ht.insert(i, i + 10)
     self.assertEqual(ht.size, 31)
     self.assertEqual(ht.occupancy, 21)
     self.assertEqual(len(ht), 21)
コード例 #2
0
 def test_membership(self):
     test_input = StringIO(1000)  # Create a ht with 1021 slots
     sys.stdin = test_input
     sys.stdout = StringIO()
     ht = OAHashTable()
     # Cause two intentional collisions, makes sure everything is still added
     ht.insert(1, "item")
     self.assertTrue(1 in ht)
     ht.insert(1022, "stuff")
     self.assertTrue(1022 in ht)
     ht.insert(1023, "stuff")
     self.assertEqual(ht.occupancy, 3)
     self.assertTrue(1023 in ht)
     self.assertFalse(2 in ht)
コード例 #3
0
 def test_remove_item(self):
     test_input = StringIO(1000)  # Create a ht with 1021 slots
     sys.stdin = test_input
     sys.stdout = StringIO()
     ht = OAHashTable()
     # Intentionally cause two collisions
     ht.insert(1, "item")
     ht.insert(1022, "stuff")
     ht.insert(1023, "things")
     # Remove an item, make sure it's gone and the occupancy is smaller
     self.assertTrue(ht.remove_item(1022))
     self.assertFalse(1022 in ht)
     self.assertEqual(ht.occupancy, 2)
     # Same as above
     self.assertTrue(ht.remove_item(1))
     self.assertFalse(1 in ht)
     self.assertEqual(ht.occupancy, 1)
     ht.insert(2014, "widget")
     self.assertTrue(2014 in ht)
     self.assertEqual(ht.occupancy, 2)
     self.assertTrue(ht.remove_item(2014))
     self.assertFalse(2014 in ht)
     self.assertEqual(ht.occupancy, 1)
     # Check that duplicates don't insert
     ht.insert(1023, "things")
     self.assertTrue(1023 in ht)
     self.assertEqual(ht.occupancy, 1)
     # Check the used spots can take items
     ht.insert(1022, "something else")
     self.assertTrue(1022 in ht)
     self.assertEqual(ht.occupancy, 2)
     # Check if the hash can find non-number entries
     ht.insert(650.000, "floats aren't ints!")
     self.assertTrue(650.000 in ht)
     ht.insert("cat", "can it hash strings? yup!")
     self.assertTrue("cat" in ht)
     testset = (1, 2, 3)
     ht.insert(testset, "can it hash sets? yup!")
     self.assertTrue(testset in ht)
コード例 #4
0
 def test_insert_hash(self):
     test_input = StringIO(1000)  # Create a ht with 1021 slots
     sys.stdin = test_input
     sys.stdout = StringIO()
     ht = OAHashTable()
     # Basic insertion _appears_ to work for immutable types (doesn't duplicate)
     # Numbers, strings, and sets: OK!  Lists & Dicts: Not hashable!
     self.assertEqual(ht.size, 1021)
     ht.insert(650, "item")
     self.assertEqual(ht.occupancy, 1)
     ht.insert(650, "item")
     self.assertEqual(ht.occupancy, 1)
     ht.insert(650.000, "floats aren't ints!")
     self.assertEqual(ht.occupancy, 2)
     ht.insert(65, "different item")
     self.assertEqual(ht.occupancy, 3)
     ht.insert("cat", "can it hash strings? yup!")
     self.assertEqual(ht.occupancy, 4)
     testset = (1, 2, 3)
     ht.insert(testset, "can it hash sets? yup!")
     self.assertEqual(ht.occupancy, 5)
コード例 #5
0
 def test_resize_table(self):
     test_input = StringIO(13)
     sys.stdin = test_input
     sys.stdout = StringIO()
     ht = OAHashTable()
     self.assertTrue(ht.size, 13)
     self.assertEqual(ht.occupancy, 0)
     # Fill table to maximum capacity before resizing (12/13, triggers on #13)
     ht.insert(0, 'a'), ht.insert(1, 'b'), ht.insert(2, 'c')
     ht.insert(3, 'd'), ht.insert(4, 'e')
     self.assertEqual(ht.size, 13)
     self.assertEqual(ht.occupancy, 5)
     ht.insert(5, 'f'), ht.insert(6, 'g'), ht.insert(7, 'h')
     ht.insert(8, 'i'), ht.insert(9, 'j')
     self.assertEqual(ht.size, 13)
     self.assertEqual(ht.occupancy, 10)
     # At this point, the table should be at 0.769 % full and resize up.
     # 2 x 13 = 26, next largest prime is 31
     ht.insert(10, 'k')
     self.assertEqual(ht.size, 31)
     self.assertEqual(ht.occupancy, 11)
     # The next full point for a table of 31 should happen at 22 elements.
     # Trying to insert the 23rd should cause a resize to 73. (Started at 0, i=21)
     for i in range(11, 22):
         ht.insert(i, i + 10)
     self.assertEqual(ht.size, 31)
     self.assertEqual(ht.occupancy, 22)
     ht.insert(22, "stuff")
     self.assertEqual(ht.size, 73)
     self.assertEqual(ht.occupancy, 23)
     # The next full point for a table of 73 should happen at 52 elements.
     # Trying to insert the 53rd should cause a resize to 151.
     for i in range(23, 52):
         ht.insert(i, i + 20)
     self.assertEqual(ht.size, 73)
     self.assertEqual(ht.occupancy, 52)
     ht.insert(52, "other thing")
     self.assertEqual(ht.size, 151)
     self.assertEqual(ht.occupancy, 53)
コード例 #6
0
from HashTableOAClass import OAHashTable

ht = OAHashTable()

for i in range(0, 100):
    ht.insert(i, i + 10)

ht.keys()
ht.values()
ht.items()

print ht.size
print ht.occupancy
print len(ht)

for i in ht.keys():
    print i,

print "/n"

for i in ht.values():
    print i,

print "/n"

for i in ht.items():
    print i,
コード例 #7
0
from HashTableOAClass import OAHashTable

ht = OAHashTable()

for i in range(0,100):
    ht.insert(i, i + 10)
   
   
ht.keys() 
ht.values()
ht.items()

print ht.size
print ht.occupancy
print len(ht)


for i in ht.keys():
    print i,

print "/n"

for i in ht.values():
    print i,

print "/n"

for i in ht.items():
    print i,