1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.tests.java.util.stream;
  26 
  27 import org.testng.Assert;
  28 import org.testng.annotations.Test;
  29 
  30 import java.util.Arrays;
  31 import java.util.List;
  32 import java.util.Random;
  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 
  40 @Test
  41 public class IntPrimitiveOpsTests {
  42 
  43     public void testSum() {
  44         long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).sum();
  45         assertEquals(sum, 20);
  46     }
  47 
  48     public void testMap() {
  49         long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).map(i -> i * 2).sum();
  50         assertEquals(sum, 40);
  51     }
  52 
  53     public void testParSum() {
  54         long sum = IntStream.range(1, 10).parallel().filter(i -> i % 2 == 0).sum();
  55         assertEquals(sum, 20);
  56     }
  57 
  58     @Test(groups = { "serialization-hostile" })
  59     public void testTee() {
  60         int[] teeSum = new int[1];
  61         long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).peek(i -> { teeSum[0] = teeSum[0] + i; }).sum();
  62         assertEquals(teeSum[0], sum);
  63     }
  64 
  65     @Test(groups = { "serialization-hostile" })
  66     public void testForEach() {
  67         int[] sum = new int[1];
  68         IntStream.range(1, 10).filter(i -> i % 2 == 0).forEach(i -> { sum[0] = sum[0] + i; });
  69         assertEquals(sum[0], 20);
  70     }
  71 
  72     @Test(groups = { "serialization-hostile" })
  73     public void testParForEach() {
  74         AtomicInteger ai = new AtomicInteger(0);
  75         IntStream.range(1, 10).parallel().filter(i -> i % 2 == 0).forEach(ai::addAndGet);
  76         assertEquals(ai.get(), 20);
  77     }
  78 
  79     public void testBox() {
  80         List<Integer> l = IntStream.range(1, 10).parallel().boxed().collect(Collectors.toList());
  81         int sum = l.stream().reduce(0, (a, b) -> a + b);
  82         assertEquals(sum, 45);
  83     }
  84 
  85     public void testUnBox() {
  86         long sum = Arrays.asList(1, 2, 3, 4, 5).stream().mapToInt(i -> (int) i).sum();
  87         assertEquals(sum, 15);
  88     }
  89 
  90     public void testToArray() {
  91         {
  92             int[] array =  IntStream.range(1, 10).map(i -> i * 2).toArray();
  93             assertEquals(array, new int[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
  94         }
  95 
  96         {
  97             int[] array =  IntStream.range(1, 10).parallel().map(i -> i * 2).toArray();
  98             assertEquals(array, new int[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
  99         }
 100     }
 101 
 102     public void testSort() {
 103         Random r = new Random();
 104 
 105         int[] content = IntStream.generate(() -> r.nextInt(100)).limit(10).toArray();
 106         int[] sortedContent = content.clone();
 107         Arrays.sort(sortedContent);
 108 
 109         {
 110             int[] array =  Arrays.stream(content).sorted().toArray();
 111             assertEquals(array, sortedContent);
 112         }
 113 
 114         {
 115             int[] array =  Arrays.stream(content).parallel().sorted().toArray();
 116             assertEquals(array, sortedContent);
 117         }
 118     }
 119 
 120     public void testSortSort() {
 121         Random r = new Random();
 122 
 123         int[] content = IntStream.generate(() -> r.nextInt(100)).limit(10).toArray();
 124         int[] sortedContent = content.clone();
 125         Arrays.sort(sortedContent);
 126 
 127         {
 128             int[] array =  Arrays.stream(content).sorted().sorted().toArray();
 129             assertEquals(array, sortedContent);
 130         }
 131 
 132         {
 133             int[] array =  Arrays.stream(content).parallel().sorted().sorted().toArray();
 134             assertEquals(array, sortedContent);
 135         }
 136     }
 137 
 138     public void testSequential() {
 139 
 140         int[] expected = IntStream.range(1, 1000).toArray();
 141 
 142         class AssertingConsumer implements IntConsumer {
 143             private final int[] array;
 144             int offset;
 145 
 146             AssertingConsumer(int[] array) {
 147                 this.array = array;
 148             }
 149 
 150             @Override
 151             public void accept(int value) {
 152                 assertEquals(array[offset++], value);
 153             }
 154 
 155             public int getCount() { return offset; }
 156         }
 157 
 158         {
 159             AssertingConsumer consumer = new AssertingConsumer(expected);
 160             IntStream.range(1, 1000).sequential().forEach(consumer);
 161             assertEquals(expected.length, consumer.getCount());
 162         }
 163 
 164         {
 165             AssertingConsumer consumer = new AssertingConsumer(expected);
 166             IntStream.range(1, 1000).parallel().sequential().forEach(consumer);
 167             assertEquals(expected.length, consumer.getCount());
 168         }
 169     }
 170 
 171     public void testLimit() {
 172         int[] expected = IntStream.range(1, 10).toArray();
 173 
 174         {
 175             int[] actual = IntStream.iterate(1, i -> i + 1).limit(9).toArray();
 176             Assert.assertTrue(Arrays.equals(expected, actual));
 177         }
 178 
 179         {
 180             int[] actual = IntStream.range(1, 100).parallel().limit(9).toArray();
 181             Assert.assertTrue(Arrays.equals(expected, actual));
 182         }
 183     }
 184 
 185 }