test/java/util/Collections/CheckedMapBash.java
Print this page
rev 7461 : 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
Reviewed-by: dmocek, martin, smarks
*** 21,48 ****
* questions.
*/
/*
* @test
! * @bug 4904067 5023830
* @summary Unit test for Collections.checkedMap
* @author Josh Bloch
*/
import java.util.*;
! public class CheckedMapBash {
! static Random rnd = new Random();
! static Object nil = new Integer(0);
! public static void main(String[] args) {
! int numItr = 100;
! int mapSize = 100;
!
! // Linked List test
! for (int i=0; i<numItr; i++) {
! Map m = newMap();
Object head = nil;
for (int j=0; j<mapSize; j++) {
Object newHead;
do {
--- 21,53 ----
* questions.
*/
/*
* @test
! * @bug 4904067 5023830 7129185
* @summary Unit test for Collections.checkedMap
* @author Josh Bloch
+ * @run testng CheckedMapBash
*/
import java.util.*;
+ import java.util.function.Supplier;
+ import org.testng.annotations.Test;
+ import org.testng.annotations.DataProvider;
! import static org.testng.Assert.fail;
! import static org.testng.Assert.assertTrue;
! public class CheckedMapBash {
! static final Random rnd = new Random();
! static final Object nil = new Integer(0);
! static final int numItr = 100;
! static final int mapSize = 100;
!
! @Test(dataProvider = "Bash.Supplier<Map<Integer,Integer>>")
! public static void testCheckedMap(String description, Supplier<Map<Integer,Integer>> supplier) {
! Map m = supplier.get();
Object head = nil;
for (int j=0; j<mapSize; j++) {
Object newHead;
do {
*** 68,78 ****
m.entrySet().equals(hm.entrySet()) &&
m.keySet().equals(hm.keySet())))
fail("Incorrect equals computation.");
}
! Map m2 = newMap(); m2.putAll(m);
m2.values().removeAll(m.keySet());
if (m2.size()!= 1 || !m2.containsValue(nil))
fail("Collection views test failed.");
int j=0;
--- 73,83 ----
m.entrySet().equals(hm.entrySet()) &&
m.keySet().equals(hm.keySet())))
fail("Incorrect equals computation.");
}
! Map m2 = supplier.get(); m2.putAll(m);
m2.values().removeAll(m.keySet());
if (m2.size()!= 1 || !m2.containsValue(nil))
fail("Collection views test failed.");
int j=0;
*** 90,109 ****
fail("Map nonempty after removing all links.");
if (j != mapSize)
fail("Linked list size not as expected.");
}
! Map m = newMap();
for (int i=0; i<mapSize; i++)
if (m.put(new Integer(i), new Integer(2*i)) != null)
fail("put returns a non-null value erroenously.");
for (int i=0; i<2*mapSize; i++)
if (m.containsValue(new Integer(i)) != (i%2==0))
fail("contains value "+i);
if (m.put(nil, nil) == null)
fail("put returns a null value erroenously.");
! Map m2 = newMap(); m2.putAll(m);
if (!m.equals(m2))
fail("Clone not equal to original. (1)");
if (!m2.equals(m))
fail("Clone not equal to original. (2)");
Set s = m.entrySet(), s2 = m2.entrySet();
--- 95,116 ----
fail("Map nonempty after removing all links.");
if (j != mapSize)
fail("Linked list size not as expected.");
}
! @Test(dataProvider = "Supplier<Map<Integer,Integer>>")
! public static void testCheckeMap2(String description, Supplier<Map<Integer,Integer>> supplier) {
! Map m = supplier.get();
for (int i=0; i<mapSize; i++)
if (m.put(new Integer(i), new Integer(2*i)) != null)
fail("put returns a non-null value erroenously.");
for (int i=0; i<2*mapSize; i++)
if (m.containsValue(new Integer(i)) != (i%2==0))
fail("contains value "+i);
if (m.put(nil, nil) == null)
fail("put returns a null value erroenously.");
! Map m2 = supplier.get(); m2.putAll(m);
if (!m.equals(m2))
fail("Clone not equal to original. (1)");
if (!m2.equals(m))
fail("Clone not equal to original. (2)");
Set s = m.entrySet(), s2 = m2.entrySet();
*** 132,149 ****
}
if (!m.isEmpty())
fail("Iterator.remove() failed");
}
- static Map newMap() {
- Map m = Collections.checkedMap(new HashMap(),
- Integer.class, Integer.class);
-
- if (!m.isEmpty())
- fail("New instance non empty.");
- return m;
- }
! static void fail(String s) {
! throw new RuntimeException(s);
}
}
--- 139,176 ----
}
if (!m.isEmpty())
fail("Iterator.remove() failed");
}
! @DataProvider(name = "Bash.Supplier<Map<Integer,Integer>>", parallel = true)
! public static Iterator<Object[]> bashNavigableMapProvider() {
! ArrayList<Object[]> iters = new ArrayList<>(makeCheckedMaps());
! iters.ensureCapacity(numItr * iters.size());
! for(int each=1; each < numItr; each++) {
! iters.addAll( makeCheckedMaps());
! }
! return iters.iterator();
! }
!
! @DataProvider(name = "Supplier<Map<Integer,Integer>>", parallel = true)
! public static Iterator<Object[]> navigableMapProvider() {
! return makeCheckedMaps().iterator();
! }
!
! public static Collection<Object[]> makeCheckedMaps() {
! return Arrays.asList(
! new Object[]{"Collections.checkedMap(HashMap)",
! (Supplier) () -> {return Collections.checkedMap(new HashMap(), Integer.class, Integer.class);}},
! new Object[]{"Collections.checkedMap(TreeSet(reverseOrder)",
! (Supplier) () -> {return Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},
! new Object[]{"Collections.checkedMap(TreeSet).descendingSet()",
! (Supplier) () -> {return Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}},
! new Object[]{"Collections.checkedNavigableMap(TreeSet)",
! (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class);}},
! new Object[]{"Collections.checkedNavigableMap(TreeSet(reverseOrder)",
! (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},
! new Object[]{"Collections.checkedNavigableMap().descendingSet()",
! (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}}
! );
}
}