public static void GetMatchingOrNextAvailableBucket_Retuns_index_of_key_Test() { HashMap <StringKey, Item> hashMap = new HashMap <StringKey, Item>(); StringKey key = new StringKey("New Item"); Item item = new Item("New Item", 1, 1.0); hashMap.Put(key, item); int index = RetrieveKeyIndex(key, hashMap.Table); Assert.AreEqual(index, hashMap.GetMatchingOrNextAvailableBucket(key)); }
public static void GetMatchingOrNextAvailableBucket_Retuns_next_index_when_collision_occurs_Test() { HashMap <StringKey, Item> hashMap = new HashMap <StringKey, Item>(); StringKey key = new StringKey("New Item"); StringKey collisionKey = new StringKey("item_COLLSION"); Item item = new Item("New Item", 1, 1.0); hashMap.Put(key, item); int index = RetrieveKeyIndex(key, hashMap.Table); // we overwrite the spot that item originally hashes to... hashMap.Table[index] = new Entry <StringKey, Item>(collisionKey, item); // test to see if putting the colliding item back retrieves index + 1 Assert.AreEqual(index + 1, hashMap.GetMatchingOrNextAvailableBucket(key)); }
public static void GetMatchingOrNextAvailableBucket_Retuns_0_index_when_collision_on_last_bucket_occurs_Test() { HashMap <StringKey, Item> hashMap = new HashMap <StringKey, Item>(); StringKey key = new StringKey("item"); StringKey collisionKey = new StringKey("item_COLLSION"); Item item = new Item("item", 1, 1.0); hashMap.Put(key, item); int index = RetrieveKeyIndex(key, hashMap.Table); // test that this item was the last item in the array. // this SHOULD work if the same hash in StringKey is made as your instructor. Assert.AreEqual(hashMap.Table.Length - 1, index); // we overwrite the spot that item originally hashes to... hashMap.Table[index] = new Entry <StringKey, Item>(collisionKey, item); // test to see if the next bucket is index 0 during a collision Assert.AreEqual(0, hashMap.GetMatchingOrNextAvailableBucket(key)); }
public static void GetMatchingOrNextAvailableBucket_Retuns_index_on_collision_entry_Test() { HashMap <StringKey, Item> hashMap = new HashMap <StringKey, Item>(); StringKey key = new StringKey("New Item"); StringKey collisionKey = new StringKey("item_COLLSION"); Item item = new Item("New Item", 1, 1.0); hashMap.Put(key, item); int firstIndex = RetrieveKeyIndex(key, hashMap.Table); hashMap.Table[firstIndex] = new Entry <StringKey, Item>(collisionKey, item); hashMap.Put(key, item); // should collide with the first entry and find the next available spot. // get the new index of "Item" int secondIndex = RetrieveKeyIndex(key, hashMap.Table); Assert.AreEqual(secondIndex, hashMap.GetMatchingOrNextAvailableBucket(key)); Assert.AreNotEqual(secondIndex, firstIndex); }