/** * Tests Map.hashCode() */ public void testMapHashCode() { resetEmpty(); assertTrue("Empty maps have different hashCodes.", map.hashCode() == confirmed.hashCode()); resetFull(); assertTrue("Equal maps have different hashCodes.", map.hashCode() == confirmed.hashCode()); }
/** * Tests that the {@link Map#values} collection is backed by * the underlying map for clear(). */ public void testValuesClearChangesMap() { if (!isRemoveSupported()) return; // clear values, reflected in map resetFull(); Collection values = map.values(); assertTrue(map.size() > 0); assertTrue(values.size() > 0); values.clear(); assertTrue(map.size() == 0); assertTrue(values.size() == 0); // clear map, reflected in values resetFull(); values = map.values(); assertTrue(map.size() > 0); assertTrue(values.size() > 0); map.clear(); assertTrue(map.size() == 0); assertTrue(values.size() == 0); }
/** * Test to ensure that makeEmptyMap and makeFull returns a new non-null * map with each invocation. */ public void testMakeMap() { Map em = makeEmptyMap(); assertTrue("failure in test: makeEmptyMap must return a non-null map.", em != null); Map em2 = makeEmptyMap(); assertTrue("failure in test: makeEmptyMap must return a non-null map.", em != null); assertTrue("failure in test: makeEmptyMap must return a new map " + "with each invocation.", em != em2); Map fm = makeFullMap(); assertTrue("failure in test: makeFullMap must return a non-null map.", fm != null); Map fm2 = makeFullMap(); assertTrue("failure in test: makeFullMap must return a non-null map.", fm != null); assertTrue("failure in test: makeFullMap must return a new map " + "with each invocation.", fm != fm2); }
/** * Tests that the {@link Map#keySet} collection is backed by * the underlying map for clear(). */ public void testKeySetClearChangesMap() { if (!isRemoveSupported()) return; // clear values, reflected in map resetFull(); Set keySet = map.keySet(); assertTrue(map.size() > 0); assertTrue(keySet.size() > 0); keySet.clear(); assertTrue(map.size() == 0); assertTrue(keySet.size() == 0); // clear map, reflected in values resetFull(); keySet = map.keySet(); assertTrue(map.size() > 0); assertTrue(keySet.size() > 0); map.clear(); assertTrue(map.size() == 0); assertTrue(keySet.size() == 0); }
/** * Tests that the {@link Map#entrySet()} collection is backed by * the underlying map for clear(). */ public void testEntrySetClearChangesMap() { if (!isRemoveSupported()) return; // clear values, reflected in map resetFull(); Set entrySet = map.entrySet(); assertTrue(map.size() > 0); assertTrue(entrySet.size() > 0); entrySet.clear(); assertTrue(map.size() == 0); assertTrue(entrySet.size() == 0); // clear map, reflected in values resetFull(); entrySet = map.entrySet(); assertTrue(map.size() > 0); assertTrue(entrySet.size() > 0); map.clear(); assertTrue(map.size() == 0); assertTrue(entrySet.size() == 0); }
public void verifyValues() { List known = new ArrayList(confirmed.values()); List test = new ArrayList(values); int size = confirmed.size(); boolean empty = confirmed.isEmpty(); assertEquals("values should be same size as HashMap's" + "\nTest: " + test + "\nReal: " + known, size, values.size()); assertEquals("values should be empty if HashMap is" + "\nTest: " + test + "\nReal: " + known, empty, values.isEmpty()); assertTrue("values should contain all HashMap's elements" + "\nTest: " + test + "\nReal: " + known, test.containsAll(known)); assertTrue("values should contain all HashMap's elements" + "\nTest: " + test + "\nReal: " + known, known.containsAll(test)); // originally coded to use a HashBag, but now separate jar so... for (Iterator it = known.iterator(); it.hasNext();) { boolean removed = test.remove(it.next()); assertTrue("Map's values should still equal HashMap's", removed); } assertTrue("Map's values should still equal HashMap's", test.isEmpty()); }
public void verifyEntrySet() { int size = confirmed.size(); boolean empty = confirmed.isEmpty(); assertEquals("entrySet should be same size as HashMap's" + "\nTest: " + entrySet + "\nReal: " + confirmed.entrySet(), size, entrySet.size()); assertEquals("entrySet should be empty if HashMap is" + "\nTest: " + entrySet + "\nReal: " + confirmed.entrySet(), empty, entrySet.isEmpty()); assertTrue("entrySet should contain all HashMap's elements" + "\nTest: " + entrySet + "\nReal: " + confirmed.entrySet(), entrySet.containsAll(confirmed.entrySet())); assertEquals("entrySet hashCodes should be the same" + "\nTest: " + entrySet + "\nReal: " + confirmed.entrySet(), confirmed.entrySet().hashCode(), entrySet.hashCode()); assertEquals("Map's entry set should still equal HashMap's", confirmed.entrySet(), entrySet); }
/** * Tests Map.equals(Object) */ public void testMapEquals() { resetEmpty(); assertTrue("Empty maps unequal.", map.equals(confirmed)); verify(); resetFull(); assertTrue("Full maps unequal.", map.equals(confirmed)); verify(); resetFull(); // modify the HashMap created from the full map and make sure this // change results in map.equals() to return false. Iterator iter = confirmed.keySet().iterator(); iter.next(); iter.remove(); assertTrue("Different maps equal.", !map.equals(confirmed)); resetFull(); assertTrue("equals(null) returned true.", !map.equals(null)); assertTrue("equals(new Object()) returned true.", !map.equals(new Object())); verify(); }
public void verifyKeySet() { int size = confirmed.size(); boolean empty = confirmed.isEmpty(); assertEquals("keySet should be same size as HashMap's" + "\nTest: " + keySet + "\nReal: " + confirmed.keySet(), size, keySet.size()); assertEquals("keySet should be empty if HashMap is" + "\nTest: " + keySet + "\nReal: " + confirmed.keySet(), empty, keySet.isEmpty()); assertTrue("keySet should contain all HashMap's elements" + "\nTest: " + keySet + "\nReal: " + confirmed.keySet(), keySet.containsAll(confirmed.keySet())); assertEquals("keySet hashCodes should be the same" + "\nTest: " + keySet + "\nReal: " + confirmed.keySet(), confirmed.keySet().hashCode(), keySet.hashCode()); assertEquals("Map's key set should still equal HashMap's", confirmed.keySet(), keySet); }
/** * Helper method to add all the mappings described by * {@link #getSampleKeys()} and {@link #getSampleValues()}. */ public void addSampleMappings(Map m) { Object[] keys = getSampleKeys(); Object[] values = getSampleValues(); for(int i = 0; i < keys.length; i++) { try { m.put(keys[i], values[i]); } catch (NullPointerException exception) { assertTrue("NullPointerException only allowed to be thrown " + "if either the key or value is null.", keys[i] == null || values[i] == null); assertTrue("NullPointerException on null key, but " + "isAllowNullKey is not overridden to return false.", keys[i] == null || !isAllowNullKey()); assertTrue("NullPointerException on null value, but " + "isAllowNullValue is not overridden to return false.", values[i] == null || !isAllowNullValue()); assertTrue("Unknown reason for NullPointer.", false); } } assertEquals("size must reflect number of mappings added.", keys.length, m.size()); }
public void verifyMap() { int size = confirmed.size(); boolean empty = confirmed.isEmpty(); assertEquals("Map should be same size as HashMap", size, map.size()); assertEquals("Map should be empty if HashMap is", empty, map.isEmpty()); assertEquals("hashCodes should be the same", confirmed.hashCode(), map.hashCode()); // this fails for LRUMap because confirmed.equals() somehow modifies // map, causing concurrent modification exceptions. //assertEquals("Map should still equal HashMap", confirmed, map); // this works though and performs the same verification: assertTrue("Map should still equal HashMap", map.equals(confirmed)); // TODO: this should really be reexamined to figure out why LRU map // behaves like it does (the equals shouldn't modify since all accesses // by the confirmed collection should be through an iterator, thus not // causing LRUMap to change). }
assertTrue("values().remove(obj) is broken", j < 10000); assertTrue( "Value should have been removed from the underlying map.", !map.containsValue(sampleValues[i]));
/** * Tests that the {@link Map#keySet} set is backed by * the underlying map by removing from the keySet set * and testing if the key was removed from the map. */ public void testKeySetRemoveChangesMap() { resetFull(); Object[] sampleKeys = getSampleKeys(); Set keys = map.keySet(); for (int i = 0; i < sampleKeys.length; i++) { try { keys.remove(sampleKeys[i]); } catch (UnsupportedOperationException e) { // if key.remove is unsupported, just skip this test return; } assertTrue( "Key should have been removed from the underlying map.", !map.containsKey(sampleKeys[i])); } }
/** * Tests Map.toString(). Since the format of the string returned by the * toString() method is not defined in the Map interface, there is no * common way to test the results of the toString() method. Thereforce, * it is encouraged that Map implementations override this test with one * that checks the format matches any format defined in its API. This * default implementation just verifies that the toString() method does * not return null. */ public void testMapToString() { resetEmpty(); assertTrue("Empty map toString() should not return null", map.toString() != null); verify(); resetFull(); assertTrue("Empty map toString() should not return null", map.toString() != null); verify(); }
Object[] newValues = getNewSampleValues(); assertTrue("failure in test: Must have keys returned from " + "getSampleKeys.", keys != null); assertTrue("failure in test: Must have values returned from " + "getSampleValues.", values != null); assertTrue("failure in test: duplicate null keys.", (keys[i] != null || keys[j] != null)); assertTrue("failure in test: duplicate non-null key.", (keys[i] == null || keys[j] == null || (!keys[i].equals(keys[j]) && !keys[j].equals(keys[i])))); assertTrue("failure in test: found null key, but isNullKeySupported " + "is false.", keys[i] != null || isAllowNullKey()); assertTrue("failure in test: found null value, but isNullValueSupported " + "is false.", values[i] != null || isAllowNullValue()); assertTrue("failure in test: found null new value, but isNullValueSupported " + "is false.", newValues[i] != null || isAllowNullValue()); assertTrue("failure in test: values should not be the same as new value", values[i] != newValues[i] && (values[i] == null || !values[i].equals(newValues[i])));
/** * Tests Map.containsKey(Object) by verifying it returns false for all * sample keys on a map created using an empty map and returns true for * all sample keys returned on a full map. */ public void testMapContainsKey() { Object[] keys = getSampleKeys(); resetEmpty(); for(int i = 0; i < keys.length; i++) { assertTrue("Map must not contain key when map is empty", !map.containsKey(keys[i])); } verify(); resetFull(); for(int i = 0; i < keys.length; i++) { assertTrue("Map must contain key for a mapping in the map. " + "Missing: " + keys[i], map.containsKey(keys[i])); } verify(); }
/** * Tests Map.containsValue(Object) by verifying it returns false for all * sample values on an empty map and returns true for all sample values on * a full map. */ public void testMapContainsValue() { Object[] values = getSampleValues(); resetEmpty(); for(int i = 0; i < values.length; i++) { assertTrue("Empty map must not contain value", !map.containsValue(values[i])); } verify(); resetFull(); for(int i = 0; i < values.length; i++) { assertTrue("Map must contain value for a mapping in the map.", map.containsValue(values[i])); } verify(); }
confirmed.put(keys[i], values[i]); verify(); assertTrue("First map.put should return null", o == null); assertTrue("Map should contain key after put", map.containsKey(keys[i])); assertTrue("Map should contain value after put", map.containsValue(values[i])); assertEquals("Map.put should return previous value when changed", values[i], o); assertTrue("Map should still contain key after put when changed", map.containsKey(keys[i])); assertTrue("Map should contain new value after put when changed", map.containsValue(newValues[i])); assertTrue("Map should not contain old value after put when changed", !map.containsValue(values[i])); assertEquals("Map.put should return previous value when changed", value, o); assertTrue("Map should still contain key after put when changed", map.containsKey(key)); assertTrue("Map should contain new value after put when changed", map.containsValue(newValues[i])); assertTrue("Map should not contain old value after put when changed", !map.containsValue(values[i]));
/** * Tests Map.get(Object) */ public void testMapGet() { resetEmpty(); Object[] keys = getSampleKeys(); Object[] values = getSampleValues(); for (int i = 0; i < keys.length; i++) { assertTrue("Empty map.get() should return null.", map.get(keys[i]) == null); } verify(); resetFull(); for (int i = 0; i < keys.length; i++) { assertEquals("Full map.get() should return value from mapping.", values[i], map.get(keys[i])); } }
for(int i = 0; i < keys.length; i++) { Object o = map.remove(keys[i]); assertTrue("First map.remove should return null", o == null);