changeset 8125:e33af7ca99f9

Add test for List's default methods and a minor typo in ArraysLambdaTest
author Tristan Yan <tristan.yan@oracle.com>
date Mon, 08 Apr 2013 02:11:14 +0800
parents 52923cc16dc4
children 53c48bc81402
files test/java/util/Arrays/ArraysLambdaTest.java test/java/util/List/SortAndReplaceAllTest.java
diffstat 2 files changed, 196 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/util/Arrays/ArraysLambdaTest.java	Fri Apr 05 19:04:04 2013 -0400
+++ b/test/java/util/Arrays/ArraysLambdaTest.java	Mon Apr 08 02:11:14 2013 +0800
@@ -178,10 +178,10 @@
     @Test
     public void testSetAll() {
         boolean[] both = new boolean[]{true, false};
-        for(boolean isParalell:both) {
+        for(boolean isParallel:both) {
             //test Arrays.setAll(double[], IntFunction)
             int[] iArray = new int[ARRAY_SIZE];
-            if (isParalell)
+            if (isParallel)
                 Arrays.parallelSetAll(iArray, i -> 2 * i);
             else
                 Arrays.setAll(iArray, i -> 2 * i);
@@ -190,7 +190,7 @@
 
             //test Arrays.setAll(long[], IntFunction)
             long[] lArray = new long[ARRAY_SIZE];
-            if (isParalell)
+            if (isParallel)
                 Arrays.parallelSetAll(lArray, i -> (long)i << 32);
             else
                 Arrays.setAll(lArray, i -> (long)i << 32);
@@ -199,7 +199,7 @@
 
             //test Arrays.setAll(double[], IntToDoubleFunction)
             double[] dArray = new double[ARRAY_SIZE];
-            if (isParalell)
+            if (isParallel)
                 Arrays.parallelSetAll(dArray, i -> Math.sin(i));
             else
                 Arrays.setAll(dArray, i -> Math.sin(i));
@@ -210,7 +210,7 @@
             //This is a other version of Arrays.copy()
             String[] sArrExpected = generateStringData(ARRAY_SIZE);
             String[] sArr = new String[ARRAY_SIZE];
-            if (isParalell)
+            if (isParallel)
                 Arrays.parallelSetAll(sArr, i -> sArrExpected[i]);
             else
                 Arrays.setAll(sArr, i -> sArrExpected[i]);
@@ -218,4 +218,4 @@
                 assertEquals(sArrExpected[i], sArrExpected[i]); 
         }
     }
-}
\ No newline at end of file
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/util/List/SortAndReplaceAllTest.java	Mon Apr 08 02:11:14 2013 +0800
@@ -0,0 +1,190 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @summary test for List default methods replaceAll() and sort();
+ * @(#) SortAndReplaceAllTest.java
+ * @library ../../
+ * @author Tristan Yan
+ * @run testng SortAndReplaceAllTest
+ */
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Comparators;
+import java.util.EnumSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Random;
+import java.util.Stack;
+import java.util.Vector;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.function.Function;
+import java.util.stream.Stream;
+import static org.testng.Assert.*;
+import org.testng.annotations.Factory;
+import org.testng.annotations.Test;
+
+public class SortAndReplaceAllTest <T extends List<Integer>>{
+    protected Class<T> typeObject;
+    
+    protected boolean hasIni;
+    
+    protected int initSize;
+
+    protected final static int START = -1 << 6;
+    
+    protected final static int END = 1 << 6;
+
+    protected final static Random rand = new Random(System.currentTimeMillis());
+
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    protected static Class[] defaultConstructorClazz = {
+        (Class<ArrayList<Integer>>)(Class<?>)(ArrayList.class),
+        (Class<CopyOnWriteArrayList<Integer>>)(Class<?>)(CopyOnWriteArrayList.class),
+        (Class<LinkedList<Integer>>)(Class<?>)(LinkedList.class),
+        (Class<Stack<Integer>>)(Class<?>)(Stack.class),
+        (Class<Vector<Integer>>)(Class<?>)(Vector.class)
+    };
+    
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    protected static Class[] capacityConstructorClazz = {
+        (Class<ArrayList<Integer>>)(Class<?>)(ArrayList.class),
+        (Class<Vector<Integer>>)(Class<?>)(Vector.class)
+    };
+
+    public SortAndReplaceAllTest(Class<T> clazz, int... initSizes){
+        this.typeObject = clazz;
+        assert(initSizes.length <= 1);
+        if(initSizes.length == 1) {
+            hasIni = true;
+            this.initSize = initSizes[0];
+        }
+    }
+    
+    private List<Integer> generateList(int start, int end, int size)
+            throws Exception{
+        List<Integer> list =  hasIni ? 
+            (List<Integer>)LambdaUtilities.create(typeObject, initSize) :
+            (List<Integer>)LambdaUtilities.create(typeObject);
+        for(int i = 0; i < size; i++) {
+            int element = start + rand.nextInt(end - start); 
+            list.add(element);
+        }
+        return list;
+    }
+
+    protected List<Integer> generateList(int start, int end) throws Exception{
+        return generateList(start, end, end - start);
+    }
+    
+    @Factory
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    public static Object[] create(){
+        List<SortAndReplaceAllTest> result = new ArrayList<>();
+        Stream<Class> stream1 = Arrays.stream(defaultConstructorClazz);
+        Stream<Class> stream2 = Arrays.stream(capacityConstructorClazz);       
+        stream1.forEach(clazz -> result.add(new SortAndReplaceAllTest(clazz)));
+        stream2.forEach(clazz -> result.add(new SortAndReplaceAllTest(clazz,
+                END - START + rand.nextInt(END - START))));
+        return result.toArray();
+    }
+
+    @Test
+    public void testReplaceAll() throws Exception {
+        List<Integer> list = generateList(START, END);
+        List<Integer> listCloned =  hasIni ? 
+            (List<Integer>)LambdaUtilities.create(typeObject, initSize) :
+            (List<Integer>)LambdaUtilities.create(typeObject);
+        listCloned.addAll(list);
+        EnumSet<LambdaUtilities.IntOp> set = EnumSet.allOf(LambdaUtilities.IntOp.class);
+        LambdaUtilities.IntOp[] ops = new LambdaUtilities.IntOp[set.size()]; 
+        set.toArray(ops);
+        LambdaUtilities.IntOp op = ops[rand.nextInt(ops.length)];
+        int value = START + rand.nextInt(END - START); 
+        if( op == LambdaUtilities.IntOp.DIVIDE || op == LambdaUtilities.IntOp.MOD) {
+            while(value == 0) {
+                value = START + rand.nextInt(END - START);
+            }
+        }
+        list.replaceAll(LambdaUtilities.opIntegerUnaryOperator(op, value));
+        checkReplace(listCloned, list, op, value);
+    }
+    
+    @Test
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    public void testSort() throws Exception{
+        final Function<Integer, String> i2s = i -> String.valueOf(i);
+        Comparator[] cs = { Comparators.<Integer>naturalOrder(),
+            Comparators.<Integer>reverseOrder(),
+            Comparators.comparing(LambdaUtilities.highestPosValueIntFunction()),
+            Comparators.comparing(LambdaUtilities.lowestPosValueIntFunction()),
+            Comparators.comparing(i2s, Comparators.<String>naturalOrder()),
+            Comparators.comparing(i2s, Comparators.<String>reverseOrder()),
+        };
+
+        Iterator<Comparator> csit = Arrays.stream(cs).iterator();
+        while(csit.hasNext()) {
+            testComparator((Comparator<Integer>)csit.next());
+        }
+    }
+   
+    private void checkReplace(List<Integer> orig, List<Integer> replaced, 
+            LambdaUtilities.IntOp op, int value) {
+        assertEquals(orig.size(), replaced.size());
+        for(int index = 0; index < orig.size(); index++) {
+            switch(op) {
+                case ADD:
+                    assertTrue(orig.get(index) + value == replaced.get(index));
+                    break;
+                case SUBTRACT: 
+                    assertTrue(orig.get(index) - value == replaced.get(index));
+                    break;
+                case MULTIPLY:
+                    assertTrue(orig.get(index) * value == replaced.get(index));
+                    break;
+                case DIVIDE:
+                    assertTrue(orig.get(index) / value == replaced.get(index));
+                    break;
+                default:
+                    assertTrue(orig.get(index) % value == replaced.get(index));
+                    break;
+            }
+        }
+    }
+
+    private void testComparator(Comparator<Integer> c) throws Exception {
+        List<Integer> list = generateList(START, END);
+        List<Integer> listCloned =  hasIni ? 
+            (List<Integer>)LambdaUtilities.create(typeObject, initSize) :
+            (List<Integer>)LambdaUtilities.create(typeObject);
+        listCloned.addAll(list);
+        list.sort(c);
+        for(int i = 0; i < list.size() - 1; i++) {
+            assertTrue(c.compare(list.get(i), list.get(i+1)) <= 0);
+        }
+    }
+}