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.Spliterator; 31 import java.util.stream.DoubleStream; 32 import java.util.stream.LongStream; 33 34 import static org.testng.Assert.assertEquals; 35 36 import static org.testng.Assert.assertFalse; 37 import static org.testng.Assert.assertTrue; 38 39 /** 40 * @test 41 * @bug 8153293 42 */ 43 @Test 44 public class DoublePrimitiveOpsTests { 45 46 // @@@ tests for double are fragile if relying on equality when accumulating and multiplying values 47 testUnBox()48 public void testUnBox() { 49 double sum = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0).stream().mapToDouble(i -> i).reduce(0.0, Double::sum); 50 assertEquals(sum, 1.0 + 2.0 + 3.0 + 4.0 + 5.0); 51 } 52 testFlags()53 public void testFlags() { 54 assertTrue(LongStream.range(1, 10).asDoubleStream().boxed().spliterator() 55 .hasCharacteristics(Spliterator.SORTED)); 56 assertFalse(DoubleStream.of(1, 10).boxed().spliterator() 57 .hasCharacteristics(Spliterator.SORTED)); 58 } 59 testToArray()60 public void testToArray() { 61 { 62 double[] array = LongStream.range(1, 10).asDoubleStream().map(i -> i * 2).toArray(); 63 assertEquals(array, new double[]{2, 4, 6, 8, 10, 12, 14, 16, 18}); 64 } 65 66 { 67 double[] array = LongStream.range(1, 10).parallel().asDoubleStream().map(i -> i * 2).toArray(); 68 assertEquals(array, new double[]{2, 4, 6, 8, 10, 12, 14, 16, 18}); 69 } 70 } 71 testSort()72 public void testSort() { 73 Random r = new Random(); 74 75 double[] content = DoubleStream.generate(() -> r.nextDouble()).limit(10).toArray(); 76 double[] sortedContent = content.clone(); 77 Arrays.sort(sortedContent); 78 79 { 80 double[] array = Arrays.stream(content).sorted().toArray(); 81 assertEquals(array, sortedContent); 82 } 83 84 { 85 double[] array = Arrays.stream(content).parallel().sorted().toArray(); 86 assertEquals(array, sortedContent); 87 } 88 } 89 testSortDistinct()90 public void testSortDistinct() { 91 { 92 double[] range = LongStream.range(0, 10).asDoubleStream().toArray(); 93 94 assertEquals(LongStream.range(0, 10).asDoubleStream().sorted().distinct().toArray(), range); 95 assertEquals(LongStream.range(0, 10).asDoubleStream().parallel().sorted().distinct().toArray(), range); 96 97 double[] data = {5, 3, 1, 1, 5, Double.NaN, 3, 9, Double.POSITIVE_INFINITY, 98 Double.NEGATIVE_INFINITY, 2, 9, 1, 0, 8, Double.NaN, -0.0}; 99 double[] expected = {Double.NEGATIVE_INFINITY, -0.0, 0, 1, 2, 3, 5, 8, 9, 100 Double.POSITIVE_INFINITY, Double.NaN}; 101 assertEquals(DoubleStream.of(data).sorted().distinct().toArray(), expected); 102 assertEquals(DoubleStream.of(data).parallel().sorted().distinct().toArray(), expected); 103 } 104 } 105 testSortSort()106 public void testSortSort() { 107 Random r = new Random(); 108 109 double[] content = DoubleStream.generate(() -> r.nextDouble()).limit(10).toArray(); 110 double[] sortedContent = content.clone(); 111 Arrays.sort(sortedContent); 112 113 { 114 double[] array = Arrays.stream(content).sorted().sorted().toArray(); 115 assertEquals(array, sortedContent); 116 } 117 118 { 119 double[] array = Arrays.stream(content).parallel().sorted().sorted().toArray(); 120 assertEquals(array, sortedContent); 121 } 122 } 123 testLimit()124 public void testLimit() { 125 double[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 126 127 { 128 double[] actual = DoubleStream.iterate(1.0, i -> i + 1.0).limit(9).toArray(); 129 Assert.assertTrue(Arrays.equals(expected, actual)); 130 } 131 132 { 133 double[] actual = LongStream.range(1, 100).parallel().asDoubleStream().limit(9).toArray(); 134 Assert.assertTrue(Arrays.equals(expected, actual)); 135 } 136 } 137 } 138