public virtual Object Clone() { try { HashSet <E> clone = (HashSet <E>)base.MemberwiseClone(); clone.backingMap = (HashMap <E, HashSet <E> >)backingMap.Clone(); return(clone); } catch (NotSupportedException) { return(null); } }
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); }