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