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 */
23package org.openjdk.tests.java.util.stream;
24
25import org.testng.Assert;
26import org.testng.annotations.Test;
27
28import java.util.Arrays;
29import java.util.List;
30import java.util.Random;
31import java.util.Spliterator;
32import java.util.TreeSet;
33import java.util.concurrent.atomic.AtomicInteger;
34import java.util.function.IntConsumer;
35import java.util.stream.Collectors;
36import java.util.stream.IntStream;
37
38import static org.testng.Assert.assertEquals;
39import static org.testng.Assert.assertFalse;
40import static org.testng.Assert.assertTrue;
41
42/**
43 * @test
44 * @bug 8153293
45 */
46@Test
47public 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() {
66        int[] teeSum = new int[1];
67        long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).peek(i -> { teeSum[0] = teeSum[0] + i; }).sum();
68        assertEquals(teeSum[0], sum);
69    }
70
71    @Test(groups = { "serialization-hostile" })
72    public void testForEach() {
73        int[] sum = new int[1];
74        IntStream.range(1, 10).filter(i -> i % 2 == 0).forEach(i -> { sum[0] = sum[0] + i; });
75        assertEquals(sum[0], 20);
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    }
195
196    public void testSequential() {
197
198        int[] expected = IntStream.range(1, 1000).toArray();
199
200        class AssertingConsumer implements IntConsumer {
201            private final int[] array;
202            int offset;
203
204            AssertingConsumer(int[] array) {
205                this.array = array;
206            }
207
208            @Override
209            public void accept(int value) {
210                assertEquals(array[offset++], value);
211            }
212
213            public int getCount() { return offset; }
214        }
215
216        {
217            AssertingConsumer consumer = new AssertingConsumer(expected);
218            IntStream.range(1, 1000).sequential().forEach(consumer);
219            assertEquals(expected.length, consumer.getCount());
220        }
221
222        {
223            AssertingConsumer consumer = new AssertingConsumer(expected);
224            IntStream.range(1, 1000).parallel().sequential().forEach(consumer);
225            assertEquals(expected.length, consumer.getCount());
226        }
227    }
228
229    public void testLimit() {
230        int[] expected = IntStream.range(1, 10).toArray();
231
232        {
233            int[] actual = IntStream.iterate(1, i -> i + 1).limit(9).toArray();
234            Assert.assertTrue(Arrays.equals(expected, actual));
235        }
236
237        {
238            int[] actual = IntStream.range(1, 100).parallel().limit(9).toArray();
239            Assert.assertTrue(Arrays.equals(expected, actual));
240        }
241    }
242
243}
244