예제 #1
0
        public void TestContainsKey()
        {
            Assert.IsTrue(hm.ContainsKey(876.ToString()), "Returned false for valid key");
            Assert.IsTrue(!hm.ContainsKey("KKDKDKD"), "Returned true for invalid key");

            HashMap <Object, Object> m = new HashMap <Object, Object>();

            m.Put(null, "test");
            Assert.IsTrue(m.ContainsKey(null), "Assert.Failed with null key");
            Assert.IsTrue(!m.ContainsKey(0), "Assert.Failed with missing key matching null hash");
        }
예제 #2
0
 public override bool Contains(E obj)
 {
     return(backingMap.ContainsKey(obj));
 }
예제 #3
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");
        }
예제 #4
0
 public override bool Contains(K key)
 {
     return(parent.ContainsKey(key));
 }