コード例 #1
0
        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));
        }
コード例 #2
0
        public void TestRemoveObject()
        {
            Object key = new Object();
            Object val = new Object();

            AbstractMap <Object, Object> map1 = new HashMap <Object, Object>(0);

            map1.Put("key", val);
            Assert.AreSame(map1.Remove("key"), val, "HashMap(0)");

            AbstractMap <Object, Object> map5 = new LinkedHashMap <Object, Object>(122);

            map5.Put(key, val);
            Assert.AreSame(map5.Remove(key), val, "LinkedHashMap");

            AbstractMap <Object, Object> aSpecialMap = new MyMap();

            aSpecialMap.Put(specialKey, specialValue);
            Object valueOut = aSpecialMap.Remove(specialKey);

            Assert.AreSame(valueOut, specialValue, "MyMap");
        }
コード例 #3
0
 public override bool Remove(E obj)
 {
     return(backingMap.Remove(obj) != null);
 }
コード例 #4
0
        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");
        }