Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Copyright (C) 2008 Google Inc.
    *
    * 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 org.mapdb;
  
  
  import java.util.*;
  
  import static java.util.Collections.singleton;

Tests representing the contract of java.util.Map. Concrete subclasses of this base class test conformance of concrete java.util.Map subclasses to that contract.

Parameters:
<K> the type of keys used by the maps under test
<V> the type of mapped values used the maps under test
Author(s):
George van den Driessche
  
  public abstract class MapInterfaceTest<K, V> extends TestCase {
      protected final boolean supportsPut;
      protected final boolean supportsRemove;
      protected final boolean supportsClear;
      protected final boolean allowsNullKeys;
      protected final boolean allowsNullValues;
      protected final boolean supportsIteratorRemove;
      protected final boolean supportsEntrySetValue;


    
Creates a new, empty instance of the class under test.

Returns:
a new, empty map instance.
Throws:
java.lang.UnsupportedOperationException if it's not possible to make an empty instance of the class under test.
  
      protected abstract Map<K, V> makeEmptyMap()
              throws UnsupportedOperationException;

    
Creates a new, non-empty instance of the class under test.

Returns:
a new, non-empty map instance.
Throws:
java.lang.UnsupportedOperationException if it's not possible to make a non-empty instance of the class under test.
  
      protected abstract Map<K, V> makePopulatedMap()
              throws UnsupportedOperationException;

    
Creates a new key that is not expected to be found in makePopulatedMap().

Returns:
a key.
Throws:
java.lang.UnsupportedOperationException if it's not possible to make a key that will not be found in the map.
  
      protected abstract K getKeyNotInPopulatedMap()
              throws UnsupportedOperationException;

    
Creates a new value that is not expected to be found in makePopulatedMap().

Returns:
a value.
Throws:
java.lang.UnsupportedOperationException if it's not possible to make a value that will not be found in the map.
  
      protected abstract V getValueNotInPopulatedMap()
              throws UnsupportedOperationException;


    
Constructor with an explicit supportsIteratorRemove parameter.
  
      protected MapInterfaceTest(
              boolean allowsNullKeys,
              boolean allowsNullValues,
              boolean supportsPut,
              boolean supportsRemove,
              boolean supportsClear,
              boolean supportsIteratorRemove,
              boolean supportsEntrySetValue) {
         this. = supportsPut;
         this. = supportsRemove;
         this. = supportsClear;
         this. = allowsNullKeys;
         this. = allowsNullValues;
         this. = supportsIteratorRemove;
         this. = supportsEntrySetValue;
 
     }

    
Used by tests that require a map, but don't care whether it's populated or not.

Returns:
a new map instance.
 
     protected Map<K, V> makeEitherMap() {
         try {
             return makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return makeEmptyMap();
         }
     }
 
     protected final boolean supportsValuesHashCode(Map<K, V> map) {
         // get the first non-null value
         Collection<V> values = map.values();
         for (V value : values) {
             if (value != null) {
                 try {
                     value.hashCode();
                 } catch (Exception e) {
                     return false;
                 }
                 return true;
             }
         }
         return true;
     }

    
Checks all the properties that should always hold of a map. Also calls assertMoreInvariants(java.util.Map) to check invariants that are peculiar to specific implementations.

Parameters:
map the map to check.
See also:
assertMoreInvariants(java.util.Map)
 
     protected final void assertInvariants(Map<K, V> map) {
         Set<K> keySet = map.keySet();
         Collection<V> valueCollection = map.values();
         Set<Entry<K, V>> entrySet = map.entrySet();
 
         assertEquals(map.size() == 0, map.isEmpty());
         assertEquals(map.size(), keySet.size());
         assertEquals(keySet.size() == 0, keySet.isEmpty());
         assertEquals(!keySet.isEmpty(), keySet.iterator().hasNext());
 
         int expectedKeySetHash = 0;
         for (K key : keySet) {
             V value = map.get(key);
             expectedKeySetHash += key != null ? key.hashCode() : 0;
             assertTrue(map.containsKey(key));
             assertTrue(map.containsValue(value));
             assertTrue(valueCollection.contains(value));
             assertTrue(valueCollection.containsAll(Collections.singleton(value)));
             assertTrue(entrySet.contains(mapEntry(keyvalue)));
             assertTrue( || (key != null));
         }
         assertEquals(expectedKeySetHashkeySet.hashCode());
 
         assertEquals(map.size(), valueCollection.size());
         assertEquals(valueCollection.size() == 0, valueCollection.isEmpty());
         assertEquals(
                 !valueCollection.isEmpty(), valueCollection.iterator().hasNext());
         for (V value : valueCollection) {
             assertTrue(map.containsValue(value));
             assertTrue( || (value != null));
         }
 
         assertEquals(map.size(), entrySet.size());
         assertEquals(entrySet.size() == 0, entrySet.isEmpty());
         assertEquals(!entrySet.isEmpty(), entrySet.iterator().hasNext());
         assertTrue(!entrySet.contains("foo"));
 
         boolean supportsValuesHashCode = supportsValuesHashCode(map);
         if (supportsValuesHashCode) {
             int expectedEntrySetHash = 0;
             for (Entry<K, V> entry : entrySet) {
                 assertTrue(map.containsKey(entry.getKey()));
                 assertTrue(entry.toString(), map.containsValue(entry.getValue()));
                 int expectedHash =
                         (entry.getKey() == null ? 0 : entry.getKey().hashCode()) ^
                                 (entry.getValue() == null ? 0 : entry.getValue().hashCode());
                 assertEquals(expectedHashentry.hashCode());
                 expectedEntrySetHash += expectedHash;
             }
             assertEquals(expectedEntrySetHashentrySet.hashCode());
             assertTrue(entrySet.containsAll(new HashSet<Entry<K, V>>(entrySet)));
             assertTrue(entrySet.equals(new HashSet<Entry<K, V>>(entrySet)));
         }
 
         Object[] entrySetToArray1 = entrySet.toArray();
         assertEquals(map.size(), entrySetToArray1.length);
         assertTrue(Arrays.asList(entrySetToArray1).containsAll(entrySet));
 
         Entry<?, ?>[] entrySetToArray2 = new Entry<?, ?>[map.size() + 2];
         entrySetToArray2[map.size()] = mapEntry("foo", 1);
         assertSame(entrySetToArray2entrySet.toArray(entrySetToArray2));
         assertNull(entrySetToArray2[map.size()]);
         assertTrue(Arrays.asList(entrySetToArray2).containsAll(entrySet));
 
         Object[] valuesToArray1 = valueCollection.toArray();
         assertEquals(map.size(), valuesToArray1.length);
         assertTrue(Arrays.asList(valuesToArray1).containsAll(valueCollection));
 
         Object[] valuesToArray2 = new Object[map.size() + 2];
         valuesToArray2[map.size()] = "foo";
         assertSame(valuesToArray2valueCollection.toArray(valuesToArray2));
         assertNull(valuesToArray2[map.size()]);
         assertTrue(Arrays.asList(valuesToArray2).containsAll(valueCollection));
 
         if (supportsValuesHashCode) {
             int expectedHash = 0;
             for (Entry<K, V> entry : entrySet) {
                 expectedHash += entry.hashCode();
             }
             assertEquals(expectedHashmap.hashCode());
         }
 
         assertMoreInvariants(map);
     }

    
Override this to check invariants which should hold true for a particular implementation, but which are not generally applicable to every instance of Map.

Parameters:
map the map whose additional invariants to check.
 
     protected void assertMoreInvariants(Map<K, V> map) {
     }
 
     public void testClear() {
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         if () {
             map.clear();
             assertEquals(0, map.size());
             assertTrue(map.isEmpty());
         } else {
             try {
                 map.clear();
                 fail("Expected UnsupportedOperationException.");
             } catch (UnsupportedOperationException e) {
                 // Expected.
             }
         }
         assertInvariants(map);
     }
 
     public void testContainsKey() {
         final Map<K, V> map;
         final K unmappedKey;
         try {
             map = makePopulatedMap();
             unmappedKey = getKeyNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertTrue(!map.containsKey(unmappedKey));
         assertTrue(map.containsKey(map.keySet().iterator().next()));
         if () {
             map.containsKey(null);
         } else {
             try {
                 map.containsKey(null);
             } catch (NullPointerException optional) {
             }
         }
         assertInvariants(map);
     }
 
     public void testContainsValue() {
         final Map<K, V> map;
         final V unmappedValue;
         try {
             map = makePopulatedMap();
             unmappedValue = getValueNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertTrue(!map.containsValue(unmappedValue));
         assertTrue(map.containsValue(map.values().iterator().next()));
         if () {
             map.containsValue(null);
         } else {
             try {
                 map.containsKey(null);
             } catch (NullPointerException optional) {
             }
         }
         assertInvariants(map);
     }
 
     public void testEntrySet() {
         final Map<K, V> map;
         final Set<Entry<K, V>> entrySet;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertInvariants(map);
 
         entrySet = map.entrySet();
         final K unmappedKey;
         final V unmappedValue;
         try {
             unmappedKey = getKeyNotInPopulatedMap();
             unmappedValue = getValueNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         for (Entry<K, V> entry : entrySet) {
             assertTrue(!unmappedKey.equals(entry.getKey()));
             assertTrue(!unmappedValue.equals(entry.getValue()));
         }
     }
 
     public void testEntrySetForEmptyMap() {
         final Map<K, V> map;
         try {
             map = makeEmptyMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertInvariants(map);
     }
 
     public void testEntrySetContainsEntryNullKeyPresent() {
         if (! || !) {
             return;
         }
         final Map<K, V> map;
         final Set<Entry<K, V>> entrySet;
         try {
             map = makeEitherMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertInvariants(map);
 
         entrySet = map.entrySet();
         final V unmappedValue;
         try {
             unmappedValue = getValueNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         map.put(nullunmappedValue);
         Entry<K, V> entry = mapEntry(nullunmappedValue);
         assertTrue(entrySet.contains(entry));
         assertTrue(!entrySet.contains(mapEntry(nullnull)));
     }
 
     public void testEntrySetContainsEntryNullKeyMissing() {
         final Map<K, V> map;
         final Set<Entry<K, V>> entrySet;
         try {
             map = makeEitherMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertInvariants(map);
 
         entrySet = map.entrySet();
         final V unmappedValue;
         try {
             unmappedValue = getValueNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         Entry<K, V> entry = mapEntry(nullunmappedValue);
         assertTrue(!entrySet.contains(entry));
         assertTrue(!entrySet.contains(mapEntry(nullnull)));
     }
 
     public void testEntrySetIteratorRemove() {
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         Iterator<Entry<K, V>> iterator = entrySet.iterator();
         if () {
             int initialSize = map.size();
             Entry<K, V> entry = iterator.next();
             iterator.remove();
             assertEquals(initialSize - 1, map.size());
             assertTrue(!entrySet.contains(entry));
             assertInvariants(map);
             try {
                 iterator.remove();
                 fail("Expected IllegalStateException.");
             } catch (IllegalStateException e) {
                 // Expected.
             }
         } else {
             try {
                 iterator.next();
                 iterator.remove();
                 fail("Expected UnsupportedOperationException.");
             } catch (UnsupportedOperationException e) {
                 // Expected.
             }
         }
         assertInvariants(map);
     }
 
     public void testEntrySetRemove() {
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         if () {
             int initialSize = map.size();
             boolean didRemove = entrySet.remove(entrySet.iterator().next());
             assertTrue(didRemove);
             assertEquals(initialSize - 1, map.size());
         } else {
             try {
                 entrySet.remove(entrySet.iterator().next());
                 fail("Expected UnsupportedOperationException.");
             } catch (UnsupportedOperationException e) {
                 // Expected.
             }
         }
         assertInvariants(map);
     }
 
     public void testEntrySetRemoveMissingKey() {
         final Map<K, V> map;
         final K key;
         try {
             map = makeEitherMap();
             key = getKeyNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         Entry<K, V> entry
                 = mapEntry(keygetValueNotInPopulatedMap());
         int initialSize = map.size();
         if () {
             boolean didRemove = entrySet.remove(entry);
             assertTrue(!didRemove);
         } else {
             try {
                 boolean didRemove = entrySet.remove(entry);
                 assertTrue(!didRemove);
             } catch (UnsupportedOperationException optional) {
             }
         }
         assertEquals(initialSizemap.size());
         assertTrue(!map.containsKey(key));
         assertInvariants(map);
     }
 
     public void testEntrySetRemoveDifferentValue() {
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         K key = map.keySet().iterator().next();
         Entry<K, V> entry
                 = mapEntry(keygetValueNotInPopulatedMap());
         int initialSize = map.size();
         if () {
             boolean didRemove = entrySet.remove(entry);
             assertTrue(!didRemove);
         } else {
             try {
                 boolean didRemove = entrySet.remove(entry);
                 assertTrue(!didRemove);
             } catch (UnsupportedOperationException optional) {
             }
         }
         assertEquals(initialSizemap.size());
         assertTrue(map.containsKey(key));
         assertInvariants(map);
     }
 
     public void testEntrySetRemoveNullKeyPresent() {
         if (! || ! || !) {
             return;
         }
         final Map<K, V> map;
         final Set<Entry<K, V>> entrySet;
         try {
             map = makeEitherMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertInvariants(map);
 
         entrySet = map.entrySet();
         final V unmappedValue;
         try {
             unmappedValue = getValueNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         map.put(nullunmappedValue);
         assertEquals(unmappedValuemap.get(null));
         assertTrue(map.containsKey(null));
         Entry<K, V> entry = mapEntry(nullunmappedValue);
         assertTrue(entrySet.remove(entry));
         assertNull(map.get(null));
         assertTrue(!map.containsKey(null));
     }
 
     public void testEntrySetRemoveNullKeyMissing() {
         final Map<K, V> map;
         try {
             map = makeEitherMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         Entry<K, V> entry
                 = mapEntry(nullgetValueNotInPopulatedMap());
         int initialSize = map.size();
         if () {
             boolean didRemove = entrySet.remove(entry);
             assertTrue(!didRemove);
         } else {
             try {
                 boolean didRemove = entrySet.remove(entry);
                 assertTrue(!didRemove);
             } catch (UnsupportedOperationException optional) {
             }
         }
         assertEquals(initialSizemap.size());
         assertInvariants(map);
     }
 
     public void testEntrySetRemoveAll() {
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         Set<Entry<K, V>> entriesToRemove =
                 singleton(entrySet.iterator().next());
         if () {
             int initialSize = map.size();
             boolean didRemove = entrySet.removeAll(entriesToRemove);
             assertTrue(didRemove);
             assertEquals(initialSize - entriesToRemove.size(), map.size());
             for (Entry<K, V> entry : entriesToRemove) {
                 assertTrue(!entrySet.contains(entry));
             }
         } else {
             try {
                 entrySet.removeAll(entriesToRemove);
                 fail("Expected UnsupportedOperationException.");
             } catch (UnsupportedOperationException e) {
                 // Expected.
             }
         }
         assertInvariants(map);
     }
 
     public void testEntrySetRemoveAllNullFromEmpty() {
         final Map<K, V> map;
         try {
             map = makeEmptyMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         if () {
             try {
                 entrySet.removeAll(null);
                 fail("Expected NullPointerException.");
             } catch (NullPointerException e) {
                 // Expected.
             }
         } else {
             try {
                 entrySet.removeAll(null);
                 fail("Expected UnsupportedOperationException or NullPointerException.");
             } catch (UnsupportedOperationException e) {
                 // Expected.
             } catch (NullPointerException e) {
                 // Expected.
             }
         }
         assertInvariants(map);
     }
 
     public void testEntrySetRetainAll() {
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         Set<Entry<K, V>> entriesToRetain =
                 singleton(entrySet.iterator().next());
         if () {
             boolean shouldRemove = (entrySet.size() > entriesToRetain.size());
             boolean didRemove = entrySet.retainAll(entriesToRetain);
             assertEquals(shouldRemovedidRemove);
             assertEquals(entriesToRetain.size(), map.size());
             for (Entry<K, V> entry : entriesToRetain) {
                 assertTrue(entrySet.contains(entry));
             }
         } else {
             try {
                 entrySet.retainAll(entriesToRetain);
                 fail("Expected UnsupportedOperationException.");
             } catch (UnsupportedOperationException e) {
                 // Expected.
             }
         }
         assertInvariants(map);
     }
 
     public void testEntrySetRetainAllNullFromEmpty() {
         final Map<K, V> map;
         try {
             map = makeEmptyMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         if () {
             try {
                 entrySet.retainAll(null);
                 // Returning successfully is not ideal, but tolerated.
             } catch (NullPointerException e) {
                 // Expected.
             }
         } else {
             try {
                 entrySet.retainAll(null);
                 // We have to tolerate a successful return (Sun bug 4802647)
             } catch (UnsupportedOperationException e) {
                 // Expected.
             } catch (NullPointerException e) {
                 // Expected.
             }
         }
         assertInvariants(map);
     }
 
     public void testEntrySetClear() {
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         if () {
             entrySet.clear();
             assertTrue(entrySet.isEmpty());
         } else {
             try {
                 entrySet.clear();
                 fail("Expected UnsupportedOperationException.");
             } catch (UnsupportedOperationException e) {
                 // Expected.
             }
         }
         assertInvariants(map);
     }
 
     public void testEntrySetAddAndAddAll() {
         final Map<K, V> map = makeEitherMap();
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         final Entry<K, V> entryToAdd = mapEntry(nullnull);
         try {
             entrySet.add(entryToAdd);
             fail("Expected UnsupportedOperationException or NullPointerException.");
         } catch (UnsupportedOperationException e) {
             // Expected.
         } catch (NullPointerException e) {
             // Expected.
         }
         assertInvariants(map);
 
         try {
             entrySet.addAll(singleton(entryToAdd));
             fail("Expected UnsupportedOperationException or NullPointerException.");
         } catch (UnsupportedOperationException e) {
             // Expected.
         } catch (NullPointerException e) {
             // Expected.
         }
         assertInvariants(map);
     }
 
     public void testEntrySetSetValue() {
         // TODO: Investigate the extent to which, in practice, maps that support
         // put() also support Entry.setValue().
         if (! || !) {
             return;
         }
 
         final Map<K, V> map;
         final V valueToSet;
         try {
             map = makePopulatedMap();
             valueToSet = getValueNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         Entry<K, V> entry = entrySet.iterator().next();
         final V oldValue = entry.getValue();
         final V returnedValue = entry.setValue(valueToSet);
         assertEquals(oldValuereturnedValue);
         assertTrue(entrySet.contains(
                 mapEntry(entry.getKey(), valueToSet)));
         assertEquals(valueToSetmap.get(entry.getKey()));
         assertInvariants(map);
     }
 
     public void testEntrySetSetValueSameValue() {
 
         // TODO: Investigate the extent to which, in practice, maps that support
         // put() also support Entry.setValue().
         if (! || !) {
             return;
         }
 
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         Set<Entry<K, V>> entrySet = map.entrySet();
         Entry<K, V> entry = entrySet.iterator().next();
         final V oldValue = entry.getValue();
         final V returnedValue = entry.setValue(oldValue);
         assertEquals(oldValuereturnedValue);
         assertTrue(entrySet.contains(
                 mapEntry(entry.getKey(), oldValue)));
         assertEquals(oldValuemap.get(entry.getKey()));
         assertInvariants(map);
     }
     
     public void testEntrySetIteratorLastHasNext() {
     	final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         Iterator<Entry<K, V>> iter = map.entrySet().iterator();
         for(int i = 0; i < map.size(); i++)
         	iter.next();
         assertFalse(iter.hasNext());
     }
 
     public void testEntrySetIteratorLastNext() {
     	final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         Iterator<Entry<K, V>> iter = map.entrySet().iterator();
         for(int i = 0; i < map.size(); i++)
         	iter.next();
         try {
         	iter.next();
         }
         catch(NoSuchElementException e) {
         	// Expected
         }
     }
 
     public void testEqualsForEqualMap() {
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         assertEquals(mapmap);
         assertEquals(makePopulatedMap(), map);
         assertTrue(!map.equals(Collections.emptyMap()));
         //no-inspection ObjectEqualsNull
         assertTrue(!map.equals(null));
     }
 
     public void testEqualsForLargerMap() {
         if (!) {
             return;
         }
 
         final Map<K, V> map;
         final Map<K, V> largerMap;
         try {
             map = makePopulatedMap();
             largerMap = makePopulatedMap();
             largerMap.put(getKeyNotInPopulatedMap(), getValueNotInPopulatedMap());
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         assertTrue(!map.equals(largerMap));
     }
 
     public void testEqualsForSmallerMap() {
         if (!) {
             return;
         }
 
         final Map<K, V> map;
         final Map<K, V> smallerMap;
         try {
             map = makePopulatedMap();
             smallerMap = new LinkedHashMap<K, V>(map);
 //      smallerMap = makePopulatedMap();
             smallerMap.remove(smallerMap.keySet().iterator().next());
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         assertTrue(!map.equals(smallerMap));
     }
 
     public void testEqualsForEmptyMap() {
         final Map<K, V> map;
         try {
             map = makeEmptyMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         assertEquals(mapmap);
         assertEquals(makeEmptyMap(), map);
         assertEquals(Collections.emptyMap(), map);
         assertTrue(!map.equals(Collections.emptySet()));
         //noinspection ObjectEqualsNull
         assertTrue(!map.equals(null));
     }
 
     public void testGet() {
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
 
         for (Entry<K, V> entry : map.entrySet()) {
             assertEquals(entry.getValue(), map.get(entry.getKey()));
         }
 
         K unmappedKey;
         try {
             unmappedKey = getKeyNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertNull(map.get(unmappedKey));
     }
 
     public void testGetForEmptyMap() {
         final Map<K, V> map;
         K unmappedKey;
         try {
             map = makeEmptyMap();
             unmappedKey = getKeyNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertNull(map.get(unmappedKey));
     }
 
     public void testGetNull() {
         Map<K, V> map = makeEitherMap();
         if () {
             if () {
                 // TODO: decide what to test here.
             } else {
                 assertEquals(map.containsKey(null), map.get(null) != null);
             }
         } else {
             try {
                 map.get(null);
             } catch (NullPointerException optional) {
             }
         }
         assertInvariants(map);
     }
 
     public void testHashCode() {
         final Map<K, V> map;
         try {
             map = makePopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertInvariants(map);
     }
 
     public void testHashCodeForEmptyMap() {
         final Map<K, V> map;
         try {
             map = makeEmptyMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         assertInvariants(map);
     }
 
     public void testPutNewKey() {
         final Map<K, V> map = makeEitherMap();
         final K keyToPut;
         final V valueToPut;
         try {
             keyToPut = getKeyNotInPopulatedMap();
             valueToPut = getValueNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         if () {
             int initialSize = map.size();
             V oldValue = map.put(keyToPutvalueToPut);
             assertEquals(valueToPutmap.get(keyToPut));
             assertTrue(map.containsKey(keyToPut));
             assertTrue(map.containsValue(valueToPut));
             assertEquals(initialSize + 1, map.size());
             assertNull(oldValue);
         } else {
             try {
                 map.put(keyToPutvalueToPut);
                 fail("Expected UnsupportedOperationException.");
             } catch (UnsupportedOperationException e) {
                 // Expected.
             }
         }
         assertInvariants(map);
     }
 
     public void testPutExistingKey() {
         final Map<K, V> map;
         final K keyToPut;
         final V valueToPut;
         try {
             map = makePopulatedMap();
             valueToPut = getValueNotInPopulatedMap();
         } catch (UnsupportedOperationException e) {
             return;
         }
         keyToPut = map.keySet().iterator().next();
         if () {
             int initialSize = map.size();
             map.put(keyToPutvalueToPut);
             assertEquals(valueToPutmap.get(keyToPut));
            assertTrue(map.containsKey(keyToPut));
            assertTrue(map.containsValue(valueToPut));
            assertEquals(initialSizemap.size());
        } else {
            try {
                map.put(keyToPutvalueToPut);
                fail("Expected UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
                // Expected.
            }
        }
        assertInvariants(map);
    }
    public void testPutNullKey() {
        if (!) {
            return;
        }
        final Map<K, V> map = makeEitherMap();
        final V valueToPut;
        try {
            valueToPut = getValueNotInPopulatedMap();
        } catch (UnsupportedOperationException e) {
            return;
        }
        if () {
            final V oldValue = map.get(null);
            final V returnedValue = map.put(nullvalueToPut);
            assertEquals(oldValuereturnedValue);
            assertEquals(valueToPutmap.get(null));
            assertTrue(map.containsKey(null));
            assertTrue(map.containsValue(valueToPut));
        } else {
            try {
                map.put(nullvalueToPut);
                fail("Expected RuntimeException");
            } catch (RuntimeException e) {
                // Expected.
            }
        }
        assertInvariants(map);
    }
    public void testPutNullValue() {
        if (!) {
            return;
        }
        final Map<K, V> map = makeEitherMap();
        final K keyToPut;
        try {
            keyToPut = getKeyNotInPopulatedMap();
        } catch (UnsupportedOperationException e) {
            return;
        }
        if () {
            int initialSize = map.size();
            final V oldValue = map.get(keyToPut);
            final V returnedValue = map.put(keyToPutnull);
            assertEquals(oldValuereturnedValue);
            assertNull(map.get(keyToPut));
            assertTrue(map.containsKey(keyToPut));
            assertTrue(map.containsValue(null));
            assertEquals(initialSize + 1, map.size());
        } else {
            try {
                map.put(keyToPutnull);
                fail("Expected RuntimeException");
            } catch (RuntimeException e) {
                // Expected.
            }
        }
        assertInvariants(map);
    }
    public void testPutNullValueForExistingKey() {
        if (!) {
            return;
        }
        final Map<K, V> map;
        final K keyToPut;
        try {
            map = makePopulatedMap();
            keyToPut = map.keySet().iterator().next();
        } catch (UnsupportedOperationException e) {
            return;
        }
        if () {
            int initialSize = map.size();
            final V oldValue = map.get(keyToPut);
            final V returnedValue = map.put(keyToPutnull);
            assertEquals(oldValuereturnedValue);
            assertNull(map.get(keyToPut));
            assertTrue(map.containsKey(keyToPut));
            assertTrue(map.containsValue(null));
            assertEquals(initialSizemap.size());
        } else {
            try {
                map.put(keyToPutnull);
                fail("Expected RuntimeException");
            } catch (RuntimeException e) {
                // Expected.
            }
        }
        assertInvariants(map);
    }
    public void testPutAllNewKey() {
        final Map<K, V> map = makeEitherMap();
        final K keyToPut;
        final V valueToPut;
        try {
            keyToPut = getKeyNotInPopulatedMap();
            valueToPut = getValueNotInPopulatedMap();
        } catch (UnsupportedOperationException e) {
            return;
        }
        final Map<K, V> mapToPut = Collections.singletonMap(keyToPutvalueToPut);
        if () {
            int initialSize = map.size();
            map.putAll(mapToPut);
            assertEquals(valueToPutmap.get(keyToPut));
            assertTrue(map.containsKey(keyToPut));
            assertTrue(map.containsValue(valueToPut));
            assertEquals(initialSize + 1, map.size());
        } else {
            try {
                map.putAll(mapToPut);
                fail("Expected UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
                // Expected.
            }
        }
        assertInvariants(map);
    }
    public void testPutAllExistingKey() {
        final Map<K, V> map;
        final K keyToPut;
        final V valueToPut;
        try {
            map = makePopulatedMap();
            valueToPut = getValueNotInPopulatedMap();
        } catch (UnsupportedOperationException e) {
            return;
        }
        keyToPut = map.keySet().iterator().next();
        final Map<K, V> mapToPut = Collections.singletonMap(keyToPutvalueToPut);
        int initialSize = map.size();
        if () {
            map.putAll(mapToPut);
            assertEquals(valueToPutmap.get(keyToPut));
            assertTrue(map.containsKey(keyToPut));
            assertTrue(map.containsValue(valueToPut));
        } else {
            try {
                map.putAll(mapToPut);
                fail("Expected UnsupportedOperationException.");
            } catch (