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 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 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 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); | 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); |