public void testMapDifferenceEmptySingleton() { MapDifference<Integer, Integer> diff = Maps.difference(EMPTY, SINGLETON); assertFalse(diff.areEqual()); assertEquals(EMPTY, diff.entriesOnlyOnLeft()); assertEquals(SINGLETON, diff.entriesOnlyOnRight()); assertEquals(EMPTY, diff.entriesInCommon()); assertEquals(EMPTY, diff.entriesDiffering()); assertEquals("not equal: only on right={1=2}", diff.toString()); }
assertTrue(result.isEmpty()); testProp.setProperty("first", "true"); assertEquals("true", result.get("first")); assertEquals(1, result.size()); testProp.setProperty("second", "null"); assertEquals("true", result.get("first")); assertEquals("null", result.get("second")); assertEquals(2, result.size()); assertEquals(4, result.size()); assertEquals("true", result.get("first")); assertEquals("", result.get("test")); assertEquals("2", result.get("second")); assertEquals("item : a short phrase ", result.get("Third")); assertFalse(result.containsKey("not here")); assertTrue(result.containsKey("java.version")); assertTrue(result.size() > 2); assertEquals("", result.get("test")); assertEquals("hidden", result.get("java.version")); assertNotSame(System.getProperty("java.version"), result.get("java.version"));
public void testAsMapReadsThrough() { Set<String> strings = Sets.newLinkedHashSet(); Collections.addAll(strings, "one", "two", "three"); Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map); assertNull(map.get("four")); strings.add("four"); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5, "four", 4), map); assertEquals(Integer.valueOf(4), map.get("four")); }
public void testImmutableEntryNull() { Entry<String, Integer> e = Maps.immutableEntry((String) null, (Integer) null); assertNull(e.getKey()); assertNull(e.getValue()); try { e.setValue(null); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } assertEquals("null=null", e.toString()); assertEquals(0, e.hashCode()); }
public void testImmutableEntry() { Entry<String, Integer> e = Maps.immutableEntry("foo", 1); assertEquals("foo", e.getKey()); assertEquals(1, (int) e.getValue()); try { e.setValue(2); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } assertEquals("foo=1", e.toString()); assertEquals(101575, e.hashCode()); }
/** * Tests that nHMWES makes hash maps large enough that adding the expected number of elements * won't cause a rehash. * * <p>As of jdk7u40, HashMap has an empty-map optimization. The argument to new HashMap(int) is * noted, but the initial table is a zero-length array. * * <p>This test may fail miserably on non-OpenJDK environments... */ @GwtIncompatible // reflection @AndroidIncompatible // relies on assumptions about OpenJDK public void testNewHashMapWithExpectedSize_wontGrow() throws Exception { // before jdk7u40: creates one-bucket table // after jdk7u40: creates empty table assertTrue(bucketsOf(Maps.newHashMapWithExpectedSize(0)) <= 1); for (int size = 1; size < 200; size++) { assertWontGrow( size, Maps.newHashMapWithExpectedSize(size), Maps.newHashMapWithExpectedSize(size)); } }
assertEquals("four", unmod.get(4)); assertEquals("four", unmod.descendingMap().get(4)); ensureNotDirectlyModifiable(unmod); ensureNotDirectlyModifiable(unmod.descendingMap()); ensureNotDirectlyModifiable(unmod.headMap(2, true)); ensureNotDirectlyModifiable(unmod.subMap(1, true, 3, true)); ensureNotDirectlyModifiable(unmod.tailMap(2, true)); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { iterator.next(); iterator.remove(); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { iterator.next(); iterator.remove(); fail("UnsupportedOperationException expected");
public void testAsMapEmpty() { Set<String> strings = ImmutableSet.of(); Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertThat(map.entrySet()).isEmpty(); assertTrue(map.isEmpty()); assertNull(map.get("five")); }
@SuppressWarnings("serial") public void testLinkedHashMapWithInitialMap() { Map<String, String> map = new LinkedHashMap<String, String>( ImmutableMap.of( "Hello", "World", "first", "second", "polygene", "lubricants", "alpha", "betical")); LinkedHashMap<String, String> copy = Maps.newLinkedHashMap(map); Iterator<Entry<String, String>> iter = copy.entrySet().iterator(); assertTrue(iter.hasNext()); Entry<String, String> entry = iter.next(); assertEquals("Hello", entry.getKey()); assertEquals("World", entry.getValue()); assertTrue(iter.hasNext()); entry = iter.next(); assertEquals("first", entry.getKey()); assertEquals("second", entry.getValue()); assertTrue(iter.hasNext()); entry = iter.next(); assertEquals("polygene", entry.getKey()); assertEquals("lubricants", entry.getValue()); assertTrue(iter.hasNext()); entry = iter.next(); assertEquals("alpha", entry.getKey()); assertEquals("betical", entry.getValue()); assertFalse(iter.hasNext()); }
assertSame(unmod.inverse(), unmod.inverse()); assertSame(unmod, unmod.inverse().inverse()); assertEquals(true, unmod.get(4).equals("four")); assertEquals(true, unmod.inverse().get("four").equals(4)); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { try { inverse.put("four", 4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { try { values.remove("four"); fail("UnsupportedOperationException expected");
public void testTreeMapWithComparator() { TreeMap<Integer, Integer> map = Maps.newTreeMap(SOME_COMPARATOR); assertEquals(Collections.emptyMap(), map); assertSame(SOME_COMPARATOR, map.comparator()); }
public void testMapDifferenceEmptyEmpty() { MapDifference<Integer, Integer> diff = Maps.difference(EMPTY, EMPTY); assertTrue(diff.areEqual()); assertEquals(EMPTY, diff.entriesOnlyOnLeft()); assertEquals(EMPTY, diff.entriesOnlyOnRight()); assertEquals(EMPTY, diff.entriesInCommon()); assertEquals(EMPTY, diff.entriesDiffering()); assertEquals("equal", diff.toString()); }
public void testCapacityForNegativeSizeFails() { try { Maps.capacity(-1); fail("Negative expected size must result in IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
public void testTransformValues() { Map<String, Integer> map = ImmutableMap.of("a", 4, "b", 9); Map<String, Double> transformed = transformValues(map, SQRT_FUNCTION); assertEquals(ImmutableMap.of("a", 2.0, "b", 3.0), transformed); }
public void testEnumMapWithInitialEmptyEnumMap() { EnumMap<SomeEnum, Integer> original = Maps.newEnumMap(SomeEnum.class); EnumMap<SomeEnum, Integer> copy = Maps.newEnumMap(original); assertEquals(original, copy); assertNotSame(original, copy); }
public void testAsConverter_isAView() throws Exception { BiMap<String, Integer> biMap = HashBiMap.create(); biMap.put("one", 1); biMap.put("two", 2); Converter<String, Integer> converter = Maps.asConverter(biMap); assertSame(1, converter.convert("one")); assertSame(2, converter.convert("two")); try { converter.convert("three"); fail(); } catch (IllegalArgumentException expected) { } biMap.put("three", 3); assertSame(1, converter.convert("one")); assertSame(2, converter.convert("two")); assertSame(3, converter.convert("three")); }
assertFalse(diff1.areEqual()); assertThat(diff1.entriesOnlyOnLeft().entrySet()) .containsExactly(Maps.immutableEntry(2, "b"), Maps.immutableEntry(4, "d")) try { diff1.entriesInCommon().put(7, "x"); fail(); } catch (UnsupportedOperationException expected) { fail(); } catch (UnsupportedOperationException expected) { fail(); } catch (UnsupportedOperationException expected) {
public void testAsConverter_nominal() throws Exception { ImmutableBiMap<String, Integer> biMap = ImmutableBiMap.of( "one", 1, "two", 2); Converter<String, Integer> converter = Maps.asConverter(biMap); for (Entry<String, Integer> entry : biMap.entrySet()) { assertSame(entry.getValue(), converter.convert(entry.getKey())); } }
public void testMapDifferenceOfSortedMapIsSorted() { Map<Integer, Integer> map = SORTED_SINGLETON; MapDifference<Integer, Integer> difference = Maps.difference(map, EMPTY); assertTrue(difference instanceof SortedMapDifference); }
public void testAsConverter_nullConversions() throws Exception { ImmutableBiMap<String, Integer> biMap = ImmutableBiMap.of( "one", 1, "two", 2); Converter<String, Integer> converter = Maps.asConverter(biMap); assertNull(converter.convert(null)); assertNull(converter.reverse().convert(null)); }