< prev index next >

test/java/util/stream/test/org/openjdk/tests/java/util/stream/IntPrimitiveOpsTests.java

Print this page




  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.tests.java.util.stream;
  24 
  25 import org.testng.Assert;
  26 import org.testng.annotations.Test;
  27 
  28 import java.util.Arrays;
  29 import java.util.List;
  30 import java.util.Random;


  31 import java.util.concurrent.atomic.AtomicInteger;
  32 import java.util.function.IntConsumer;
  33 import java.util.stream.Collectors;
  34 import java.util.stream.IntStream;
  35 
  36 import static org.testng.Assert.assertEquals;


  37 




  38 @Test
  39 public class IntPrimitiveOpsTests {
  40 
  41     public void testSum() {
  42         long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).sum();
  43         assertEquals(sum, 20);
  44     }
  45 
  46     public void testMap() {
  47         long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).map(i -> i * 2).sum();
  48         assertEquals(sum, 40);
  49     }
  50 
  51     public void testParSum() {
  52         long sum = IntStream.range(1, 10).parallel().filter(i -> i % 2 == 0).sum();
  53         assertEquals(sum, 20);
  54     }
  55 
  56     @Test(groups = { "serialization-hostile" })
  57     public void testTee() {


  68     }
  69 
  70     @Test(groups = { "serialization-hostile" })
  71     public void testParForEach() {
  72         AtomicInteger ai = new AtomicInteger(0);
  73         IntStream.range(1, 10).parallel().filter(i -> i % 2 == 0).forEach(ai::addAndGet);
  74         assertEquals(ai.get(), 20);
  75     }
  76 
  77     public void testBox() {
  78         List<Integer> l = IntStream.range(1, 10).parallel().boxed().collect(Collectors.toList());
  79         int sum = l.stream().reduce(0, (a, b) -> a + b);
  80         assertEquals(sum, 45);
  81     }
  82 
  83     public void testUnBox() {
  84         long sum = Arrays.asList(1, 2, 3, 4, 5).stream().mapToInt(i -> (int) i).sum();
  85         assertEquals(sum, 15);
  86     }
  87 























  88     public void testToArray() {
  89         {
  90             int[] array =  IntStream.range(1, 10).map(i -> i * 2).toArray();
  91             assertEquals(array, new int[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
  92         }
  93 
  94         {
  95             int[] array =  IntStream.range(1, 10).parallel().map(i -> i * 2).toArray();
  96             assertEquals(array, new int[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
  97         }
  98     }
  99 
 100     public void testSort() {
 101         Random r = new Random();
 102 
 103         int[] content = IntStream.generate(() -> r.nextInt(100)).limit(10).toArray();
 104         int[] sortedContent = content.clone();
 105         Arrays.sort(sortedContent);
 106 
 107         {
 108             int[] array =  Arrays.stream(content).sorted().toArray();
 109             assertEquals(array, sortedContent);
 110         }
 111 
 112         {
 113             int[] array =  Arrays.stream(content).parallel().sorted().toArray();
 114             assertEquals(array, sortedContent);





























 115         }
 116     }
 117 
 118     public void testSortSort() {
 119         Random r = new Random();
 120 
 121         int[] content = IntStream.generate(() -> r.nextInt(100)).limit(10).toArray();
 122         int[] sortedContent = content.clone();
 123         Arrays.sort(sortedContent);
 124 
 125         {
 126             int[] array =  Arrays.stream(content).sorted().sorted().toArray();
 127             assertEquals(array, sortedContent);
 128         }
 129 
 130         {
 131             int[] array =  Arrays.stream(content).parallel().sorted().sorted().toArray();
 132             assertEquals(array, sortedContent);
 133         }
 134     }




  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.tests.java.util.stream;
  24 
  25 import org.testng.Assert;
  26 import org.testng.annotations.Test;
  27 
  28 import java.util.Arrays;
  29 import java.util.List;
  30 import java.util.Random;
  31 import java.util.Spliterator;
  32 import java.util.TreeSet;
  33 import java.util.concurrent.atomic.AtomicInteger;
  34 import java.util.function.IntConsumer;
  35 import java.util.stream.Collectors;
  36 import java.util.stream.IntStream;
  37 
  38 import static org.testng.Assert.assertEquals;
  39 import static org.testng.Assert.assertFalse;
  40 import static org.testng.Assert.assertTrue;
  41 
  42 /**
  43  * @test
  44  * @bug 8153293
  45  */
  46 @Test
  47 public class IntPrimitiveOpsTests {
  48 
  49     public void testSum() {
  50         long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).sum();
  51         assertEquals(sum, 20);
  52     }
  53 
  54     public void testMap() {
  55         long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).map(i -> i * 2).sum();
  56         assertEquals(sum, 40);
  57     }
  58 
  59     public void testParSum() {
  60         long sum = IntStream.range(1, 10).parallel().filter(i -> i % 2 == 0).sum();
  61         assertEquals(sum, 20);
  62     }
  63 
  64     @Test(groups = { "serialization-hostile" })
  65     public void testTee() {


  76     }
  77 
  78     @Test(groups = { "serialization-hostile" })
  79     public void testParForEach() {
  80         AtomicInteger ai = new AtomicInteger(0);
  81         IntStream.range(1, 10).parallel().filter(i -> i % 2 == 0).forEach(ai::addAndGet);
  82         assertEquals(ai.get(), 20);
  83     }
  84 
  85     public void testBox() {
  86         List<Integer> l = IntStream.range(1, 10).parallel().boxed().collect(Collectors.toList());
  87         int sum = l.stream().reduce(0, (a, b) -> a + b);
  88         assertEquals(sum, 45);
  89     }
  90 
  91     public void testUnBox() {
  92         long sum = Arrays.asList(1, 2, 3, 4, 5).stream().mapToInt(i -> (int) i).sum();
  93         assertEquals(sum, 15);
  94     }
  95 
  96     public void testFlags() {
  97         assertTrue(IntStream.range(1, 10).boxed().spliterator()
  98                       .hasCharacteristics(Spliterator.SORTED | Spliterator.DISTINCT));
  99         assertFalse(IntStream.of(1, 10).boxed().spliterator()
 100                       .hasCharacteristics(Spliterator.SORTED));
 101         assertFalse(IntStream.of(1, 10).boxed().spliterator()
 102                       .hasCharacteristics(Spliterator.DISTINCT));
 103 
 104         assertTrue(IntStream.range(1, 10).asLongStream().spliterator()
 105                       .hasCharacteristics(Spliterator.SORTED | Spliterator.DISTINCT));
 106         assertFalse(IntStream.of(1, 10).asLongStream().spliterator()
 107                       .hasCharacteristics(Spliterator.SORTED));
 108         assertFalse(IntStream.of(1, 10).asLongStream().spliterator()
 109                       .hasCharacteristics(Spliterator.DISTINCT));
 110 
 111         assertTrue(IntStream.range(1, 10).asDoubleStream().spliterator()
 112                       .hasCharacteristics(Spliterator.SORTED | Spliterator.DISTINCT));
 113         assertFalse(IntStream.of(1, 10).asDoubleStream().spliterator()
 114                       .hasCharacteristics(Spliterator.SORTED));
 115         assertFalse(IntStream.of(1, 10).asDoubleStream().spliterator()
 116                       .hasCharacteristics(Spliterator.DISTINCT));
 117     }
 118 
 119     public void testToArray() {
 120         {
 121             int[] array =  IntStream.range(1, 10).map(i -> i * 2).toArray();
 122             assertEquals(array, new int[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
 123         }
 124 
 125         {
 126             int[] array =  IntStream.range(1, 10).parallel().map(i -> i * 2).toArray();
 127             assertEquals(array, new int[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
 128         }
 129     }
 130 
 131     public void testSort() {
 132         Random r = new Random();
 133 
 134         int[] content = IntStream.generate(() -> r.nextInt(100)).limit(10).toArray();
 135         int[] sortedContent = content.clone();
 136         Arrays.sort(sortedContent);
 137 
 138         {
 139             int[] array =  Arrays.stream(content).sorted().toArray();
 140             assertEquals(array, sortedContent);
 141         }
 142 
 143         {
 144             int[] array =  Arrays.stream(content).parallel().sorted().toArray();
 145             assertEquals(array, sortedContent);
 146         }
 147     }
 148 
 149     public void testSortDistinct() {
 150         {
 151             int[] range = IntStream.range(0, 10).toArray();
 152 
 153             assertEquals(IntStream.range(0, 10).sorted().distinct().toArray(), range);
 154             assertEquals(IntStream.range(0, 10).parallel().sorted().distinct().toArray(), range);
 155 
 156             int[] data = {5, 3, 1, 1, 5, 3, 9, 2, 9, 1, 0, 8};
 157             int[] expected = {0, 1, 2, 3, 5, 8, 9};
 158             assertEquals(IntStream.of(data).sorted().distinct().toArray(), expected);
 159             assertEquals(IntStream.of(data).parallel().sorted().distinct().toArray(), expected);
 160         }
 161 
 162         {
 163             int[] input = new Random().ints(100, -10, 10).map(x -> x+Integer.MAX_VALUE).toArray();
 164             TreeSet<Long> longs = new TreeSet<>();
 165             for(int i : input) longs.add((long)i);
 166             long[] expectedLongs = longs.stream().mapToLong(Long::longValue).toArray();
 167             assertEquals(IntStream.of(input).sorted().asLongStream().sorted().distinct().toArray(),
 168                          expectedLongs);
 169 
 170             TreeSet<Double> doubles = new TreeSet<>();
 171             for(int i : input) doubles.add((double)i);
 172             double[] expectedDoubles = doubles.stream().mapToDouble(Double::doubleValue).toArray();
 173             assertEquals(IntStream.of(input).sorted().distinct().asDoubleStream()
 174                          .sorted().distinct().toArray(), expectedDoubles);
 175         }
 176     }
 177 
 178     public void testSortSort() {
 179         Random r = new Random();
 180 
 181         int[] content = IntStream.generate(() -> r.nextInt(100)).limit(10).toArray();
 182         int[] sortedContent = content.clone();
 183         Arrays.sort(sortedContent);
 184 
 185         {
 186             int[] array =  Arrays.stream(content).sorted().sorted().toArray();
 187             assertEquals(array, sortedContent);
 188         }
 189 
 190         {
 191             int[] array =  Arrays.stream(content).parallel().sorted().sorted().toArray();
 192             assertEquals(array, sortedContent);
 193         }
 194     }


< prev index next >