public static void Remove_uses_linear_probing_on_collisions_Test() { HashMap <StringKey, Item> hashMap = new HashMap <StringKey, Item>(); Entry <StringKey, Item>[] table = hashMap.Table; Item item1Value = new Item("Purse", 10, 1); StringKey item1Key = new StringKey(item1Value.Name); Item item2Value = new Item("Helmet", 3, 2.5); StringKey item2Key = new StringKey(item2Value.Name); hashMap.Put(item1Key, item1Value); int bucket1 = item1Key.GetHashCode() % table.Length; int bucket2 = item2Key.GetHashCode() % table.Length; // check to see if there is a collision Assert.AreEqual(bucket1, bucket2); // causes a collision, test for linear probing here! hashMap.Put(item2Key, item2Value); // The second item is in the next available index Assert.AreEqual(item2Value, table[((bucket1 + 1) % table.Length)].Value); //Test Remove HERE }
public static void Put_Overwrites_Value_At_Key_On_Update_Test() { HashMap <StringKey, Item> hashMap = new HashMap <StringKey, Item>(); StringKey key = new StringKey("item"); Item originalItem = new Item("item", 1, 1.0); Item newItem = new Item("item", 2, 2.0); hashMap.Put(key, originalItem); Item oldValue = hashMap.Put(key, newItem); Entry <StringKey, Item>[] table = hashMap.Table; int bucket = key.GetHashCode() % table.Length; Assert.AreEqual(newItem, table[bucket].Value); // doing a thourough check that the OLD value is no longer in the map anywhere bool isFound = false; for (int i = 0; i < table.Length; i++) { if (table[i] != null && table[i].Value.Equals(oldValue) && table[i].Key.Equals(key)) { isFound = true; } } Assert.IsFalse(isFound); }
public static void GetHashCodeEmptyNameTest() { int expectedResult = 0; StringKey stringKey = new StringKey(""); Assert.AreEqual(expectedResult, stringKey.GetHashCode()); }
public static void GetHashCodeVarietyTest() { StringKey stringKey1 = new StringKey("stop"); StringKey stringKey2 = new StringKey("pots"); Assert.AreNotEqual(stringKey1.GetHashCode(), stringKey2.GetHashCode()); }
public static void GetHashCodeTest() { int expectedResult = 3446974; StringKey stringKey = new StringKey("stop"); // NOTE: this may differ from your tests, you may ignore this test! Assert.AreEqual(expectedResult, stringKey.GetHashCode()); }
public void GetHashCodeTest() { double coe = 31; StringKey stringKey = new StringKey("and"); int expected = (int)(('a' - '0') * 1 + ('n' - '0') * coe + ('d' - '0') * coe * coe); Assert.AreEqual(expected, stringKey.GetHashCode()); }
public static void Put_Adds_Value_To_Hash_Test() { HashMap <StringKey, Item> hashMap = new HashMap <StringKey, Item>(); StringKey key = new StringKey("item"); Item item = new Item("item", 1, 1.0); hashMap.Put(key, item); Entry <StringKey, Item>[] table = hashMap.Table; int bucket = key.GetHashCode() % table.Length; Assert.AreEqual(item, table[bucket].Value); }
public override int GetHashCode() { int hash = 1; if (typeCase_ == TypeOneofCase.StringKey) { hash ^= StringKey.GetHashCode(); } hash ^= (int)typeCase_; if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } return(hash); }
public static void Remove_keeps_a_key_placeholder_Test() { HashMap <StringKey, Item> hashMap = new HashMap <StringKey, Item>(); StringKey key = new StringKey("item"); Item item = new Item("item", 1, 1.0); hashMap.Put(key, item); Item returnedItem = hashMap.Remove(key); Entry <StringKey, Item>[] table = hashMap.Table; int bucket = key.GetHashCode() % table.Length; // a placeholder will have an Entry with a null for the value, but the key is stil the same as the removed Assert.AreEqual(table[bucket].Key, key); Assert.AreEqual(table[bucket].Value, null); }
public static void GetHashCode_is_not_negative_Test() { StringKey stringKey1 = new StringKey("A REALLY BIG STRING SHOULD NOT OVERFLOW TO NEGATIVE! ALWAYS ABSOLUTE VALUE YOUR HASHCODE!"); Assert.GreaterOrEqual(stringKey1.GetHashCode(), 0); }