/** * {@inheritDoc} */ public void putAll(final Map<? extends Integer, ? extends Integer> map) { for (final Map.Entry<? extends Integer, ? extends Integer> entry : map.entrySet()) { put(entry.getKey(), entry.getValue()); } }
/** * {@inheritDoc} */ public Integer put(final Integer key, final Integer value) { return valOrNull(put((int)key, (int)value)); }
public Map<Integer, Integer> create(final Object... entries) { final Int2IntHashMap map = new Int2IntHashMap( entries.length * 2, Hashing.DEFAULT_LOAD_FACTOR, -1, false); for (final Object o : entries) { @SuppressWarnings("unchecked") final Map.Entry<Integer, Integer> e = (Map.Entry<Integer, Integer>)o; map.put(e.getKey(), e.getValue()); } return map; }
@Test public void entriesAreAllocatedByEntriesIterator() { map.put(1, 1); map.put(2, 2); final Iterator<Entry<Integer, Integer>> entryIterator = map.entrySet().iterator(); final Entry<Integer, Integer> entry1 = entryIterator.next(); final Entry<Integer, Integer> entry2 = entryIterator.next(); assertNotEquals(entry1, entry2); } }
@Test public void forEachShouldLoopOverEveryElement() { map.put(1, 1); map.put(100, 100); final IntIntConsumer mockConsumer = mock(IntIntConsumer.class); map.intForEach(mockConsumer); final InOrder inOrder = inOrder(mockConsumer); inOrder.verify(mockConsumer).accept(1, 1); inOrder.verify(mockConsumer).accept(100, 100); inOrder.verifyNoMoreInteractions(); }
@Test public void shouldGenerateStringRepresentation() { final int[] testEntries = { 3, 1, 19, 7, 11, 12, 7 }; for (final int testEntry : testEntries) { map.put(testEntry, testEntry + 1000); } final String mapAsAString = "{12=1012, 11=1011, 7=1007, 19=1019, 3=1003, 1=1001}"; assertThat(map.toString(), equalTo(mapAsAString)); }
@Test public void shouldContainValueForAPresentEntry() { map.put(1, 1); assertTrue(map.containsValue(1)); }
@Test public void sizeShouldReturnNumberOfEntries() { final int count = 100; for (int key = 0; key < count; key++) { map.put(key, 1); } assertEquals(count, map.size()); }
@Test public void correctSizeAfterRehash() { final Int2IntHashMap map = new Int2IntHashMap(16, 0.6f, -1); IntStream.range(1, 17).forEach((i) -> map.put(i, i)); assertEquals("Map has correct size", 16, map.size()); final List<Integer> keys = new ArrayList<>(map.keySet()); keys.forEach(map::remove); assertTrue("Map isn't empty", map.isEmpty()); }
@Test public void correctSizeAfterRehash() { final Int2IntHashMap map = new Int2IntHashMap(16, 0.6f, -1); IntStream.range(1, 17).forEach((i) -> map.put(i, i)); assertEquals("Map has correct size", 16, map.size()); final List<Integer> keys = new ArrayList<>(map.keySet()); keys.forEach(map::remove); assertTrue("Map isn't empty", map.isEmpty()); }
@Test public void shouldContainKeyOfAPresentKey() { map.put(1, 1); assertTrue(map.containsKey(1)); }
@Test public void removeShouldReturnValueRemoved() { map.put(1, 2); assertEquals(2, map.remove(1)); }
@Test public void shouldNotContainMissingValue() { assertFalse(map.containsValue(MISSING_VALUE)); map.put(MISSING_VALUE, 1); assertFalse(map.containsValue(MISSING_VALUE)); }
@SuppressWarnings("ConstantConditions") @Test public void removeShouldRemoveEntry() { map.put(1, 2); map.remove(1); assertTrue(map.isEmpty()); assertFalse(map.containsKey(1)); assertFalse(map.containsValue(2)); }