public void TestRehash() { // This map should rehash on adding the ninth element. HashMap <MyKey, Object> hm = new HashMap <MyKey, Object>(10, 0.5f); // Ordered set of keys. MyKey[] keyOrder = new MyKey[9]; for (int i = 0; i < keyOrder.Length; i++) { keyOrder[i] = new MyKey(); } // Store eight elements for (int i = 0; i < 8; i++) { hm.Put(keyOrder[i], i); } // Check expected ordering (inverse of adding order) MyKey[] returnedKeys = hm.KeySet().ToArray(); for (int i = 0; i < 8; i++) { Assert.AreSame(keyOrder[i], returnedKeys[7 - i]); } // The Next Put causes a rehash hm.Put(keyOrder[8], 8); // Check expected new ordering (adding order) returnedKeys = hm.KeySet().ToArray(); for (int i = 0; i < 9; i++) { Assert.AreSame(keyOrder[i], returnedKeys[i]); } }
public void TestKeySet() { Set <Object> s = hm.KeySet(); Assert.IsTrue(s.Size() == hm.Size(), "Returned set of incorrect Size()"); for (int i = 0; i < objArray.Length; i++) { Assert.IsTrue(s.Contains(objArray[i].ToString()), "Returned set does not contain all keys"); } HashMap <Object, Object> m = new HashMap <Object, Object>(); m.Put(null, "test"); Assert.IsTrue(m.KeySet().Contains(null), "Assert.Failed with null key"); Assert.IsNull(m.KeySet().Iterator().Next(), "Assert.Failed with null key"); Map <Object, Object> map = new HashMap <Object, Object>(101); map.Put(1, "1"); map.Put(102, "102"); map.Put(203, "203"); Iterator <Object> it = map.KeySet().Iterator(); Object remove1 = it.Next(); Assert.IsTrue(it.HasNext); it.Remove(); Object remove2 = (Int32)it.Next(); it.Remove(); ArrayList <Object> list = new ArrayList <Object>(Arrays.AsList(new Object[] { 1, 102, 203 })); list.Remove(remove1); list.Remove(remove2); Assert.IsTrue(it.Next().Equals(list.Get(0)), "Wrong result"); Assert.AreEqual(1, map.Size(), "Wrong Size"); Assert.IsTrue(map.KeySet().Iterator().Next().Equals(list.Get(0)), "Wrong contents"); Map <Object, Object> map2 = new HashMap <Object, Object>(101); map2.Put(1, "1"); map2.Put(4, "4"); Iterator <Object> it2 = map2.KeySet().Iterator(); Object remove3 = it2.Next(); Object Next; if ((int)remove3 == 1) { Next = 4; } else { Next = 1; } Assert.IsTrue(it2.HasNext); it2.Remove(); Assert.IsTrue(it2.Next().Equals(Next), "Wrong result 2"); Assert.AreEqual(1, map2.Size(), "Wrong Size 2"); Assert.IsTrue(map2.KeySet().Iterator().Next().Equals(Next), "Wrong contents 2"); }
public void TestKeySet() { AbstractMap <Object, Object> map1 = new HashMap <Object, Object>(0); Assert.AreSame(map1.KeySet(), map1.KeySet(), "HashMap(0)"); AbstractMap <Object, Object> map2 = new HashMap <Object, Object>(10); Assert.AreSame(map2.KeySet(), map2.KeySet(), "HashMap(10)"); Map <Object, Object> map3 = CollectionUtils.EMPTY_MAP; Assert.AreSame(map3.KeySet(), map3.KeySet(), "EMPTY_MAP"); AbstractMap <Object, Object> map5 = new LinkedHashMap <Object, Object>(122); Assert.AreSame(map5.KeySet(), map5.KeySet(), "LinkedHashMap"); }
public void TestNullsOnViews() { Map <String, String> nullHostile = new HashMap <String, String>(); nullHostile.Put("a", "apple"); TestNullsOnView(nullHostile.EntrySet()); nullHostile.Put("a", "apple"); TestNullsOnView(nullHostile.KeySet()); nullHostile.Put("a", "apple"); TestNullsOnView(nullHostile.Values()); }
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 override Iterator <E> Iterator() { return(backingMap.KeySet().Iterator()); }
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"); }