Ejemplo n.º 1
0
        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]);
            }
        }
Ejemplo n.º 2
0
        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");
        }
Ejemplo n.º 3
0
 public void SetUp()
 {
     hm = new HashMap <object, Object>();
     for (int i = 0; i < objArray.Length; i++)
     {
         hm.Put(objArray2[i], objArray[i]);
     }
     hm.Put("test", null);
     hm.Put(null, "test");
 }
Ejemplo n.º 4
0
        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());
        }
Ejemplo n.º 5
0
        public void TestEqualsWithNullValues()
        {
            Map <String, String> a = new HashMap <String, String>();

            a.Put("a", null);
            a.Put("b", null);

            Map <String, String> b = new HashMap <String, String>();

            a.Put("c", "cat");
            a.Put("d", "dog");

            Assert.IsFalse(a.Equals(b));
            Assert.IsFalse(b.Equals(a));
        }
Ejemplo n.º 6
0
        public void TestMapEntryHashCode()
        {
            HashMap <Object, Object> map = new HashMap <Object, Object>(10);
            Object key = 1;
            Object val = 2;

            map.Put(key, val);
            int expected = key.GetHashCode() ^ val.GetHashCode();

            Assert.AreEqual(expected, map.GetHashCode());
            key = 4;
            val = 8;
            map.Put(key, val);
            expected += key.GetHashCode() ^ val.GetHashCode();
            Assert.AreEqual(expected, map.GetHashCode());
        }
Ejemplo n.º 7
0
        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"));
        }
Ejemplo n.º 8
0
        public void TestValues()
        {
            Collection <Object> c = hm.Values();

            Assert.IsTrue(c.Size() == hm.Size(), "Returned collection of incorrect Size()");
            for (int i = 0; i < objArray.Length; i++)
            {
                Assert.IsTrue(c.Contains(objArray[i]), "Returned collection does not contain all keys");
            }

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

            for (int i = 0; i < 100; i++)
            {
                myHashMap.Put(objArray2[i], objArray[i]);
            }

            Collection <Object> values = myHashMap.Values();

//            new Support_UnmodifiableCollectionTest(
//                 "Test Returned Collection From HashMap.Values()", values)
//                 .runTest();
            values.Remove(0);
            Assert.IsTrue(!myHashMap.ContainsValue(0),
                          "Removing from the values collection should Remove from the original map");
        }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
0
        public void TestPutAllLMap()
        {
            Map <Object, Object> ht  = new HashMap <Object, Object>();
            Map <Object, Object> amt = new AMT();

            ht.Put("this", "that");
            amt.PutAll(ht);
            Assert.AreEqual(amt, ht, "Should be equal");
        }
Ejemplo n.º 11
0
        public void TestToString()
        {
            HashMap <Object, Object> m = new HashMap <Object, Object>();

            m.Put(m, m);
            String result = m.ToString();

            Assert.IsTrue(result.IndexOf("(this") > -1, "should contain self ref");
        }
Ejemplo n.º 12
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");
        }
Ejemplo n.º 13
0
        public void TestConstructorOfMap()
        {
            Map <Object, Object> myMap = new HashMap <Object, Object>();

            for (int counter = 0; counter < hmSize; counter++)
            {
                myMap.Put(objArray2[counter], objArray[counter]);
            }
            LinkedHashMap <Object, Object> hm2 = new LinkedHashMap <Object, Object>(myMap);

            for (int counter = 0; counter < hmSize; counter++)
            {
                Assert.IsTrue(hm.Get(objArray2[counter]) == hm2.Get(objArray2[counter]),
                              "Failed to construct correct LinkedHashMap");
            }
        }
Ejemplo n.º 14
0
        public void TestEntrySet2()
        {
            HashMap <Object, Object> map = new HashMap <Object, Object>();

            map.Put(1, "ONE");

            Set <Entry <Object, Object> >      EntrySet = map.EntrySet();
            Iterator <Entry <Object, Object> > e        = EntrySet.Iterator();
            Object real = e.Next();
            Entry <Object, Object> copyEntry = new MockEntry();

            Assert.AreEqual(real, copyEntry);
            Assert.IsTrue(EntrySet.Contains(copyEntry));

            EntrySet.Remove(copyEntry);
            Assert.IsFalse(EntrySet.Contains(copyEntry));
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
0
        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");
        }
Ejemplo n.º 17
0
        public void TestClear()
        {
            // normal Clear()
            AbstractMap <Object, Object> map = new HashMap <Object, Object>();

            map.Put(1, 1);
            map.Clear();
            Assert.IsTrue(map.IsEmpty());

            // Special entrySet return a Set with no Clear method.
            AbstractMap <Object, Object> myMap = new MocAbstractMap <Object, Object>();

            try
            {
                myMap.Clear();
                Assert.Fail("Should throw NotSupportedException");
            }
            catch (NotSupportedException)
            {
            }
        }
Ejemplo n.º 18
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");
        }
Ejemplo n.º 19
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");
            }
        }
Ejemplo n.º 20
0
        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));
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Adds the specified object to this HashSet if not already present.
 /// </summary>
 public override bool Add(E obj)
 {
     return(backingMap.Put(obj, this) == null);
 }
Ejemplo n.º 22
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");
        }