mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8285405: add test and check for negative argument to HashMap::newHashMap et al
Reviewed-by: chegar, naoto, lancea, smarks
This commit is contained in:
parent
2e0a17c560
commit
4b03e135e1
6 changed files with 41 additions and 2 deletions
|
@ -2578,6 +2578,9 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
||||||
* @since 19
|
* @since 19
|
||||||
*/
|
*/
|
||||||
public static <K, V> HashMap<K, V> newHashMap(int numMappings) {
|
public static <K, V> HashMap<K, V> newHashMap(int numMappings) {
|
||||||
|
if (numMappings < 0) {
|
||||||
|
throw new IllegalArgumentException("Negative number of mappings: " + numMappings);
|
||||||
|
}
|
||||||
return new HashMap<>(calculateHashMapCapacity(numMappings));
|
return new HashMap<>(calculateHashMapCapacity(numMappings));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -394,6 +394,9 @@ public class HashSet<E>
|
||||||
* @since 19
|
* @since 19
|
||||||
*/
|
*/
|
||||||
public static <T> HashSet<T> newHashSet(int numElements) {
|
public static <T> HashSet<T> newHashSet(int numElements) {
|
||||||
|
if (numElements < 0) {
|
||||||
|
throw new IllegalArgumentException("Negative number of elements: " + numElements);
|
||||||
|
}
|
||||||
return new HashSet<>(HashMap.calculateHashMapCapacity(numElements));
|
return new HashSet<>(HashMap.calculateHashMapCapacity(numElements));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -810,6 +810,9 @@ public class LinkedHashMap<K,V>
|
||||||
* @since 19
|
* @since 19
|
||||||
*/
|
*/
|
||||||
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int numMappings) {
|
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int numMappings) {
|
||||||
|
if (numMappings < 0) {
|
||||||
|
throw new IllegalArgumentException("Negative number of mappings: " + numMappings);
|
||||||
|
}
|
||||||
return new LinkedHashMap<>(HashMap.calculateHashMapCapacity(numMappings));
|
return new LinkedHashMap<>(HashMap.calculateHashMapCapacity(numMappings));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -215,6 +215,9 @@ public class LinkedHashSet<E>
|
||||||
* @since 19
|
* @since 19
|
||||||
*/
|
*/
|
||||||
public static <T> LinkedHashSet<T> newLinkedHashSet(int numElements) {
|
public static <T> LinkedHashSet<T> newLinkedHashSet(int numElements) {
|
||||||
|
if (numElements < 0) {
|
||||||
|
throw new IllegalArgumentException("Negative number of elements: " + numElements);
|
||||||
|
}
|
||||||
return new LinkedHashSet<>(HashMap.calculateHashMapCapacity(numElements));
|
return new LinkedHashSet<>(HashMap.calculateHashMapCapacity(numElements));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1357,6 +1357,9 @@ public class WeakHashMap<K,V>
|
||||||
* @since 19
|
* @since 19
|
||||||
*/
|
*/
|
||||||
public static <K, V> WeakHashMap<K, V> newWeakHashMap(int numMappings) {
|
public static <K, V> WeakHashMap<K, V> newWeakHashMap(int numMappings) {
|
||||||
|
if (numMappings < 0) {
|
||||||
|
throw new IllegalArgumentException("Negative number of mappings: " + numMappings);
|
||||||
|
}
|
||||||
return new WeakHashMap<>(HashMap.calculateHashMapCapacity(numMappings));
|
return new WeakHashMap<>(HashMap.calculateHashMapCapacity(numMappings));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,6 @@ import java.lang.invoke.MethodHandle;
|
||||||
import java.lang.invoke.MethodHandles;
|
import java.lang.invoke.MethodHandles;
|
||||||
import java.lang.invoke.MethodType;
|
import java.lang.invoke.MethodType;
|
||||||
import java.lang.invoke.VarHandle;
|
import java.lang.invoke.VarHandle;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.AbstractMap;
|
import java.util.AbstractMap;
|
||||||
import java.util.AbstractSet;
|
import java.util.AbstractSet;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -44,10 +43,12 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.IntFunction;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
import static org.testng.Assert.assertNull;
|
import static org.testng.Assert.assertNull;
|
||||||
|
import static org.testng.Assert.assertThrows;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
|
@ -426,4 +427,27 @@ public class WhiteBoxResizeTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DataProvider(name = "negativeNumMappings")
|
||||||
|
public Iterator<Object[]> negativeNumMappings() {
|
||||||
|
final List<Object[]> methods = new ArrayList<>();
|
||||||
|
methods.add(new Object[] {(IntFunction<?>) HashMap::newHashMap, "HashMap::newHashMap"});
|
||||||
|
methods.add(new Object[] {(IntFunction<?>) LinkedHashMap::newLinkedHashMap,
|
||||||
|
"LinkedHashMap::newLinkedHashMap"});
|
||||||
|
methods.add(new Object[] {(IntFunction<?>) WeakHashMap::newWeakHashMap,
|
||||||
|
"WeakHashMap::newWeakHashMap"});
|
||||||
|
methods.add(new Object[] {(IntFunction<?>) HashSet::newHashSet, "HashSet::newHashSet"});
|
||||||
|
methods.add(new Object[] {(IntFunction<?>) LinkedHashSet::newLinkedHashSet,
|
||||||
|
"LinkedHashSet::newLinkedHashSet"});
|
||||||
|
return methods.iterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that the APIs that take {@code numMappings} or {@code numElements} as a parameter for
|
||||||
|
* creating the collection instance (for example: {@link HashMap#newHashMap(int)}), throw
|
||||||
|
* an {@code IllegalArgumentException} when a negative value is passed to them
|
||||||
|
*/
|
||||||
|
@Test(dataProvider = "negativeNumMappings")
|
||||||
|
public void testNegativeNumMappings(final IntFunction<?> method, final String methodName) {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> method.apply(-1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue