@Test public void withKeyValue() { MutableMapIterable<String, Integer> map = this.newMapWithKeyValue("A", 1); MutableMapIterable<String, Integer> mapWith = map.withKeyValue("B", 2); Assert.assertSame(map, mapWith); Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 1, "B", 2), mapWith); MutableMapIterable<String, Integer> mapWith2 = mapWith.withKeyValue("A", 11); Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 11, "B", 2), mapWith); }
@Test public void removeNullFromValues() { if (this.newMap() instanceof ConcurrentMap) { return; } MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3); Assert.assertFalse(map.values().remove(null)); Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Two", 2, "Three", 3), map); map.put("Four", null); Assert.assertTrue(map.values().remove(null)); Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Two", 2, "Three", 3), map); }
@Test public void removeObject() { MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3); map.remove("Two"); Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map); }
@Test public void retainAllFromEntrySet() { MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3); Assert.assertFalse( map.entrySet() .retainAll( FastList.newListWith( ImmutableEntry.of("One", 1), ImmutableEntry.of("Two", 2), ImmutableEntry.of("Three", 3), ImmutableEntry.of("Four", 4)))); Assert.assertTrue( map.entrySet() .retainAll( FastList.newListWith( ImmutableEntry.of("One", 1), ImmutableEntry.of("Three", 3), ImmutableEntry.of("Four", 4)))); Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map); MutableMapIterable<Integer, Integer> integers = this.newMapWithKeysValues(1, 1, 2, 2, 3, 3); Integer copy = new Integer(1); Assert.assertTrue(integers.entrySet().retainAll(mList(ImmutableEntry.of(copy, copy)))); Assert.assertEquals(iMap(copy, copy), integers); Assert.assertNotSame(copy, Iterate.getOnly(integers.entrySet()).getKey()); Assert.assertNotSame(copy, Iterate.getOnly(integers.entrySet()).getValue()); }
@Test public void removeFromEntrySet() { MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3); Assert.assertTrue(map.entrySet().remove(ImmutableEntry.of("Two", 2))); Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map); Assert.assertFalse(map.entrySet().remove(ImmutableEntry.of("Four", 4))); Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map); Assert.assertFalse(map.entrySet().remove(null)); MutableMapIterable<String, Integer> mapWithNullKey = this.newMapWithKeysValues("One", 1, null, 2, "Three", 3); Assert.assertTrue( mapWithNullKey.entrySet().remove(new ImmutableEntry<String, Integer>(null, 2))); }
@Test public void withoutAllKeys() { MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("A", 1, "B", 2, "C", 3); MutableMapIterable<String, Integer> mapWithout = map.withoutAllKeys(FastList.newListWith("A", "C")); Assert.assertSame(map, mapWithout); Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("B", 2), mapWithout); }
@Test public void withAllKeyValueArguments() { MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("A", 1, "B", 2); MutableMapIterable<String, Integer> mapWith = map.withAllKeyValueArguments(Tuples.pair("B", 22), Tuples.pair("C", 3)); Assert.assertSame(map, mapWith); Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 1, "B", 22, "C", 3), mapWith); }
@Test public void add() { MutableMapIterable<String, Integer> map = this.newMapWithKeyValue("A", 1); Assert.assertEquals(Integer.valueOf(1), map.add(Tuples.pair("A", 3))); Assert.assertNull(map.add(Tuples.pair("B", 2))); Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 3, "B", 2), map); }
@Test public void retainAllFromValues() { MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3); Assert.assertFalse(map.values().retainAll(FastList.newListWith(1, 2, 3, 4))); Assert.assertTrue(map.values().retainAll(FastList.newListWith(1, 3))); Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map); }
@Test public void removeFromValues() { MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3); Assert.assertFalse(map.values().remove(4)); Assert.assertTrue(map.values().remove(2)); Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map); }
@Test public void removeAllFromKeySet() { MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3); Assert.assertFalse(map.keySet().removeAll(FastList.newListWith("Four"))); Assert.assertTrue(map.keySet().removeAll(FastList.newListWith("Two", "Four"))); Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map); }
@Test public void getIfAbsentPut_block_throws() { MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3"); Verify.assertThrows( RuntimeException.class, () -> map.getIfAbsentPut( 4, () -> { throw new RuntimeException(); })); Assert.assertEquals(UnifiedMap.newWithKeysValues(1, "1", 2, "2", 3, "3"), map); }
@Test public void put() { MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two"); Assert.assertNull(map.put(3, "Three")); Assert.assertEquals(UnifiedMap.newWithKeysValues(1, "One", 2, "Two", 3, "Three"), map); ImmutableList<Integer> key1 = Lists.immutable.with(null); ImmutableList<Integer> key2 = Lists.immutable.with(null); Object value1 = new Object(); Object value2 = new Object(); MutableMapIterable<ImmutableList<Integer>, Object> map2 = this.newMapWithKeyValue(key1, value1); Object previousValue = map2.put(key2, value2); Assert.assertSame(value1, previousValue); Assert.assertSame(key1, map2.keysView().getFirst()); }