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.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
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 
testSum()41     public void testSum() {
42         long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).sum();
43         assertEquals(sum, 20);
44     }
45 
testMap()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 
testParSum()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" })
testTee()57     public void testTee() {
58         int[] teeSum = new int[1];
59         long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).peek(i -> { teeSum[0] = teeSum[0] + i; }).sum();
60         assertEquals(teeSum[0], sum);
61     }
62 
63     @Test(groups = { "serialization-hostile" })
testForEach()64     public void testForEach() {
65         int[] sum = new int[1];
66         IntStream.range(1, 10).filter(i -> i % 2 == 0).forEach(i -> { sum[0] = sum[0] + i; });
67         assertEquals(sum[0], 20);
68     }
69 
70     @Test(groups = { "serialization-hostile" })
testParForEach()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 
testBox()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 
testUnBox()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 
testToArray()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 
testSort()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 
testSortSort()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     }
135 
testSequential()136     public void testSequential() {
137 
138         int[] expected = IntStream.range(1, 1000).toArray();
139 
140         class AssertingConsumer implements IntConsumer {
141             private final int[] array;
142             int offset;
143 
144             AssertingConsumer(int[] array) {
145                 this.array = array;
146             }
147 
148             @Override
149             public void accept(int value) {
150                 assertEquals(array[offset++], value);
151             }
152 
153             public int getCount() { return offset; }
154         }
155 
156         {
157             AssertingConsumer consumer = new AssertingConsumer(expected);
158             IntStream.range(1, 1000).sequential().forEach(consumer);
159             assertEquals(expected.length, consumer.getCount());
160         }
161 
162         {
163             AssertingConsumer consumer = new AssertingConsumer(expected);
164             IntStream.range(1, 1000).parallel().sequential().forEach(consumer);
165             assertEquals(expected.length, consumer.getCount());
166         }
167     }
168 
testLimit()169     public void testLimit() {
170         int[] expected = IntStream.range(1, 10).toArray();
171 
172         {
173             int[] actual = IntStream.iterate(1, i -> i + 1).limit(9).toArray();
174             Assert.assertTrue(Arrays.equals(expected, actual));
175         }
176 
177         {
178             int[] actual = IntStream.range(1, 100).parallel().limit(9).toArray();
179             Assert.assertTrue(Arrays.equals(expected, actual));
180         }
181     }
182 
183 }
184