public void TestConstructorMap() { Map <Object, Object> myMap = new LinkedHashMap <Object, Object>(); for (int counter = 0; counter < hmSize; counter++) { myMap.Put(objArray2[counter], objArray[counter]); } HashMap <Object, Object> hm2 = new HashMap <Object, Object>(myMap); for (int counter = 0; counter < hmSize; counter++) { Assert.IsTrue(hm.Get(objArray2[counter]) == hm2.Get(objArray2[counter]), "Assert.Failed to construct correct HashMap"); } try { Map <Object, Object> mockMap = new MockMap(); hm = new HashMap <Object, Object>(mockMap); Assert.Fail("Should throw NullReferenceException"); } catch (NullReferenceException) { //empty } HashMap <Object, Object> map = new HashMap <Object, Object>(); map.Put("a", "a"); SubMap <Object, Object> map2 = new SubMap <Object, Object>(map); Assert.IsTrue(map2.ContainsKey("a")); Assert.IsTrue(map2.ContainsValue("a")); }
public void TestRemove() { int Size = hm.Size(); Object y = 9; Object x = hm.Remove(y.ToString()); Assert.IsTrue(x.Equals(9), "Remove returned incorrect value"); Assert.IsNull(hm.Get(9), "Assert.Failed to Remove given key"); Assert.IsTrue(hm.Size() == (Size - 1), "Assert.Failed to decrement Size"); Assert.IsNull(hm.Remove("LCLCLC"), "Remove of non-existent key returned non-null"); HashMap <Object, Object> m = new HashMap <Object, Object>(); m.Put(null, "test"); Assert.IsNull(m.Remove(0), "Assert.Failed with same hash as null"); Assert.AreEqual("test", m.Remove(null), "Assert.Failed with null key"); HashMap <Object, Object> map = new HashMap <Object, Object>(); for (int i = 0; i < 32768; i++) { map.Put(i, "const"); } Object[] values = new Object[32768]; for (int i = 0; i < 32768; i++) { values[i] = new Object(); map.Put(i, values[i]); } for (int i = 32767; i >= 0; i--) { Assert.AreEqual(values[i], map.Remove(i), "Assert.Failed to Remove same value"); } // Ensure keys with identical hashcode are Removed properly map = new HashMap <Object, Object>(); for (int i = -32767; i < 32768; i++) { map.Put(i, "foobar"); } // Remove non equal object with same hashcode Assert.IsNull(map.Remove(new MyKey())); Assert.AreEqual("foobar", map.Get(0)); map.Remove(0); Assert.IsNull(map.Get(0)); }
public void TestClone() { HashMap <Object, Object> hm2 = (HashMap <Object, Object>)hm.Clone(); Assert.IsTrue(hm2 != hm, "Clone answered equivalent HashMap"); for (int counter = 0; counter < hmSize; counter++) { Assert.IsTrue(hm.Get(objArray2[counter]) == hm2.Get(objArray2[counter]), "Clone answered unequal HashMap"); } HashMap <Object, Object> map = new HashMap <Object, Object>(); map.Put("key", "value"); // get the KeySet() and Values() on the original Map Set <Object> keys = map.KeySet(); Collection <Object> values = map.Values(); Assert.AreEqual("value", values.Iterator().Next(), "Values() does not work"); Assert.AreEqual("key", keys.Iterator().Next(), "KeySet() does not work"); AbstractMap <Object, Object> map2 = (AbstractMap <Object, Object>)map.Clone(); map2.Put("key", "value2"); Collection <Object> values2 = map2.Values(); Assert.IsTrue(values2 != values, "Values() is identical"); // Values() and KeySet() on the Cloned() map should be different Assert.AreEqual("value2", values2.Iterator().Next(), "Values() was not Cloned"); map2.Clear(); map2.Put("key2", "value3"); Set <Object> key2 = map2.KeySet(); Assert.IsTrue(key2 != keys, "KeySet() is identical"); Assert.AreEqual("key2", key2.Iterator().Next(), "KeySet() was not Cloned"); HashMap <Object, Object> hashmap = new HashMap <Object, Object>(); MockClonable mock = new MockClonable(1); hashmap.Put(1, mock); Assert.AreEqual(1, ((MockClonable)hashmap.Get(1)).i); HashMap <Object, Object> hm3 = (HashMap <Object, Object>)hashmap.Clone(); Assert.AreEqual(1, ((MockClonable)hm3.Get(1)).i); mock.i = 0; Assert.AreEqual(0, ((MockClonable)hashmap.Get(1)).i); Assert.AreEqual(0, ((MockClonable)hm3.Get(1)).i); }
public void TestConstructorIF() { HashMap <Object, Object> hm2 = new HashMap <Object, Object>(5, (float)0.5); Assert.AreEqual(0, hm2.Size(), "Created incorrect HashMap"); try { new HashMap <Object, Object>(0, 0); } catch (ArgumentException) { return; } Assert.Fail("Assert.Failed to throw ArgumentException for initial load factor <= 0"); HashMap <Object, Object> empty = new HashMap <Object, Object>(0, 0.75f); Assert.IsNull(empty.Get("nothing"), "Empty hashtable access"); empty.Put("something", "here"); Assert.IsTrue(empty.Get("something").Equals("here"), "cannot get element"); }
public void TestGet() { Assert.IsNull(hm.Get("T"), "Get returned non-null for non existent key"); hm.Put("T", "HELLO"); Assert.AreEqual("HELLO", hm.Get("T"), "Get returned incorrect value for existing key"); HashMap <Object, Object> m = new HashMap <Object, Object>(); m.Put(null, "test"); Assert.AreEqual("test", m.Get(null), "Assert.Failed with null key"); Assert.IsNull(m.Get(0), "Assert.Failed with missing key matching null hash"); ReusableKey k = new ReusableKey(); HashMap <Object, Object> map = new HashMap <Object, Object>(); k.Key = 1; map.Put(k, "value1"); k.Key = 18; Assert.IsNull(map.Get(k)); k.Key = 17; Assert.IsNull(map.Get(k)); }
public void TestPutAll() { HashMap <Object, Object> hm2 = new HashMap <Object, Object>(); hm2.PutAll(hm); for (int i = 0; i < 1000; i++) { Assert.IsTrue(hm2.Get(i.ToString()).Equals((i)), "Assert.Failed to Clear all elements"); } Map <Object, Object> mockMap = new MockMap(); hm2 = new HashMap <Object, Object>(); hm2.PutAll(mockMap); Assert.AreEqual(0, hm2.Size(), "Size should be 0"); }
public void TestClear() { hm.Clear(); Assert.AreEqual(0, hm.Size(), "Clear Assert.Failed to reset Size"); for (int i = 0; i < hmSize; i++) { Assert.IsNull(hm.Get(objArray2[i]), "Assert.Failed to Clear all elements"); } // Check Clear on a large loaded map of Int32 keys HashMap <Object, String> map = new HashMap <Object, String>(); for (int i = -32767; i < 32768; i++) { map.Put(i, "foobar"); } map.Clear(); Assert.AreEqual(0, hm.Size(), "Assert.Failed to reset Size on large integer map"); for (int i = -32767; i < 32768; i++) { Assert.IsNull(map.Get(i), "Assert.Failed to Clear integer map values"); } }
public void TestPut2() { hm.Put("KEY", "VALUE"); Assert.AreEqual("VALUE", hm.Get("KEY"), "Assert.Failed to install key/value pair"); HashMap <Object, Object> m = new HashMap <Object, Object>(); m.Put(0, "short"); m.Put(null, "test"); m.Put(0, "int"); Assert.AreEqual("int", m.Get(0), "Assert.Failed adding to bucket containing null"); Assert.AreEqual("int", m.Get(0), "Assert.Failed adding to bucket containing null2"); // Check my actual key instance is returned HashMap <Object, String> map = new HashMap <Object, String>(); for (int i = -32767; i < 32768; i++) { map.Put(i, "foobar"); } Object myKey = 0; // Put a new value at the old key position map.Put(myKey, "myValue"); Assert.IsTrue(map.ContainsKey(myKey)); Assert.AreEqual("myValue", map.Get(myKey)); bool found = false; for (Iterator <Object> itr = map.KeySet().Iterator(); itr.HasNext;) { Object key = itr.Next(); if (found = key == myKey) { break; } } Assert.IsFalse(found, "Should not find new key instance in hashmap"); // Add a new key instance and check it is returned Assert.IsNotNull(map.Remove(myKey)); map.Put(myKey, "myValue"); Assert.IsTrue(map.ContainsKey(myKey)); Assert.AreEqual(map.Get(myKey), "myValue"); for (Iterator <Object> itr = map.KeySet().Iterator(); itr.HasNext;) { Object key = itr.Next(); if (found = key == myKey) { break; } } Assert.IsTrue(found, "Did not find new key instance in hashmap"); // Ensure keys with identical hashcode are stored separately HashMap <Object, Object> objmap = new HashMap <Object, Object>(); for (int i = 0; i < 32768; i++) { objmap.Put(i, "foobar"); } // Put non-equal object with same hashcode MyKey aKey = new MyKey(); Assert.IsNull(objmap.Put(aKey, "value")); Assert.IsNull(objmap.Remove(new MyKey())); Assert.AreEqual(objmap.Get(0), "foobar"); Assert.AreEqual(objmap.Get(aKey), "value"); }