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.Spliterator;
32 import java.util.TreeSet;
33 import java.util.concurrent.atomic.AtomicLong;
34 import java.util.function.LongConsumer;
35 import java.util.stream.Collectors;
36 import java.util.stream.LongStream;
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 LongPrimitiveOpsTests {
48 
testSum()49     public void testSum() {
50         long sum = LongStream.range(1, 10).filter(i -> i % 2 == 0).sum();
51         assertEquals(sum, 20);
52     }
53 
testMap()54     public void testMap() {
55         long sum = LongStream.range(1, 10).filter(i -> i % 2 == 0).map(i -> i * 2).sum();
56         assertEquals(sum, 40);
57     }
58 
testParSum()59     public void testParSum() {
60         long sum = LongStream.range(1, 10).parallel().filter(i -> i % 2 == 0).sum();
61         assertEquals(sum, 20);
62     }
63 
64     @Test(groups = { "serialization-hostile" })
testTee()65     public void testTee() {
66         long[] teeSum = new long[1];
67         long sum = LongStream.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" })
testForEach()72     public void testForEach() {
73         long[] sum = new long[1];
74         LongStream.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" })
testParForEach()79     public void testParForEach() {
80         AtomicLong ai = new AtomicLong(0);
81         LongStream.range(1, 10).parallel().filter(i -> i % 2 == 0).forEach(ai::addAndGet);
82         assertEquals(ai.get(), 20);
83     }
84 
testBox()85     public void testBox() {
86         List<Long> l = LongStream.range(1, 10).parallel().boxed().collect(Collectors.toList());
87         long sum = l.stream().reduce(0L, (a, b) -> a + b);
88         assertEquals(sum, 45);
89     }
90 
testUnBox()91     public void testUnBox() {
92         long sum = Arrays.asList(1L, 2L, 3L, 4L, 5L).stream().mapToLong(i -> (long) i).sum();
93         assertEquals(sum, 15);
94     }
95 
testFlags()96     public void testFlags() {
97         assertTrue(LongStream.range(1, 10).boxed().spliterator()
98                       .hasCharacteristics(Spliterator.SORTED | Spliterator.DISTINCT));
99         assertFalse(LongStream.of(1, 10).boxed().spliterator()
100                       .hasCharacteristics(Spliterator.SORTED));
101         assertFalse(LongStream.of(1, 10).boxed().spliterator()
102                       .hasCharacteristics(Spliterator.DISTINCT));
103 
104         assertTrue(LongStream.range(1, 10).asDoubleStream().spliterator()
105                       .hasCharacteristics(Spliterator.SORTED));
106         assertFalse(LongStream.range(1, 10).asDoubleStream().spliterator()
107                       .hasCharacteristics(Spliterator.DISTINCT));
108         assertFalse(LongStream.of(1, 10).asDoubleStream().spliterator()
109                       .hasCharacteristics(Spliterator.SORTED));
110     }
111 
testToArray()112     public void testToArray() {
113         {
114             long[] array =  LongStream.range(1, 10).map(i -> i * 2).toArray();
115             assertEquals(array, new long[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
116         }
117 
118         {
119             long[] array =  LongStream.range(1, 10).parallel().map(i -> i * 2).toArray();
120             assertEquals(array, new long[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
121         }
122     }
123 
testSort()124     public void testSort() {
125         Random r = new Random();
126 
127         long[] content = LongStream.generate(() -> r.nextLong()).limit(10).toArray();
128         long[] sortedContent = content.clone();
129         Arrays.sort(sortedContent);
130 
131         {
132             long[] array =  Arrays.stream(content).sorted().toArray();
133             assertEquals(array, sortedContent);
134         }
135 
136         {
137             long[] array =  Arrays.stream(content).parallel().sorted().toArray();
138             assertEquals(array, sortedContent);
139         }
140     }
141 
testSortDistinct()142     public void testSortDistinct() {
143         {
144             long[] range = LongStream.range(0, 10).toArray();
145 
146             assertEquals(LongStream.range(0, 10).sorted().distinct().toArray(), range);
147             assertEquals(LongStream.range(0, 10).parallel().sorted().distinct().toArray(), range);
148 
149             long[] data = {5, 3, 1, 1, 5, 3, 9, 2, 9, 1, 0, 8};
150             long[] expected = {0, 1, 2, 3, 5, 8, 9};
151             assertEquals(LongStream.of(data).sorted().distinct().toArray(), expected);
152             assertEquals(LongStream.of(data).parallel().sorted().distinct().toArray(), expected);
153         }
154 
155         {
156             long[] input = new Random().longs(100, -10, 10).map(x -> x+Long.MAX_VALUE).toArray();
157 
158             TreeSet<Double> doubles = new TreeSet<>();
159             for(long i : input) doubles.add((double)i);
160             double[] expectedDoubles = doubles.stream().mapToDouble(Double::doubleValue).toArray();
161             assertEquals(LongStream.of(input).sorted().distinct().asDoubleStream()
162                          .sorted().distinct().toArray(), expectedDoubles);
163         }
164     }
165 
testSortSort()166     public void testSortSort() {
167         Random r = new Random();
168 
169         long[] content = LongStream.generate(() -> r.nextLong()).limit(10).toArray();
170         long[] sortedContent = content.clone();
171         Arrays.sort(sortedContent);
172 
173         {
174             long[] array =  Arrays.stream(content).sorted().sorted().toArray();
175             assertEquals(array, sortedContent);
176         }
177 
178         {
179             long[] array =  Arrays.stream(content).parallel().sorted().sorted().toArray();
180             assertEquals(array, sortedContent);
181         }
182     }
183 
testSequential()184     public void testSequential() {
185 
186         long[] expected = LongStream.range(1, 1000).toArray();
187 
188         class AssertingConsumer implements LongConsumer {
189             private final long[] array;
190             int offset;
191 
192             AssertingConsumer(long[] array) {
193                 this.array = array;
194             }
195 
196             @Override
197             public void accept(long value) {
198                 assertEquals(array[offset++], value);
199             }
200 
201             public int getCount() { return offset; }
202         }
203 
204         {
205             AssertingConsumer consumer = new AssertingConsumer(expected);
206             LongStream.range(1, 1000).sequential().forEach(consumer);
207             assertEquals(expected.length, consumer.getCount());
208         }
209 
210         {
211             AssertingConsumer consumer = new AssertingConsumer(expected);
212             LongStream.range(1, 1000).parallel().sequential().forEach(consumer);
213             assertEquals(expected.length, consumer.getCount());
214         }
215     }
216 
testLimit()217     public void testLimit() {
218         long[] expected = LongStream.range(1, 10).toArray();
219 
220         {
221             long[] actual = LongStream.iterate(1, i -> i + 1).limit(9).toArray();
222             Assert.assertTrue(Arrays.equals(expected, actual));
223         }
224 
225         {
226             long[] actual = LongStream.range(1, 100).parallel().limit(9).toArray();
227             Assert.assertTrue(Arrays.equals(expected, actual));
228         }
229     }
230 
231 }
232