• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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.Random;
30 import java.util.stream.DoubleStream;
31 import java.util.stream.LongStream;
32 
33 import static org.testng.Assert.assertEquals;
34 
35 @Test
36 public class DoublePrimitiveOpsTests {
37 
38     // @@@ tests for double are fragile if relying on equality when accumulating and multiplying values
39 
testUnBox()40     public void testUnBox() {
41         double sum = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0).stream().mapToDouble(i -> i).reduce(0.0, Double::sum);
42         assertEquals(sum, 1.0 + 2.0 + 3.0 + 4.0 + 5.0);
43     }
44 
testToArray()45     public void testToArray() {
46         {
47             double[] array =  LongStream.range(1, 10).asDoubleStream().map(i -> i * 2).toArray();
48             assertEquals(array, new double[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
49         }
50 
51         {
52             double[] array =  LongStream.range(1, 10).parallel().asDoubleStream().map(i -> i * 2).toArray();
53             assertEquals(array, new double[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
54         }
55     }
56 
testSort()57     public void testSort() {
58         Random r = new Random();
59 
60         double[] content = DoubleStream.generate(() -> r.nextDouble()).limit(10).toArray();
61         double[] sortedContent = content.clone();
62         Arrays.sort(sortedContent);
63 
64         {
65             double[] array =  Arrays.stream(content).sorted().toArray();
66             assertEquals(array, sortedContent);
67         }
68 
69         {
70             double[] array =  Arrays.stream(content).parallel().sorted().toArray();
71             assertEquals(array, sortedContent);
72         }
73     }
74 
testSortSort()75     public void testSortSort() {
76         Random r = new Random();
77 
78         double[] content = DoubleStream.generate(() -> r.nextDouble()).limit(10).toArray();
79         double[] sortedContent = content.clone();
80         Arrays.sort(sortedContent);
81 
82         {
83             double[] array =  Arrays.stream(content).sorted().sorted().toArray();
84             assertEquals(array, sortedContent);
85         }
86 
87         {
88             double[] array =  Arrays.stream(content).parallel().sorted().sorted().toArray();
89             assertEquals(array, sortedContent);
90         }
91     }
92 
testLimit()93     public void testLimit() {
94         double[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
95 
96         {
97             double[] actual = DoubleStream.iterate(1.0, i -> i + 1.0).limit(9).toArray();
98             Assert.assertTrue(Arrays.equals(expected, actual));
99         }
100 
101         {
102             double[] actual = LongStream.range(1, 100).parallel().asDoubleStream().limit(9).toArray();
103             Assert.assertTrue(Arrays.equals(expected, actual));
104         }
105     }
106 }
107