You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/03/15 12:47:39 UTC
svn commit: r386058 [37/49] - in /incubator/harmony/enhanced/classlib/trunk:
make/ modules/archive/make/common/ modules/archive/src/test/java/tests/
modules/archive/src/test/java/tests/api/
modules/archive/src/test/java/tests/api/java/ modules/archive/...
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/HashtableTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/HashtableTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/HashtableTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/HashtableTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,653 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.ConcurrentModificationException;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.Vector;
+
+import tests.support.Support_MapTest2;
+import tests.support.Support_UnmodifiableCollectionTest;
+
+public class HashtableTest extends junit.framework.TestCase {
+
+ private Hashtable ht10;
+
+ private Hashtable ht100;
+
+ private Hashtable htfull;
+
+ private Vector keyVector;
+
+ private Vector elmVector;
+
+ private String h10sVal;
+
+ /**
+ * @tests java.util.Hashtable#Hashtable()
+ */
+ public void test_Constructor() {
+ // Test for method java.util.Hashtable()
+ new Support_MapTest2(new Hashtable()).runTest();
+
+ Hashtable h = new Hashtable();
+
+ assertTrue("Created incorrect hashtable", h.size() == 0);
+ }
+
+ /**
+ * @tests java.util.Hashtable#Hashtable(int)
+ */
+ public void test_ConstructorI() {
+ // Test for method java.util.Hashtable(int)
+ Hashtable h = new Hashtable(9);
+
+ assertTrue("Created incorrect hashtable", h.size() == 0);
+
+ Hashtable empty = new Hashtable(0);
+ assertTrue("Empty hashtable access", empty.get("nothing") == null);
+ empty.put("something", "here");
+ assertTrue("cannot get element", empty.get("something") == "here");
+ }
+
+ /**
+ * @tests java.util.Hashtable#Hashtable(int, float)
+ */
+ public void test_ConstructorIF() {
+ // Test for method java.util.Hashtable(int, float)
+ Hashtable h = new java.util.Hashtable(10, 0.5f);
+ assertTrue("Created incorrect hashtable", h.size() == 0);
+
+ Hashtable empty = new Hashtable(0, 0.75f);
+ assertTrue("Empty hashtable access", empty.get("nothing") == null);
+ empty.put("something", "here");
+ assertTrue("cannot get element", empty.get("something") == "here");
+ }
+
+ /**
+ * @tests java.util.Hashtable#Hashtable(java.util.Map)
+ */
+ public void test_ConstructorLjava_util_Map() {
+ // Test for method java.util.Hashtable(java.util.Map)
+ Map map = new TreeMap();
+ Object firstVal = "Gabba";
+ Object secondVal = new Integer(5);
+ map.put("Gah", firstVal);
+ map.put("Ooga", secondVal);
+ Hashtable ht = new Hashtable(map);
+ assertTrue("a) Incorrect Hashtable constructed",
+ ht.get("Gah") == firstVal);
+ assertTrue("b) Incorrect Hashtable constructed",
+ ht.get("Ooga") == secondVal);
+ }
+
+ /**
+ * @tests java.util.Hashtable#clear()
+ */
+ public void test_clear() {
+ // Test for method void java.util.Hashtable.clear()
+ Hashtable h = hashtableClone(htfull);
+ h.clear();
+ assertTrue("Hashtable was not cleared", h.size() == 0);
+ Enumeration el = h.elements();
+ Enumeration keys = h.keys();
+ assertTrue("Hashtable improperly cleared", !el.hasMoreElements()
+ && !(keys.hasMoreElements()));
+ }
+
+ /**
+ * @tests java.util.Hashtable#clone()
+ */
+ public void test_clone() {
+ // Test for method java.lang.Object java.util.Hashtable.clone()
+
+ Hashtable h = (Hashtable) htfull.clone();
+ assertTrue("Clone different size than original", h.size() == htfull
+ .size());
+
+ Enumeration org = htfull.keys();
+ Enumeration cpy = h.keys();
+
+ String okey, ckey;
+ while (org.hasMoreElements()) {
+ assertTrue("Key comparison failed", (okey = (String) org
+ .nextElement()).equals(ckey = (String) cpy.nextElement()));
+ assertTrue("Value comparison failed", ((String) htfull.get(okey))
+ .equals((String) h.get(ckey)));
+ }
+ assertTrue("Copy has more keys than original", !cpy.hasMoreElements());
+ }
+
+ /**
+ * @tests java.util.Hashtable#contains(java.lang.Object)
+ */
+ public void test_containsLjava_lang_Object() {
+ // Test for method boolean
+ // java.util.Hashtable.contains(java.lang.Object)
+ assertTrue("Element not found", ht10.contains("Val 7"));
+ assertTrue("Invalid element found", !ht10.contains("ZZZZZZZZZZZZZZZZ"));
+ }
+
+ /**
+ * @tests java.util.Hashtable#containsKey(java.lang.Object)
+ */
+ public void test_containsKeyLjava_lang_Object() {
+ // Test for method boolean
+ // java.util.Hashtable.containsKey(java.lang.Object)
+
+ assertTrue("Failed to find key", htfull.containsKey("FKey 4"));
+ assertTrue("Failed to find key", !htfull.containsKey("FKey 99"));
+ }
+
+ /**
+ * @tests java.util.Hashtable#containsValue(java.lang.Object)
+ */
+ public void test_containsValueLjava_lang_Object() {
+ // Test for method boolean
+ // java.util.Hashtable.containsValue(java.lang.Object)
+ Enumeration e = elmVector.elements();
+ while (e.hasMoreElements())
+ assertTrue("Returned false for valid value", ht10.containsValue(e
+ .nextElement()));
+ assertTrue("Returned true for invalid value", !ht10
+ .containsValue(new Object()));
+ }
+
+ /**
+ * @tests java.util.Hashtable#elements()
+ */
+ public void test_elements() {
+ // Test for method java.util.Enumeration java.util.Hashtable.elements()
+ Enumeration elms = ht10.elements();
+ int i = 0;
+ while (elms.hasMoreElements()) {
+ String s = (String) elms.nextElement();
+ assertTrue("Missing key from enumeration", elmVector.contains(s));
+ ++i;
+ }
+
+ assertTrue("All keys not retrieved", ht10.size() == 10);
+ }
+
+ /**
+ * @tests java.util.Hashtable#elements()
+ */
+ public void test_elements_subtest0() {
+ // this is the reference implementation behavior
+ final Hashtable ht = new Hashtable(7);
+ ht.put("1", "a");
+ // these three elements hash to the same bucket in a 7 element Hashtable
+ ht.put("2", "b");
+ ht.put("9", "c");
+ ht.put("12", "d");
+ // Hashtable looks like:
+ // 0: "1"
+ // 1: "12" -> "9" -> "2"
+ Enumeration en = ht.elements();
+ // cache the first entry
+ en.hasMoreElements();
+ ht.remove("12");
+ ht.remove("9");
+ boolean exception = false;
+ try {
+ // cached "12"
+ Object result = en.nextElement();
+ assertTrue("unexpected: " + result, result == null);
+ // next is removed "9"
+ result = en.nextElement();
+ assertTrue("unexpected: " + result, result == null);
+ result = en.nextElement();
+ assertTrue("unexpected: " + result, "b".equals(result));
+ } catch (NoSuchElementException e) {
+ exception = true;
+ }
+ assertTrue("unexpected NoSuchElementException", !exception);
+ }
+
+ /**
+ * @tests java.util.Hashtable#entrySet()
+ */
+ public void test_entrySet() {
+ // Test for method java.util.Set java.util.Hashtable.entrySet()
+ Set s = ht10.entrySet();
+ Set s2 = new HashSet();
+ Iterator i = s.iterator();
+ while (i.hasNext())
+ s2.add(((Map.Entry) i.next()).getValue());
+ Enumeration e = elmVector.elements();
+ while (e.hasMoreElements())
+ assertTrue("Returned incorrect entry set", s2.contains(e
+ .nextElement()));
+
+ assertTrue("Not synchronized", s.getClass().getName().equals(
+ "java.util.Collections$SynchronizedSet"));
+
+ boolean exception = false;
+ try {
+ ((Map.Entry) ht10.entrySet().iterator().next()).setValue(null);
+ } catch (NullPointerException e1) {
+ exception = true;
+ }
+ assertTrue(
+ "Should not be able to assign null to a Hashtable entrySet() Map.Entry",
+ exception);
+ }
+
+ /**
+ * @tests java.util.Hashtable#equals(java.lang.Object)
+ */
+ public void test_equalsLjava_lang_Object() {
+ // Test for method boolean java.util.Hashtable.equals(java.lang.Object)
+ Hashtable h = hashtableClone(ht10);
+ assertTrue("Returned false for equal tables", ht10.equals(h));
+ assertTrue("Returned true for unequal tables", !ht10.equals(htfull));
+ }
+
+ /**
+ * @tests java.util.Hashtable#get(java.lang.Object)
+ */
+ public void test_getLjava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.util.Hashtable.get(java.lang.Object)
+ Hashtable h = hashtableClone(htfull);
+ assertTrue("Could not retrieve element", ((String) h.get("FKey 2"))
+ .equals("FVal 2"));
+ }
+
+ /**
+ * @tests java.util.Hashtable#hashCode()
+ */
+ public void test_hashCode() {
+ // Test for method int java.util.Hashtable.hashCode()
+ Set entrySet = ht10.entrySet();
+ Iterator iterator = entrySet.iterator();
+ int expectedHash;
+ for (expectedHash = 0; iterator.hasNext(); expectedHash += iterator
+ .next().hashCode())
+ ;
+ assertTrue("Incorrect hashCode returned. Wanted: " + expectedHash
+ + " got: " + ht10.hashCode(), expectedHash == ht10.hashCode());
+ }
+
+ /**
+ * @tests java.util.Hashtable#isEmpty()
+ */
+ public void test_isEmpty() {
+ // Test for method boolean java.util.Hashtable.isEmpty()
+
+ assertTrue("isEmpty returned incorrect value", !ht10.isEmpty());
+ assertTrue("isEmpty returned incorrect value",
+ new java.util.Hashtable().isEmpty());
+
+ final Hashtable ht = new Hashtable();
+ ht.put("0", "");
+ Thread t1 = new Thread() {
+ public void run() {
+ while (!ht.isEmpty())
+ ;
+ ht.put("final", "");
+ }
+ };
+ t1.start();
+ for (int i = 1; i < 10000; i++) {
+ synchronized (ht) {
+ ht.remove(String.valueOf(i - 1));
+ ht.put(String.valueOf(i), "");
+ }
+ int size;
+ if ((size = ht.size()) != 1) {
+ String result = "Size is not 1: " + size + " " + ht;
+ // terminate the thread
+ ht.clear();
+ fail(result);
+ }
+ }
+ // terminate the thread
+ ht.clear();
+ }
+
+ /**
+ * @tests java.util.Hashtable#keys()
+ */
+ public void test_keys() {
+ // Test for method java.util.Enumeration java.util.Hashtable.keys()
+
+ Enumeration keys = ht10.keys();
+ int i = 0;
+ while (keys.hasMoreElements()) {
+ String s = (String) keys.nextElement();
+ assertTrue("Missing key from enumeration", keyVector.contains(s));
+ ++i;
+ }
+
+ assertTrue("All keys not retrieved", ht10.size() == 10);
+ }
+
+ /**
+ * @tests java.util.Hashtable#keys()
+ */
+ public void test_keys_subtest0() {
+ // this is the reference implementation behavior
+ final Hashtable ht = new Hashtable(3);
+ ht.put("initial", "");
+ Enumeration en = ht.keys();
+ en.hasMoreElements();
+ ht.remove("initial");
+ boolean exception = false;
+ try {
+ Object result = en.nextElement();
+ assertTrue("unexpected: " + result, "initial".equals(result));
+ } catch (NoSuchElementException e) {
+ exception = true;
+ }
+ assertTrue("unexpected NoSuchElementException", !exception);
+ }
+
+ /**
+ * @tests java.util.Hashtable#keySet()
+ */
+ public void test_keySet() {
+ // Test for method java.util.Set java.util.Hashtable.keySet()
+ Set s = ht10.keySet();
+ Enumeration e = keyVector.elements();
+ while (e.hasMoreElements())
+ assertTrue("Returned incorrect key set", s
+ .contains(e.nextElement()));
+
+ assertTrue("Not synchronized", s.getClass().getName().equals(
+ "java.util.Collections$SynchronizedSet"));
+
+ Map map = new Hashtable(101);
+ map.put(new Integer(1), "1");
+ map.put(new Integer(102), "102");
+ map.put(new Integer(203), "203");
+ Iterator it = map.keySet().iterator();
+ Integer remove1 = (Integer) it.next();
+ it.remove();
+ Integer remove2 = (Integer) it.next();
+ it.remove();
+ ArrayList list = new ArrayList(Arrays.asList(new Integer[] {
+ new Integer(1), new Integer(102), new Integer(203) }));
+ list.remove(remove1);
+ list.remove(remove2);
+ assertTrue("Wrong result", it.next().equals(list.get(0)));
+ assertTrue("Wrong size", map.size() == 1);
+ assertTrue("Wrong contents", map.keySet().iterator().next().equals(
+ list.get(0)));
+
+ Map map2 = new Hashtable(101);
+ map2.put(new Integer(1), "1");
+ map2.put(new Integer(4), "4");
+ Iterator it2 = map2.keySet().iterator();
+ Integer remove3 = (Integer) it2.next();
+ Integer next;
+ if (remove3.intValue() == 1)
+ next = new Integer(4);
+ else
+ next = new Integer(1);
+ it2.hasNext();
+ it2.remove();
+ assertTrue("Wrong result 2", it2.next().equals(next));
+ assertTrue("Wrong size 2", map2.size() == 1);
+ assertTrue("Wrong contents 2", map2.keySet().iterator().next().equals(
+ next));
+ }
+
+ /**
+ * @tests java.util.Hashtable#keySet()
+ */
+ public void test_keySet_subtest0() {
+ Set s1 = ht10.keySet();
+ assertTrue("should contain key", s1.remove("Key 0"));
+ assertTrue("should not contain key", !s1.remove("Key 0"));
+
+ final int iterations = 10000;
+ final Hashtable ht = new Hashtable();
+ Thread t1 = new Thread() {
+ public void run() {
+ for (int i = 0; i < iterations; i++) {
+ ht.put(String.valueOf(i), "");
+ ht.remove(String.valueOf(i));
+ }
+ }
+ };
+ t1.start();
+ Set set = ht.keySet();
+ for (int i = 0; i < iterations; i++) {
+ Iterator it = set.iterator();
+ try {
+ it.next();
+ it.remove();
+ int size;
+ // ensure removing with the iterator doesn't corrupt the
+ // Hashtable
+ if ((size = ht.size()) < 0) {
+ fail("invalid size: " + size);
+ }
+ } catch (NoSuchElementException e) {
+ } catch (ConcurrentModificationException e) {
+ }
+ }
+ }
+
+ /**
+ * @tests java.util.Hashtable#keySet()
+ */
+ public void test_keySet_subtest1() {
+ // this is the reference implementation behavior
+ final Hashtable ht = new Hashtable(7);
+ ht.put("1", "a");
+ // these three elements hash to the same bucket in a 7 element Hashtable
+ ht.put("2", "b");
+ ht.put("9", "c");
+ ht.put("12", "d");
+ // Hashtable looks like:
+ // 0: "1"
+ // 1: "12" -> "9" -> "2"
+ Enumeration en = ht.elements();
+ // cache the first entry
+ en.hasMoreElements();
+ Iterator it = ht.keySet().iterator();
+ // this is mostly a copy of the test in test_elements_subtest0()
+ // test removing with the iterator does not null the values
+ while (it.hasNext()) {
+ String key = (String) it.next();
+ if ("12".equals(key) || "9".equals(key)) {
+ it.remove();
+ }
+ }
+ it.remove();
+ boolean exception = false;
+ try {
+ // cached "12"
+ Object result = en.nextElement();
+ assertTrue("unexpected: " + result, "d".equals(result));
+ // next is removed "9"
+ result = en.nextElement();
+ assertTrue("unexpected: " + result, "c".equals(result));
+ result = en.nextElement();
+ assertTrue("unexpected: " + result, "b".equals(result));
+ } catch (NoSuchElementException e) {
+ exception = true;
+ }
+ assertTrue("unexpected NoSuchElementException", !exception);
+ }
+
+ /**
+ * @tests java.util.Hashtable#put(java.lang.Object, java.lang.Object)
+ */
+ public void test_putLjava_lang_ObjectLjava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.util.Hashtable.put(java.lang.Object, java.lang.Object)
+ Hashtable h = hashtableClone(ht100);
+ Integer key = new Integer(100);
+ h.put("Value 100", key);
+ assertTrue("Key/Value not inserted", h.size() == 1 && (h.contains(key)));
+
+ // Put into "full" table
+ h = hashtableClone(htfull);
+ h.put("Value 100", key);
+ assertTrue("Key/Value not inserted into full table", h.size() == 8
+ && (h.contains(key)));
+ }
+
+ /**
+ * @tests java.util.Hashtable#putAll(java.util.Map)
+ */
+ public void test_putAllLjava_util_Map() {
+ // Test for method void java.util.Hashtable.putAll(java.util.Map)
+ Hashtable h = new Hashtable();
+ h.putAll(ht10);
+ Enumeration e = keyVector.elements();
+ while (e.hasMoreElements()) {
+ Object x = e.nextElement();
+ assertTrue("Failed to put all elements", h.get(x).equals(
+ ht10.get(x)));
+ }
+ }
+
+ /**
+ * @tests java.util.Hashtable#remove(java.lang.Object)
+ */
+ public void test_removeLjava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.util.Hashtable.remove(java.lang.Object)
+ Hashtable h = hashtableClone(htfull);
+ Object k = h.remove("FKey 0");
+ assertTrue("Remove failed", !h.containsKey("FKey 0") || k == null);
+ }
+
+ /**
+ * @tests java.util.Hashtable#size()
+ */
+ public void test_size() {
+ // Test for method int java.util.Hashtable.size()
+ assertTrue("Returned invalid size", ht10.size() == 10
+ && (ht100.size() == 0));
+
+ final Hashtable ht = new Hashtable();
+ ht.put("0", "");
+ Thread t1 = new Thread() {
+ public void run() {
+ while (ht.size() > 0)
+ ;
+ ht.put("final", "");
+ }
+ };
+ t1.start();
+ for (int i = 1; i < 10000; i++) {
+ synchronized (ht) {
+ ht.remove(String.valueOf(i - 1));
+ ht.put(String.valueOf(i), "");
+ }
+ int size;
+ if ((size = ht.size()) != 1) {
+ String result = "Size is not 1: " + size + " " + ht;
+ // terminate the thread
+ ht.clear();
+ fail(result);
+ }
+ }
+ // terminate the thread
+ ht.clear();
+ }
+
+ /**
+ * @tests java.util.Hashtable#toString()
+ */
+ public void test_toString() {
+ // Test for method java.lang.String java.util.Hashtable.toString()
+ Hashtable h = new Hashtable();
+ assertTrue("Incorrect toString for Empty table", h.toString().equals(
+ "{}"));
+
+ h.put("one", "1");
+ h.put("two", h);
+ h.put(h, "3");
+ h.put(h, h);
+ String result = h.toString();
+ assertTrue("should contain self ref", result.indexOf("(this") > -1);
+ }
+
+ /**
+ * @tests java.util.Hashtable#values()
+ */
+ public void test_values() {
+ // Test for method java.util.Collection java.util.Hashtable.values()
+ Collection c = ht10.values();
+ Enumeration e = elmVector.elements();
+ while (e.hasMoreElements())
+ assertTrue("Returned incorrect values", c.contains(e.nextElement()));
+
+ assertTrue("Not synchronized", c.getClass().getName().equals(
+ "java.util.Collections$SynchronizedCollection"));
+
+ Hashtable myHashtable = new Hashtable();
+ for (int i = 0; i < 100; i++)
+ myHashtable.put(new Integer(i), new Integer(i));
+ Collection values = myHashtable.values();
+ new Support_UnmodifiableCollectionTest(
+ "Test Returned Collection From Hashtable.values()", values)
+ .runTest();
+ values.remove(new Integer(0));
+ assertTrue(
+ "Removing from the values collection should remove from the original map",
+ !myHashtable.containsValue(new Integer(0)));
+ }
+
+ protected Hashtable hashtableClone(Hashtable s) {
+ return (Hashtable) s.clone();
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+
+ ht10 = new Hashtable(10);
+ ht100 = new Hashtable(100);
+ htfull = new Hashtable(10);
+ keyVector = new Vector(10);
+ elmVector = new Vector(10);
+
+ for (int i = 0; i < 10; i++) {
+ ht10.put("Key " + i, "Val " + i);
+ keyVector.addElement("Key " + i);
+ elmVector.addElement("Val " + i);
+ }
+
+ for (int i = 0; i < 7; i++)
+ htfull.put("FKey " + i, "FVal " + i);
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/IdentityHashMap2Test.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/IdentityHashMap2Test.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/IdentityHashMap2Test.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/IdentityHashMap2Test.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,317 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.util;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+public class IdentityHashMap2Test extends junit.framework.TestCase {
+
+ /**
+ * @tests java.util.IdentityHashMap#containsKey(java.lang.Object)
+ * @tests java.util.IdentityHashMap#containsValue(java.lang.Object)
+ * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
+ * @tests java.util.IdentityHashMap#get(java.lang.Object)
+ */
+ public void test_null_Keys_and_Values() {
+ // tests with null keys and values
+ IdentityHashMap map = new IdentityHashMap();
+ Object result;
+
+ // null key and null value
+ result = map.put(null, null);
+ assertTrue("testA can not find null key", map.containsKey(null));
+ assertTrue("testA can not find null value", map.containsValue(null));
+ assertTrue("testA can not get null value for null key",
+ map.get(null) == null);
+ assertTrue("testA put returned wrong value", result == null);
+
+ // null value
+ String value = "a value";
+ result = map.put(null, value);
+ assertTrue("testB can not find null key", map.containsKey(null));
+ assertTrue("testB can not find a value with null key", map
+ .containsValue(value));
+ assertTrue("testB can not get value for null key",
+ map.get(null) == value);
+ assertTrue("testB put returned wrong value", result == null);
+
+ // a null key
+ String key = "a key";
+ result = map.put(key, null);
+ assertTrue("testC can not find a key with null value", map
+ .containsKey(key));
+ assertTrue("testC can not find null value", map.containsValue(null));
+ assertTrue("testC can not get null value for key", map.get(key) == null);
+ assertTrue("testC put returned wrong value", result == null);
+
+ // another null key
+ String anothervalue = "another value";
+ result = map.put(null, anothervalue);
+ assertTrue("testD can not find null key", map.containsKey(null));
+ assertTrue("testD can not find a value with null key", map
+ .containsValue(anothervalue));
+ assertTrue("testD can not get value for null key",
+ map.get(null) == anothervalue);
+ assertTrue("testD put returned wrong value", result == value);
+
+ // remove a null key
+ result = map.remove(null);
+ assertTrue("testE remove returned wrong value", result == anothervalue);
+ assertTrue("testE should not find null key", !map.containsKey(null));
+ assertTrue("testE should not find a value with null key", !map
+ .containsValue(anothervalue));
+ assertTrue("testE should not get value for null key",
+ map.get(null) == null);
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#remove(java.lang.Object)
+ * @tests java.util.IdentityHashMap#keySet()
+ */
+ public void test_remove() {
+ IdentityHashMap map = new IdentityHashMap();
+ map.put(null, null);
+ map.put("key1", "value1");
+ map.put("key2", "value2");
+ map.remove("key1");
+
+ assertTrue("Did not remove key1", !map.containsKey("key1"));
+ assertTrue("Did not remove the value for key1", !map
+ .containsValue("value1"));
+
+ assertTrue("Modified key2", map.get("key2") != null
+ && map.get("key2") == "value2");
+ assertTrue("Modified null entry", map.get(null) == null);
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#entrySet()
+ * @tests java.util.IdentityHashMap#keySet()
+ * @tests java.util.IdentityHashMap#values()
+ */
+ public void test_sets() {
+ // tests with null keys and values
+ IdentityHashMap map = new IdentityHashMap();
+
+ // null key and null value
+ map.put("key", "value");
+ map.put(null, null);
+ map.put("a key", null);
+ map.put("another key", null);
+
+ Set keyset = map.keySet();
+ Collection valueset = map.values();
+ Set entries = map.entrySet();
+ Iterator it = entries.iterator();
+ while (it.hasNext()) {
+ Map.Entry entry = (Map.Entry) it.next();
+ assertTrue("EntrySetIterator can not find entry ", entries
+ .contains(entry));
+
+ assertTrue("entry key not found in map", map.containsKey(entry
+ .getKey()));
+ assertTrue("entry value not found in map", map.containsValue(entry
+ .getValue()));
+
+ assertTrue("entry key not found in the keyset", keyset
+ .contains(entry.getKey()));
+ assertTrue("entry value not found in the valueset", valueset
+ .contains(entry.getValue()));
+ }
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#entrySet()
+ * @tests java.util.IdentityHashMap#remove(java.lang.Object)
+ */
+ public void test_entrySet_removeAll() {
+ IdentityHashMap map = new IdentityHashMap();
+ for (int i = 0; i < 1000; i++) {
+ map.put(new Integer(i), new Integer(i));
+ }
+ Set set = map.entrySet();
+
+ set.removeAll(set);
+ assertTrue("did not remove all elements in the map", map.size() == 0);
+ assertTrue("did not remove all elements in the entryset", set.isEmpty());
+
+ Iterator it = set.iterator();
+ assertTrue("entrySet iterator still has elements", !it.hasNext());
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#keySet()
+ * @tests java.util.IdentityHashMap#clear()
+ */
+ public void test_keySet_clear() {
+ IdentityHashMap map = new IdentityHashMap();
+ for (int i = 0; i < 1000; i++) {
+ map.put(new Integer(i), new Integer(i));
+ }
+ Set set = map.keySet();
+ set.clear();
+
+ assertTrue("did not remove all elements in the map", map.size() == 0);
+ assertTrue("did not remove all elements in the keyset", set.isEmpty());
+
+ Iterator it = set.iterator();
+ assertTrue("keySet iterator still has elements", !it.hasNext());
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#values()
+ */
+ public void test_values() {
+
+ IdentityHashMap map = new IdentityHashMap();
+ for (int i = 0; i < 10; i++) {
+ map.put(new Integer(i), new Integer(i));
+ }
+
+ Integer key = new Integer(20);
+ Integer value = new Integer(40);
+ map.put(key, value);
+
+ Collection vals = map.values();
+ boolean result = vals.remove(key);
+ assertTrue("removed entries incorrectly", map.size() == 11 && !result);
+ assertTrue("removed key incorrectly", map.containsKey(key));
+ assertTrue("removed value incorrectly", map.containsValue(value));
+
+ result = vals.remove(value);
+ assertTrue("Did not remove entry as expected", map.size() == 10
+ && result);
+ assertTrue("Did not remove key as expected", !map.containsKey(key));
+ assertTrue("Did not remove value as expected", !map
+ .containsValue(value));
+
+ // put an equivalent key to a value
+ key = new Integer(1);
+ value = new Integer(100);
+ map.put(key, value);
+
+ result = vals.remove(key);
+ assertTrue("TestB. removed entries incorrectly", map.size() == 11
+ && !result);
+ assertTrue("TestB. removed key incorrectly", map.containsKey(key));
+ assertTrue("TestB. removed value incorrectly", map.containsValue(value));
+
+ result = vals.remove(value);
+ assertTrue("TestB. Did not remove entry as expected", map.size() == 10
+ && result);
+ assertTrue("TestB. Did not remove key as expected", !map
+ .containsKey(key));
+ assertTrue("TestB. Did not remove value as expected", !map
+ .containsValue(value));
+
+ vals.clear();
+ assertTrue("Did not remove all entries as expected", map.size() == 0);
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#keySet()
+ * @tests java.util.IdentityHashMap#remove(java.lang.Object)
+ */
+ public void test_keySet_removeAll() {
+ IdentityHashMap map = new IdentityHashMap();
+ for (int i = 0; i < 1000; i++) {
+ map.put(new Integer(i), new Integer(i));
+ }
+ Set set = map.keySet();
+ set.removeAll(set);
+
+ assertTrue("did not remove all elements in the map", map.size() == 0);
+ assertTrue("did not remove all elements in the keyset", set.isEmpty());
+
+ Iterator it = set.iterator();
+ assertTrue("keySet iterator still has elements", !it.hasNext());
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#keySet()
+ */
+ public void test_keySet_retainAll() {
+ IdentityHashMap map = new IdentityHashMap();
+ for (int i = 0; i < 1000; i++) {
+ map.put(new Integer(i), new Integer(i));
+ }
+ Set set = map.keySet();
+
+ // retain all the elements
+ boolean result = set.retainAll(set);
+ assertTrue("retain all should return false", !result);
+ assertTrue("did not retain all", set.size() == 1000);
+
+ // send empty set to retainAll
+ result = set.retainAll(new TreeSet());
+ assertTrue("retain all should return true", result);
+ assertTrue("did not remove all elements in the map", map.size() == 0);
+ assertTrue("did not remove all elements in the keyset", set.isEmpty());
+
+ Iterator it = set.iterator();
+ assertTrue("keySet iterator still has elements", !it.hasNext());
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#keySet()
+ * @tests java.util.IdentityHashMap#remove(java.lang.Object)
+ */
+ public void test_keyset_remove() {
+ IdentityHashMap map = new IdentityHashMap();
+
+ Integer key = new Integer(21);
+
+ map.put(new Integer(1), null);
+ map.put(new Integer(11), null);
+ map.put(key, null);
+ map.put(new Integer(31), null);
+ map.put(new Integer(41), null);
+ map.put(new Integer(51), null);
+ map.put(new Integer(61), null);
+ map.put(new Integer(71), null);
+ map.put(new Integer(81), null);
+ map.put(new Integer(91), null);
+
+ Set set = map.keySet();
+
+ Set newset = new HashSet();
+ Iterator it = set.iterator();
+ while (it.hasNext()) {
+ Object element = it.next();
+ if (element == key) {
+ it.remove();
+ } else
+ newset.add(element);
+ }
+ int size = newset.size();
+ assertTrue("keyset and newset don't have same size",
+ newset.size() == size);
+ assertTrue("element is in newset ", !newset.contains(key));
+ assertTrue("element not removed from keyset", !set.contains(key));
+ assertTrue("element not removed from map", !map.containsKey(key));
+
+ assertTrue("newset and keyset do not have same elements 1", newset
+ .equals(set));
+ assertTrue("newset and keyset do not have same elements 2", set
+ .equals(newset));
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/IdentityHashMapTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/IdentityHashMapTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/IdentityHashMapTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/IdentityHashMapTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,428 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.util;
+
+import java.util.AbstractMap;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.IdentityHashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import tests.support.Support_MapTest2;
+
+public class IdentityHashMapTest extends junit.framework.TestCase {
+
+ /*
+ * TODO: change all the statements testing the keys and values with equals()
+ * method to check for reference equality instead
+ */
+
+ IdentityHashMap hm;
+
+ final static int hmSize = 1000;
+
+ static Object[] objArray;
+
+ static Object[] objArray2;
+ {
+ objArray = new Object[hmSize];
+ objArray2 = new Object[hmSize];
+ for (int i = 0; i < objArray.length; i++) {
+ objArray[i] = new Integer(i);
+ objArray2[i] = objArray[i].toString();
+ }
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#IdentityHashMap()
+ */
+ public void test_Constructor() {
+ // Test for method java.util.IdentityHashMap()
+ new Support_MapTest2(new IdentityHashMap()).runTest();
+
+ IdentityHashMap hm2 = new IdentityHashMap();
+ assertTrue("Created incorrect IdentityHashMap", hm2.size() == 0);
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#IdentityHashMap(int)
+ */
+ public void test_ConstructorI() {
+ // Test for method java.util.IdentityHashMap(int)
+ IdentityHashMap hm2 = new IdentityHashMap(5);
+ assertTrue("Created incorrect IdentityHashMap", hm2.size() == 0);
+ try {
+ new IdentityHashMap(-1);
+ } catch (IllegalArgumentException e) {
+ return;
+ }
+ fail(
+ "Failed to throw IllegalArgumentException for initial capacity < 0");
+
+ IdentityHashMap empty = new IdentityHashMap(0);
+ assertTrue("Empty IdentityHashMap access", empty.get("nothing") == null);
+ empty.put("something", "here");
+ assertTrue("cannot get element", empty.get("something") == "here");
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#IdentityHashMap(java.util.Map)
+ */
+ public void test_ConstructorLjava_util_Map() {
+ // Test for method java.util.IdentityHashMap(java.util.Map)
+ Map myMap = new TreeMap();
+ for (int counter = 0; counter < hmSize; counter++)
+ myMap.put(objArray2[counter], objArray[counter]);
+ IdentityHashMap hm2 = new IdentityHashMap(myMap);
+ for (int counter = 0; counter < hmSize; counter++)
+ assertTrue("Failed to construct correct IdentityHashMap", hm
+ .get(objArray2[counter]) == hm2.get(objArray2[counter]));
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#clear()
+ */
+ public void test_clear() {
+ // Test for method void java.util.IdentityHashMap.clear()
+ hm.clear();
+ assertTrue("Clear failed to reset size", hm.size() == 0);
+ for (int i = 0; i < hmSize; i++)
+ assertTrue("Failed to clear all elements",
+ hm.get(objArray2[i]) == null);
+
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#clone()
+ */
+ public void test_clone() {
+ // Test for method java.lang.Object java.util.IdentityHashMap.clone()
+ IdentityHashMap hm2 = (IdentityHashMap) hm.clone();
+ assertTrue("Clone answered equivalent IdentityHashMap", hm2 != hm);
+ for (int counter = 0; counter < hmSize; counter++)
+ assertTrue("Clone answered unequal IdentityHashMap", hm
+ .get(objArray2[counter]) == hm2.get(objArray2[counter]));
+
+ IdentityHashMap map = new IdentityHashMap();
+ map.put("key", "value");
+ // get the keySet() and values() on the original Map
+ Set keys = map.keySet();
+ Collection values = map.values();
+ assertTrue("values() does not work", values.iterator().next().equals(
+ "value"));
+ assertTrue("keySet() does not work", keys.iterator().next().equals(
+ "key"));
+ AbstractMap map2 = (AbstractMap) map.clone();
+ map2.put("key", "value2");
+ Collection values2 = map2.values();
+ assertTrue("values() is identical", values2 != values);
+ // values() and keySet() on the cloned() map should be different
+ assertTrue("values() was not cloned", values2.iterator().next().equals(
+ "value2"));
+ map2.clear();
+ map2.put("key2", "value3");
+ Set key2 = map2.keySet();
+ assertTrue("keySet() is identical", key2 != keys);
+ assertTrue("keySet() was not cloned", key2.iterator().next().equals(
+ "key2"));
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#containsKey(java.lang.Object)
+ */
+ public void test_containsKeyLjava_lang_Object() {
+ // Test for method boolean
+ // java.util.IdentityHashMap.containsKey(java.lang.Object)
+ assertTrue("Returned false for valid key", hm
+ .containsKey(objArray2[23]));
+ assertTrue("Returned true for copy of valid key", !hm
+ .containsKey(new Integer(23).toString()));
+ assertTrue("Returned true for invalid key", !hm.containsKey("KKDKDKD"));
+
+ IdentityHashMap m = new IdentityHashMap();
+ m.put(null, "test");
+ assertTrue("Failed with null key", m.containsKey(null));
+ assertTrue("Failed with missing key matching null hash", !m
+ .containsKey(new Integer(0)));
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#containsValue(java.lang.Object)
+ */
+ public void test_containsValueLjava_lang_Object() {
+ // Test for method boolean
+ // java.util.IdentityHashMap.containsValue(java.lang.Object)
+ assertTrue("Returned false for valid value", hm
+ .containsValue(objArray[19]));
+ assertTrue("Returned true for invalid valie", !hm
+ .containsValue(new Integer(-9)));
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#entrySet()
+ */
+ public void test_entrySet() {
+ // Test for method java.util.Set java.util.IdentityHashMap.entrySet()
+ Set s = hm.entrySet();
+ Iterator i = s.iterator();
+ assertTrue("Returned set of incorrect size", hm.size() == s.size());
+ while (i.hasNext()) {
+ Map.Entry m = (Map.Entry) i.next();
+ assertTrue("Returned incorrect entry set", hm.containsKey(m
+ .getKey())
+ && hm.containsValue(m.getValue()));
+ }
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#get(java.lang.Object)
+ */
+ public void test_getLjava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.util.IdentityHashMap.get(java.lang.Object)
+ assertTrue("Get returned non-null for non existent key",
+ hm.get("T") == null);
+ hm.put("T", "HELLO");
+ assertTrue("Get returned incorecct value for existing key", hm.get("T")
+ .equals("HELLO"));
+
+ IdentityHashMap m = new IdentityHashMap();
+ m.put(null, "test");
+ assertTrue("Failed with null key", m.get(null).equals("test"));
+ assertTrue("Failed with missing key matching null hash", m
+ .get(new Integer(0)) == null);
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#isEmpty()
+ */
+ public void test_isEmpty() {
+ // Test for method boolean java.util.IdentityHashMap.isEmpty()
+ assertTrue("Returned false for new map", new IdentityHashMap()
+ .isEmpty());
+ assertTrue("Returned true for non-empty", !hm.isEmpty());
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#keySet()
+ */
+ public void test_keySet() {
+ // Test for method java.util.Set java.util.IdentityHashMap.keySet()
+ Set s = hm.keySet();
+ assertTrue("Returned set of incorrect size()", s.size() == hm.size());
+ for (int i = 0; i < objArray.length; i++) {
+ assertTrue("Returned set does not contain all keys", s
+ .contains(objArray2[i]));
+ }
+
+ IdentityHashMap m = new IdentityHashMap();
+ m.put(null, "test");
+ assertTrue("Failed with null key", m.keySet().contains(null));
+ assertTrue("Failed with null key", m.keySet().iterator().next() == null);
+
+ Map map = new IdentityHashMap(101);
+ map.put(new Integer(1), "1");
+ map.put(new Integer(102), "102");
+ map.put(new Integer(203), "203");
+ Iterator it = map.keySet().iterator();
+ Integer remove1 = (Integer) it.next();
+ it.hasNext();
+ it.remove();
+ Integer remove2 = (Integer) it.next();
+ it.remove();
+ ArrayList list = new ArrayList(Arrays.asList(new Integer[] {
+ new Integer(1), new Integer(102), new Integer(203) }));
+ list.remove(remove1);
+ list.remove(remove2);
+ assertTrue("Wrong result", it.next().equals(list.get(0)));
+ assertTrue("Wrong size", map.size() == 1);
+ assertTrue("Wrong contents", map.keySet().iterator().next().equals(
+ list.get(0)));
+
+ Map map2 = new IdentityHashMap(101);
+ map2.put(new Integer(1), "1");
+ map2.put(new Integer(4), "4");
+ Iterator it2 = map2.keySet().iterator();
+ Integer remove3 = (Integer) it2.next();
+ Integer next;
+ if (remove3.intValue() == 1)
+ next = new Integer(4);
+ else
+ next = new Integer(1);
+ it2.hasNext();
+ it2.remove();
+ assertTrue("Wrong result 2", it2.next().equals(next));
+ assertTrue("Wrong size 2", map2.size() == 1);
+ assertTrue("Wrong contents 2", map2.keySet().iterator().next().equals(
+ next));
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
+ */
+ public void test_putLjava_lang_ObjectLjava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.util.IdentityHashMap.put(java.lang.Object, java.lang.Object)
+ hm.put("KEY", "VALUE");
+ assertTrue("Failed to install key/value pair", hm.get("KEY").equals(
+ "VALUE"));
+
+ IdentityHashMap m = new IdentityHashMap();
+ Short s0 = new Short((short) 0);
+ m.put(s0, "short");
+ m.put(null, "test");
+ Integer i0 = new Integer(0);
+ m.put(i0, "int");
+ assertTrue("Failed adding to bucket containing null", m.get(s0).equals(
+ "short"));
+ assertTrue("Failed adding to bucket containing null2", m.get(i0)
+ .equals("int"));
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#putAll(java.util.Map)
+ */
+ public void test_putAllLjava_util_Map() {
+ // Test for method void java.util.IdentityHashMap.putAll(java.util.Map)
+ IdentityHashMap hm2 = new IdentityHashMap();
+ hm2.putAll(hm);
+ for (int i = 0; i < 1000; i++)
+ assertTrue("Failed to clear all elements", hm2.get(objArray2[i])
+ .equals((new Integer(i))));
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#remove(java.lang.Object)
+ */
+ public void test_removeLjava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.util.IdentityHashMap.remove(java.lang.Object)
+ int size = hm.size();
+ Integer x = ((Integer) hm.remove(objArray2[9]));
+ assertTrue("Remove returned incorrect value", x.equals(new Integer(9)));
+ assertTrue("Failed to remove given key", hm.get(objArray2[9]) == null);
+ assertTrue("Failed to decrement size", hm.size() == (size - 1));
+ assertTrue("Remove of non-existent key returned non-null", hm
+ .remove("LCLCLC") == null);
+
+ IdentityHashMap m = new IdentityHashMap();
+ m.put(null, "test");
+ assertTrue("Failed with same hash as null",
+ m.remove(objArray[0]) == null);
+ assertTrue("Failed with null key", m.remove(null).equals("test"));
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#size()
+ */
+ public void test_size() {
+ // Test for method int java.util.IdentityHashMap.size()
+ assertEquals("Returned incorrect size, ", (objArray.length + 2), hm
+ .size());
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#equals(java.lang.Object)
+ */
+ public void test_equalsLjava_lang_Object() {
+ IdentityHashMap mapOne = new IdentityHashMap();
+ IdentityHashMap mapTwo = new IdentityHashMap();
+ IdentityHashMap mapThree = new IdentityHashMap();
+ IdentityHashMap mapFour = new IdentityHashMap();
+
+ String one = "one";
+ String alsoOne = new String(one); // use the new operator to ensure a
+ // new reference is constructed
+ String two = "two";
+ String alsoTwo = new String(two); // use the new operator to ensure a
+ // new reference is constructed
+
+ mapOne.put(one, two);
+ mapFour.put(one, two);
+
+ // these two are not equal to the above two
+ mapTwo.put(alsoOne, two);
+ mapThree.put(one, alsoTwo);
+
+ assertEquals("failure of equality of IdentityHashMaps", mapOne, mapFour);
+ assertTrue("failure of non-equality of IdentityHashMaps one and two",
+ !mapOne.equals(mapTwo));
+ assertTrue("failure of non-equality of IdentityHashMaps one and three",
+ !mapOne.equals(mapThree));
+ assertTrue("failure of non-equality of IdentityHashMaps two and three",
+ !mapTwo.equals(mapThree));
+
+ HashMap hashMapTwo = new HashMap();
+ HashMap hashMapThree = new HashMap();
+ hashMapTwo.put(alsoOne, two);
+ hashMapThree.put(one, alsoTwo);
+
+ assertTrue(
+ "failure of non-equality of IdentityHashMaps one and Hashmap two",
+ !mapOne.equals(hashMapTwo));
+ assertTrue(
+ "failure of non-equality of IdentityHashMaps one and Hashmap three",
+ !mapOne.equals(hashMapThree));
+ }
+
+ /**
+ * @tests java.util.IdentityHashMap#values()
+ */
+ public void test_values() {
+ // Test for method java.util.Collection
+ // java.util.IdentityHashMap.values()
+ Collection c = hm.values();
+ assertTrue("Returned collection of incorrect size()", c.size() == hm
+ .size());
+ for (int i = 0; i < objArray.length; i++)
+ assertTrue("Returned collection does not contain all keys", c
+ .contains(objArray[i]));
+
+ IdentityHashMap myIdentityHashMap = new IdentityHashMap();
+ for (int i = 0; i < 100; i++)
+ myIdentityHashMap.put(objArray2[i], objArray[i]);
+ Collection values = myIdentityHashMap.values();
+ values.remove(objArray[0]);
+ assertTrue(
+ "Removing from the values collection should remove from the original map",
+ !myIdentityHashMap.containsValue(objArray2[0]));
+
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ hm = new IdentityHashMap();
+ for (int i = 0; i < objArray.length; i++)
+ hm.put(objArray2[i], objArray[i]);
+ hm.put("test", null);
+ hm.put(null, "test");
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/LinkedHashMapTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/LinkedHashMapTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/LinkedHashMapTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/LinkedHashMapTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,602 @@
+/* Copyright 2004, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.util;
+
+import java.util.AbstractMap;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import tests.support.Support_MapTest2;
+import tests.support.Support_UnmodifiableCollectionTest;
+
+/**
+ * @tests java.util.LinkedHashMap
+ */
+public class LinkedHashMapTest extends junit.framework.TestCase {
+
+ LinkedHashMap hm;
+
+ final static int hmSize = 1000;
+
+ static Object[] objArray;
+
+ static Object[] objArray2;
+ {
+ objArray = new Object[hmSize];
+ objArray2 = new Object[hmSize];
+ for (int i = 0; i < objArray.length; i++) {
+ objArray[i] = new Integer(i);
+ objArray2[i] = objArray[i].toString();
+ }
+ }
+
+ static final class CacheMap extends LinkedHashMap {
+ protected boolean removeEldestEntry(Map.Entry e) {
+ return size() > 5;
+ }
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#LinkedHashMap()
+ */
+ public void test_Constructor() {
+ // Test for method java.util.LinkedHashMap()
+ new Support_MapTest2(new LinkedHashMap()).runTest();
+
+ LinkedHashMap hm2 = new LinkedHashMap();
+ assertTrue("Created incorrect LinkedHashMap", hm2.size() == 0);
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#LinkedHashMap(int)
+ */
+ public void test_ConstructorI() {
+ // Test for method java.util.LinkedHashMap(int)
+ LinkedHashMap hm2 = new LinkedHashMap(5);
+ assertTrue("Created incorrect LinkedHashMap", hm2.size() == 0);
+ try {
+ new LinkedHashMap(-1);
+ } catch (IllegalArgumentException e) {
+ return;
+ }
+ fail(
+ "Failed to throw IllegalArgumentException for initial capacity < 0");
+
+ LinkedHashMap empty = new LinkedHashMap(0);
+ assertTrue("Empty LinkedHashMap access", empty.get("nothing") == null);
+ empty.put("something", "here");
+ assertTrue("cannot get element", empty.get("something") == "here");
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#LinkedHashMap(int, float)
+ */
+ public void test_ConstructorIF() {
+ // Test for method java.util.LinkedHashMap(int, float)
+ LinkedHashMap hm2 = new LinkedHashMap(5, (float) 0.5);
+ assertTrue("Created incorrect LinkedHashMap", hm2.size() == 0);
+ try {
+ new LinkedHashMap(0, 0);
+ } catch (IllegalArgumentException e) {
+ return;
+ }
+ fail(
+ "Failed to throw IllegalArgumentException for initial load factor <= 0");
+ LinkedHashMap empty = new LinkedHashMap(0, 0.75f);
+ assertTrue("Empty hashtable access", empty.get("nothing") == null);
+ empty.put("something", "here");
+ assertTrue("cannot get element", empty.get("something") == "here");
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#LinkedHashMap(java.util.Map)
+ */
+ public void test_ConstructorLjava_util_Map() {
+ // Test for method java.util.LinkedHashMap(java.util.Map)
+ Map myMap = new TreeMap();
+ for (int counter = 0; counter < hmSize; counter++)
+ myMap.put(objArray2[counter], objArray[counter]);
+ LinkedHashMap hm2 = new LinkedHashMap(myMap);
+ for (int counter = 0; counter < hmSize; counter++)
+ assertTrue("Failed to construct correct LinkedHashMap", hm
+ .get(objArray2[counter]) == hm2.get(objArray2[counter]));
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#get(java.lang.Object)
+ */
+ public void test_getLjava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.util.LinkedHashMap.get(java.lang.Object)
+ assertTrue("Get returned non-null for non existent key",
+ hm.get("T") == null);
+ hm.put("T", "HELLO");
+ assertTrue("Get returned incorecct value for existing key", hm.get("T")
+ .equals("HELLO"));
+
+ LinkedHashMap m = new LinkedHashMap();
+ m.put(null, "test");
+ assertTrue("Failed with null key", m.get(null).equals("test"));
+ assertTrue("Failed with missing key matching null hash", m
+ .get(new Integer(0)) == null);
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#put(java.lang.Object, java.lang.Object)
+ */
+ public void test_putLjava_lang_ObjectLjava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.util.LinkedHashMap.put(java.lang.Object, java.lang.Object)
+ hm.put("KEY", "VALUE");
+ assertTrue("Failed to install key/value pair", hm.get("KEY").equals(
+ "VALUE"));
+
+ LinkedHashMap m = new LinkedHashMap();
+ m.put(new Short((short) 0), "short");
+ m.put(null, "test");
+ m.put(new Integer(0), "int");
+ assertTrue("Failed adding to bucket containing null", m.get(
+ new Short((short) 0)).equals("short"));
+ assertTrue("Failed adding to bucket containing null2", m.get(
+ new Integer(0)).equals("int"));
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#putAll(java.util.Map)
+ */
+ public void test_putAllLjava_util_Map() {
+ // Test for method void java.util.LinkedHashMap.putAll(java.util.Map)
+ LinkedHashMap hm2 = new LinkedHashMap();
+ hm2.putAll(hm);
+ for (int i = 0; i < 1000; i++)
+ assertTrue("Failed to clear all elements", hm2.get(
+ new Integer(i).toString()).equals((new Integer(i))));
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#entrySet()
+ */
+ public void test_entrySet() {
+ // Test for method java.util.Set java.util.LinkedHashMap.entrySet()
+ Set s = hm.entrySet();
+ Iterator i = s.iterator();
+ assertTrue("Returned set of incorrect size", hm.size() == s.size());
+ while (i.hasNext()) {
+ Map.Entry m = (Map.Entry) i.next();
+ assertTrue("Returned incorrect entry set", hm.containsKey(m
+ .getKey())
+ && hm.containsValue(m.getValue()));
+ }
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#keySet()
+ */
+ public void test_keySet() {
+ // Test for method java.util.Set java.util.LinkedHashMap.keySet()
+ Set s = hm.keySet();
+ assertTrue("Returned set of incorrect size()", s.size() == hm.size());
+ for (int i = 0; i < objArray.length; i++)
+ assertTrue("Returned set does not contain all keys", s
+ .contains(objArray[i].toString()));
+
+ LinkedHashMap m = new LinkedHashMap();
+ m.put(null, "test");
+ assertTrue("Failed with null key", m.keySet().contains(null));
+ assertTrue("Failed with null key", m.keySet().iterator().next() == null);
+
+ Map map = new LinkedHashMap(101);
+ map.put(new Integer(1), "1");
+ map.put(new Integer(102), "102");
+ map.put(new Integer(203), "203");
+ Iterator it = map.keySet().iterator();
+ Integer remove1 = (Integer) it.next();
+ it.hasNext();
+ it.remove();
+ Integer remove2 = (Integer) it.next();
+ it.remove();
+ ArrayList list = new ArrayList(Arrays.asList(new Integer[] {
+ new Integer(1), new Integer(102), new Integer(203) }));
+ list.remove(remove1);
+ list.remove(remove2);
+ assertTrue("Wrong result", it.next().equals(list.get(0)));
+ assertTrue("Wrong size", map.size() == 1);
+ assertTrue("Wrong contents", map.keySet().iterator().next().equals(
+ list.get(0)));
+
+ Map map2 = new LinkedHashMap(101);
+ map2.put(new Integer(1), "1");
+ map2.put(new Integer(4), "4");
+ Iterator it2 = map2.keySet().iterator();
+ Integer remove3 = (Integer) it2.next();
+ Integer next;
+ if (remove3.intValue() == 1)
+ next = new Integer(4);
+ else
+ next = new Integer(1);
+ it2.hasNext();
+ it2.remove();
+ assertTrue("Wrong result 2", it2.next().equals(next));
+ assertTrue("Wrong size 2", map2.size() == 1);
+ assertTrue("Wrong contents 2", map2.keySet().iterator().next().equals(
+ next));
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#values()
+ */
+ public void test_values() {
+ // Test for method java.util.Collection java.util.LinkedHashMap.values()
+ Collection c = hm.values();
+ assertTrue("Returned collection of incorrect size()", c.size() == hm
+ .size());
+ for (int i = 0; i < objArray.length; i++)
+ assertTrue("Returned collection does not contain all keys", c
+ .contains(objArray[i]));
+
+ LinkedHashMap myLinkedHashMap = new LinkedHashMap();
+ for (int i = 0; i < 100; i++)
+ myLinkedHashMap.put(objArray2[i], objArray[i]);
+ Collection values = myLinkedHashMap.values();
+ new Support_UnmodifiableCollectionTest(
+ "Test Returned Collection From LinkedHashMap.values()", values)
+ .runTest();
+ values.remove(new Integer(0));
+ assertTrue(
+ "Removing from the values collection should remove from the original map",
+ !myLinkedHashMap.containsValue(new Integer(0)));
+
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#remove(java.lang.Object)
+ */
+ public void test_removeLjava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.util.LinkedHashMap.remove(java.lang.Object)
+ int size = hm.size();
+ Integer y = new Integer(9);
+ Integer x = ((Integer) hm.remove(y.toString()));
+ assertTrue("Remove returned incorrect value", x.equals(new Integer(9)));
+ assertTrue("Failed to remove given key", hm.get(new Integer(9)) == null);
+ assertTrue("Failed to decrement size", hm.size() == (size - 1));
+ assertTrue("Remove of non-existent key returned non-null", hm
+ .remove("LCLCLC") == null);
+
+ LinkedHashMap m = new LinkedHashMap();
+ m.put(null, "test");
+ assertTrue("Failed with same hash as null",
+ m.remove(new Integer(0)) == null);
+ assertTrue("Failed with null key", m.remove(null).equals("test"));
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#clear()
+ */
+ public void test_clear() {
+ // Test for method void java.util.LinkedHashMap.clear()
+ hm.clear();
+ assertTrue("Clear failed to reset size", hm.size() == 0);
+ for (int i = 0; i < hmSize; i++)
+ assertTrue("Failed to clear all elements",
+ hm.get(objArray2[i]) == null);
+
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#clone()
+ */
+ public void test_clone() {
+ // Test for method java.lang.Object java.util.LinkedHashMap.clone()
+ LinkedHashMap hm2 = (LinkedHashMap) hm.clone();
+ assertTrue("Clone answered equivalent LinkedHashMap", hm2 != hm);
+ for (int counter = 0; counter < hmSize; counter++)
+ assertTrue("Clone answered unequal LinkedHashMap", hm
+ .get(objArray2[counter]) == hm2.get(objArray2[counter]));
+
+ LinkedHashMap map = new LinkedHashMap();
+ map.put("key", "value");
+ // get the keySet() and values() on the original Map
+ Set keys = map.keySet();
+ Collection values = map.values();
+ assertTrue("values() does not work", values.iterator().next().equals(
+ "value"));
+ assertTrue("keySet() does not work", keys.iterator().next().equals(
+ "key"));
+ AbstractMap map2 = (AbstractMap) map.clone();
+ map2.put("key", "value2");
+ Collection values2 = map2.values();
+ assertTrue("values() is identical", values2 != values);
+
+ // values() and keySet() on the cloned() map should be different
+ assertTrue("values() was not cloned", values2.iterator().next().equals(
+ "value2"));
+ map2.clear();
+ map2.put("key2", "value3");
+ Set key2 = map2.keySet();
+ assertTrue("keySet() is identical", key2 != keys);
+ assertTrue("keySet() was not cloned", key2.iterator().next().equals(
+ "key2"));
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#containsKey(java.lang.Object)
+ */
+ public void test_containsKeyLjava_lang_Object() {
+ // Test for method boolean
+ // java.util.LinkedHashMap.containsKey(java.lang.Object)
+ assertTrue("Returned false for valid key", hm.containsKey(new Integer(
+ 876).toString()));
+ assertTrue("Returned true for invalid key", !hm.containsKey("KKDKDKD"));
+
+ LinkedHashMap m = new LinkedHashMap();
+ m.put(null, "test");
+ assertTrue("Failed with null key", m.containsKey(null));
+ assertTrue("Failed with missing key matching null hash", !m
+ .containsKey(new Integer(0)));
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#containsValue(java.lang.Object)
+ */
+ public void test_containsValueLjava_lang_Object() {
+ // Test for method boolean
+ // java.util.LinkedHashMap.containsValue(java.lang.Object)
+ assertTrue("Returned false for valid value", hm
+ .containsValue(new Integer(875)));
+ assertTrue("Returned true for invalid valie", !hm
+ .containsValue(new Integer(-9)));
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#isEmpty()
+ */
+ public void test_isEmpty() {
+ // Test for method boolean java.util.LinkedHashMap.isEmpty()
+ assertTrue("Returned false for new map", new LinkedHashMap().isEmpty());
+ assertTrue("Returned true for non-empty", !hm.isEmpty());
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#size()
+ */
+ public void test_size() {
+ // Test for method int java.util.LinkedHashMap.size()
+ assertTrue("Returned incorrect size",
+ hm.size() == (objArray.length + 2));
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#entrySet()
+ */
+ public void test_ordered_entrySet() {
+ int i;
+ int sz = 100;
+ LinkedHashMap lhm = new LinkedHashMap();
+ for (i = 0; i < sz; i++) {
+ Integer ii = new Integer(i);
+ lhm.put(ii, ii.toString());
+ }
+
+ Set s1 = lhm.entrySet();
+ Iterator it1 = s1.iterator();
+ assertTrue("Returned set of incorrect size 1", lhm.size() == s1.size());
+ for (i = 0; it1.hasNext(); i++) {
+ Map.Entry m = (Map.Entry) it1.next();
+ Integer jj = (Integer) m.getKey();
+ assertTrue("Returned incorrect entry set 1", jj.intValue() == i);
+ }
+
+ LinkedHashMap lruhm = new LinkedHashMap(200, .75f, true);
+ for (i = 0; i < sz; i++) {
+ Integer ii = new Integer(i);
+ lruhm.put(ii, ii.toString());
+ }
+
+ Set s3 = lruhm.entrySet();
+ Iterator it3 = s3.iterator();
+ assertTrue("Returned set of incorrect size 2", lruhm.size() == s3
+ .size());
+ for (i = 0; i < sz && it3.hasNext(); i++) {
+ Map.Entry m = (Map.Entry) it3.next();
+ Integer jj = (Integer) m.getKey();
+ assertTrue("Returned incorrect entry set 2", jj.intValue() == i);
+ }
+
+ /* fetch the even numbered entries to affect traversal order */
+ int p = 0;
+ for (i = 0; i < sz; i += 2) {
+ String ii = (String) lruhm.get(new Integer(i));
+ p = p + Integer.parseInt(ii);
+ }
+ assertTrue("invalid sum of even numbers", p == 2450);
+
+ Set s2 = lruhm.entrySet();
+ Iterator it2 = s2.iterator();
+ assertTrue("Returned set of incorrect size 3", lruhm.size() == s2
+ .size());
+ for (i = 1; i < sz && it2.hasNext(); i += 2) {
+ Map.Entry m = (Map.Entry) it2.next();
+ Integer jj = (Integer) m.getKey();
+ assertTrue("Returned incorrect entry set 3", jj.intValue() == i);
+ }
+ for (i = 0; i < sz && it2.hasNext(); i += 2) {
+ Map.Entry m = (Map.Entry) it2.next();
+ Integer jj = (Integer) m.getKey();
+ assertTrue("Returned incorrect entry set 4", jj.intValue() == i);
+ }
+ assertTrue("Entries left to iterate on", !it2.hasNext());
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#keySet()
+ */
+ public void test_ordered_keySet() {
+ int i;
+ int sz = 100;
+ LinkedHashMap lhm = new LinkedHashMap();
+ for (i = 0; i < sz; i++) {
+ Integer ii = new Integer(i);
+ lhm.put(ii, ii.toString());
+ }
+
+ Set s1 = lhm.keySet();
+ Iterator it1 = s1.iterator();
+ assertTrue("Returned set of incorrect size", lhm.size() == s1.size());
+ for (i = 0; it1.hasNext(); i++) {
+ Integer jj = (Integer) it1.next();
+ assertTrue("Returned incorrect entry set", jj.intValue() == i);
+ }
+
+ LinkedHashMap lruhm = new LinkedHashMap(200, .75f, true);
+ for (i = 0; i < sz; i++) {
+ Integer ii = new Integer(i);
+ lruhm.put(ii, ii.toString());
+ }
+
+ Set s3 = lruhm.keySet();
+ Iterator it3 = s3.iterator();
+ assertTrue("Returned set of incorrect size", lruhm.size() == s3.size());
+ for (i = 0; i < sz && it3.hasNext(); i++) {
+ Integer jj = (Integer) it3.next();
+ assertTrue("Returned incorrect entry set", jj.intValue() == i);
+ }
+
+ /* fetch the even numbered entries to affect traversal order */
+ int p = 0;
+ for (i = 0; i < sz; i += 2) {
+ String ii = (String) lruhm.get(new Integer(i));
+ p = p + Integer.parseInt(ii);
+ }
+ assertTrue("invalid sum of even numbers", p == 2450);
+
+ Set s2 = lruhm.keySet();
+ Iterator it2 = s2.iterator();
+ assertTrue("Returned set of incorrect size", lruhm.size() == s2.size());
+ for (i = 1; i < sz && it2.hasNext(); i += 2) {
+ Integer jj = (Integer) it2.next();
+ assertTrue("Returned incorrect entry set", jj.intValue() == i);
+ }
+ for (i = 0; i < sz && it2.hasNext(); i += 2) {
+ Integer jj = (Integer) it2.next();
+ assertTrue("Returned incorrect entry set", jj.intValue() == i);
+ }
+ assertTrue("Entries left to iterate on", !it2.hasNext());
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#values()
+ */
+ public void test_ordered_values() {
+ int i;
+ int sz = 100;
+ LinkedHashMap lhm = new LinkedHashMap();
+ for (i = 0; i < sz; i++) {
+ Integer ii = new Integer(i);
+ lhm.put(ii, new Integer(i * 2));
+ }
+
+ Collection s1 = lhm.values();
+ Iterator it1 = s1.iterator();
+ assertTrue("Returned set of incorrect size 1", lhm.size() == s1.size());
+ for (i = 0; it1.hasNext(); i++) {
+ Integer jj = (Integer) it1.next();
+ assertTrue("Returned incorrect entry set 1", jj.intValue() == i * 2);
+ }
+
+ LinkedHashMap lruhm = new LinkedHashMap(200, .75f, true);
+ for (i = 0; i < sz; i++) {
+ Integer ii = new Integer(i);
+ lruhm.put(ii, new Integer(i * 2));
+ }
+
+ Collection s3 = lruhm.values();
+ Iterator it3 = s3.iterator();
+ assertTrue("Returned set of incorrect size", lruhm.size() == s3.size());
+ for (i = 0; i < sz && it3.hasNext(); i++) {
+ Integer jj = (Integer) it3.next();
+ assertTrue("Returned incorrect entry set", jj.intValue() == i * 2);
+ }
+
+ // fetch the even numbered entries to affect traversal order
+ int p = 0;
+ for (i = 0; i < sz; i += 2) {
+ Integer ii = (Integer) lruhm.get(new Integer(i));
+ p = p + ii.intValue();
+ }
+ assertTrue("invalid sum of even numbers", p == 2450 * 2);
+
+ Collection s2 = lruhm.values();
+ Iterator it2 = s2.iterator();
+ assertTrue("Returned set of incorrect size", lruhm.size() == s2.size());
+ for (i = 1; i < sz && it2.hasNext(); i += 2) {
+ Integer jj = (Integer) it2.next();
+ assertTrue("Returned incorrect entry set", jj.intValue() == i * 2);
+ }
+ for (i = 0; i < sz && it2.hasNext(); i += 2) {
+ Integer jj = (Integer) it2.next();
+ assertTrue("Returned incorrect entry set", jj.intValue() == i * 2);
+ }
+ assertTrue("Entries left to iterate on", !it2.hasNext());
+ }
+
+ /**
+ * @tests java.util.LinkedHashMap#removeEldestEntry(java.util.Map$Entry)
+ */
+ public void test_remove_eldest() {
+ int i;
+ int sz = 10;
+ CacheMap lhm = new CacheMap();
+ for (i = 0; i < sz; i++) {
+ Integer ii = new Integer(i);
+ lhm.put(ii, new Integer(i * 2));
+ }
+
+ Collection s1 = lhm.values();
+ Iterator it1 = s1.iterator();
+ assertTrue("Returned set of incorrect size 1", lhm.size() == s1.size());
+ for (i = 5; it1.hasNext(); i++) {
+ Integer jj = (Integer) it1.next();
+ assertTrue("Returned incorrect entry set 1", jj.intValue() == i * 2);
+ }
+ assertTrue("Entries left in map", !it1.hasNext());
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ hm = new LinkedHashMap();
+ for (int i = 0; i < objArray.length; i++)
+ hm.put(objArray2[i], objArray[i]);
+ hm.put("test", null);
+ hm.put(null, "test");
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/LinkedHashSetTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/LinkedHashSetTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/LinkedHashSetTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/LinkedHashSetTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,221 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.util;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+/**
+ * @tests java.util.LinkedHashSet
+ */
+public class LinkedHashSetTest extends junit.framework.TestCase {
+
+ LinkedHashSet hs;
+
+ static Object[] objArray;
+ {
+ objArray = new Object[1000];
+ for (int i = 0; i < objArray.length; i++)
+ objArray[i] = new Integer(i);
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#LinkedHashSet()
+ */
+ public void test_Constructor() {
+ // Test for method java.util.LinkedHashSet()
+ LinkedHashSet hs2 = new LinkedHashSet();
+ assertTrue("Created incorrect LinkedHashSet", hs2.size() == 0);
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#LinkedHashSet(int)
+ */
+ public void test_ConstructorI() {
+ // Test for method java.util.LinkedHashSet(int)
+ LinkedHashSet hs2 = new LinkedHashSet(5);
+ assertTrue("Created incorrect LinkedHashSet", hs2.size() == 0);
+ try {
+ new LinkedHashSet(-1);
+ } catch (IllegalArgumentException e) {
+ return;
+ }
+ fail(
+ "Failed to throw IllegalArgumentException for capacity < 0");
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#LinkedHashSet(int, float)
+ */
+ public void test_ConstructorIF() {
+ // Test for method java.util.LinkedHashSet(int, float)
+ LinkedHashSet hs2 = new LinkedHashSet(5, (float) 0.5);
+ assertTrue("Created incorrect LinkedHashSet", hs2.size() == 0);
+ try {
+ new LinkedHashSet(0, 0);
+ } catch (IllegalArgumentException e) {
+ return;
+ }
+ fail(
+ "Failed to throw IllegalArgumentException for initial load factor <= 0");
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#LinkedHashSet(java.util.Collection)
+ */
+ public void test_ConstructorLjava_util_Collection() {
+ // Test for method java.util.LinkedHashSet(java.util.Collection)
+ LinkedHashSet hs2 = new LinkedHashSet(Arrays.asList(objArray));
+ for (int counter = 0; counter < objArray.length; counter++)
+ assertTrue("LinkedHashSet does not contain correct elements", hs
+ .contains(objArray[counter]));
+ assertTrue("LinkedHashSet created from collection incorrect size", hs2
+ .size() == objArray.length);
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#add(java.lang.Object)
+ */
+ public void test_addLjava_lang_Object() {
+ // Test for method boolean java.util.LinkedHashSet.add(java.lang.Object)
+ int size = hs.size();
+ hs.add(new Integer(8));
+ assertTrue("Added element already contained by set", hs.size() == size);
+ hs.add(new Integer(-9));
+ assertTrue("Failed to increment set size after add",
+ hs.size() == size + 1);
+ assertTrue("Failed to add element to set", hs.contains(new Integer(-9)));
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#clear()
+ */
+ public void test_clear() {
+ // Test for method void java.util.LinkedHashSet.clear()
+ Set orgSet = (Set) hs.clone();
+ hs.clear();
+ Iterator i = orgSet.iterator();
+ assertTrue("Returned non-zero size after clear", hs.size() == 0);
+ while (i.hasNext())
+ assertTrue("Failed to clear set", !hs.contains(i.next()));
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#clone()
+ */
+ public void test_clone() {
+ // Test for method java.lang.Object java.util.LinkedHashSet.clone()
+ LinkedHashSet hs2 = (LinkedHashSet) hs.clone();
+ assertTrue("clone returned an equivalent LinkedHashSet", hs != hs2);
+ assertTrue("clone did not return an equal LinkedHashSet", hs
+ .equals(hs2));
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#contains(java.lang.Object)
+ */
+ public void test_containsLjava_lang_Object() {
+ // Test for method boolean
+ // java.util.LinkedHashSet.contains(java.lang.Object)
+ assertTrue("Returned false for valid object", hs.contains(objArray[90]));
+ assertTrue("Returned true for invalid Object", !hs
+ .contains(new Object()));
+
+ LinkedHashSet s = new LinkedHashSet();
+ s.add(null);
+ assertTrue("Cannot handle null", s.contains(null));
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#isEmpty()
+ */
+ public void test_isEmpty() {
+ // Test for method boolean java.util.LinkedHashSet.isEmpty()
+ assertTrue("Empty set returned false", new LinkedHashSet().isEmpty());
+ assertTrue("Non-empty set returned true", !hs.isEmpty());
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#iterator()
+ */
+ public void test_iterator() {
+ // Test for method java.util.Iterator java.util.LinkedHashSet.iterator()
+ Iterator i = hs.iterator();
+ int x = 0;
+ int j;
+ for (j = 0; i.hasNext(); j++) {
+ Object oo = i.next();
+ if (oo != null) {
+ Integer ii = (Integer) oo;
+ assertTrue("Incorrect element found", ii.intValue() == j);
+ } else {
+ assertTrue("Cannot find null", hs.contains(oo));
+ }
+ ++x;
+ }
+ assertTrue("Returned iteration of incorrect size", hs.size() == x);
+
+ LinkedHashSet s = new LinkedHashSet();
+ s.add(null);
+ assertTrue("Cannot handle null", s.iterator().next() == null);
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#remove(java.lang.Object)
+ */
+ public void test_removeLjava_lang_Object() {
+ // Test for method boolean
+ // java.util.LinkedHashSet.remove(java.lang.Object)
+ int size = hs.size();
+ hs.remove(new Integer(98));
+ assertTrue("Failed to remove element", !hs.contains(new Integer(98)));
+ assertTrue("Failed to decrement set size", hs.size() == size - 1);
+
+ LinkedHashSet s = new LinkedHashSet();
+ s.add(null);
+ assertTrue("Cannot handle null", s.remove(null));
+ }
+
+ /**
+ * @tests java.util.LinkedHashSet#size()
+ */
+ public void test_size() {
+ // Test for method int java.util.LinkedHashSet.size()
+ assertTrue("Returned incorrect size", hs.size() == (objArray.length + 1));
+ hs.clear();
+ assertTrue("Cleared set returned non-zero size", hs.size() == 0);
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ hs = new LinkedHashSet();
+ for (int i = 0; i < objArray.length; i++)
+ hs.add(objArray[i]);
+ hs.add(null);
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}