changeset 7615:fbd0019da331

Cut off execution time for Streams/BasicTest.java and correct IntStreamTest for IntStream.sum() is returning int
author Tristan Yan <tristan.yan@oracle.com>
date Tue, 12 Mar 2013 20:06:05 +0800
parents cfc1cdcbd8b5
children f5ce4c3958b1
files test/java/util/LambdaUtilities.java test/java/util/stream/Stream/IntStreamTest.java test/java/util/stream/Streams/BasicTest.java
diffstat 3 files changed, 19 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/util/LambdaUtilities.java	Tue Mar 12 11:28:41 2013 +0100
+++ b/test/java/util/LambdaUtilities.java	Tue Mar 12 20:06:05 2013 +0800
@@ -327,7 +327,7 @@
     }
 
     public static Supplier<AtomicInteger> atomicIntegerSupplier(int value) {
-        return () -> new AtomicInteger(0);
+        return () -> new AtomicInteger(value);
     }
 
     public static <T> Supplier<AtomicReference<T>> atomicGenericSupplier(T value) {
--- a/test/java/util/stream/Stream/IntStreamTest.java	Tue Mar 12 11:28:41 2013 +0100
+++ b/test/java/util/stream/Stream/IntStreamTest.java	Tue Mar 12 20:06:05 2013 +0800
@@ -32,7 +32,7 @@
 
 
 import java.util.*;
-import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.BiFunction;
 import java.util.function.IntPredicate;
 import java.util.stream.IntStream;
@@ -174,12 +174,14 @@
             IntStream avgStream1 = (p == ParallelType.Parallel) ? Arrays.parallelStream(array)
                     : (p == ParallelType.Sequential) ? Arrays.stream(array).sequential() :
                     Arrays.stream(array);
-            long total1 = 0;
+            double total1 = 0;
+            int total1Int = 0;
             for(int index = 0; index < array.length; index++) {
                 total1 += array[index];
+                total1Int += array[index];
             }
-            assertEquals(avgStream1.average().getAsDouble(), (double)total1 / array.length);
-            assertEquals(sumStream1.sum(), total1);
+            assertEquals(avgStream1.average().getAsDouble(), total1 / array.length);
+            assertEquals(sumStream1.sum(), total1Int);
 
             int startIndex = rand.nextInt(array.length);
             int endIndex = startIndex + rand.nextInt(array.length - startIndex + 1);
@@ -193,12 +195,14 @@
                assertEquals(avgStream2.average(), OptionalDouble.empty());
                assertEquals(sumStream2.sum(), 0);
             } else {
-                long total2 = 0;
+                double total2 = 0;
+                int total2Int = 0;
                 for(int index = startIndex; index < endIndex; index++) {
                     total2 += array[index];
+                    total2Int += array[index];
                 }
-                assertEquals(avgStream2.average().getAsDouble(), (double)total2 / (endIndex - startIndex));
-                assertEquals(sumStream2.sum(), total2);
+                assertEquals(avgStream2.average().getAsDouble(), total2 / (endIndex - startIndex));
+                assertEquals(sumStream2.sum(), total2Int);
             }
         }
     }
@@ -624,14 +628,14 @@
             IntStream stream1 = (p == ParallelType.Parallel) ? Arrays.parallelStream(array)
                     : (p == ParallelType.Sequential) ? Arrays.stream(array).sequential() :
                     Arrays.stream(array);
-            AtomicLong ai1 = new AtomicLong(0);
-            long sum = stream1.peek(LambdaUtilities.addLongConsumer(ai1)).sum();
+            AtomicInteger ai1 = new AtomicInteger(0);
+            long sum = stream1.peek(LambdaUtilities.addIntConsumer(ai1)).sum();
             assertEquals(ai1.get(), sum);
 
             IntStream emptyStream = (p == ParallelType.Parallel) ? Arrays.parallelStream(new int[0]):
                     (p == ParallelType.Sequential) ? Arrays.stream(new int[0]).sequential() :
                     Arrays.stream(new int[0]);
-            assertFalse(emptyStream.peek(LambdaUtilities.addLongConsumer(ai1)).iterator().hasNext());
+            assertFalse(emptyStream.peek(LambdaUtilities.addIntConsumer(ai1)).iterator().hasNext());
             assertEquals(ai1.get(), sum);
         }
     }
--- a/test/java/util/stream/Streams/BasicTest.java	Tue Mar 12 11:28:41 2013 +0100
+++ b/test/java/util/stream/Streams/BasicTest.java	Tue Mar 12 20:06:05 2013 +0800
@@ -34,7 +34,6 @@
 import java.util.Iterator;
 import java.util.PrimitiveIterator;
 import java.util.Random;
-import java.util.Spliterators;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.IntConsumer;
 import java.util.function.IntUnaryOperator;
@@ -73,7 +72,7 @@
     @Test
     public void testGenerate() {
         final int randomValue = rand.nextInt();
-        final int randomSize= rand.nextInt(2 << 10);
+        final int randomSize= rand.nextInt(1 << 8);
         IntStream is = Streams.generateInt(() -> randomValue);
         assertEquals(is.spliterator().getExactSizeIfKnown(), -1);
 
@@ -103,8 +102,8 @@
         }
 
         //below is time-consuming, need watch out if it's gonna affect our test
-        int start = rand.nextInt(2 << 12);
-        int end = rand.nextInt(2 << 12);
+        int start = rand.nextInt(1 << 12);
+        int end = rand.nextInt(1 << 12);
 
         if(start > end) {
             start = start ^ end;
@@ -121,6 +120,7 @@
         while(it2.hasNext())
             assertEquals(it2.nextInt(), start += step);
         assertTrue(start + step > end);
+
     }
 
     @Test