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