1 /* 2 * Copyright (c) 2013, 2017, 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 java.util.stream; 24 25 import java.util.ArrayList; 26 import java.util.Arrays; 27 import java.util.List; 28 import java.util.PrimitiveIterator; 29 import java.util.Spliterators; 30 import java.util.SpliteratorTestHelper; 31 import java.util.function.Function; 32 33 import org.testng.annotations.DataProvider; 34 import org.testng.annotations.Test; 35 36 @Test 37 public class DoubleNodeTest extends OpTestCase { 38 39 @DataProvider(name = "nodes") 40 public Object[][] createSizes() { 41 List<Object[]> params = new ArrayList<>(); 42 43 for (int size : Arrays.asList(0, 1, 4, 15, 16, 17, 127, 128, 129, 1000)) { 44 double[] array = new double[size]; 45 for (int i = 0; i < array.length; i++) { 46 array[i] = i; 47 } 48 49 List<Node<Double>> nodes = new ArrayList<>(); 50 51 nodes.add(Nodes.node(array)); 52 nodes.add(degenerateTree(Spliterators.iterator(Arrays.spliterator(array)))); 53 nodes.add(tree(toList(array), l -> Nodes.node(toDoubleArray(l)))); 54 nodes.add(fill(array, Nodes.doubleBuilder(array.length))); 55 nodes.add(fill(array, Nodes.doubleBuilder())); 56 57 for (Node<Double> node : nodes) { 58 params.add(new Object[]{array, node}); 59 } 60 61 } 62 63 return params.toArray(new Object[0][]); 64 } 65 66 private static void assertEqualsListDoubleArray(List<Double> list, double[] array) { 67 assertEquals(list.size(), array.length); 68 for (int i = 0; i < array.length; i++) 69 assertEquals(array[i], list.get(i)); 70 } 71 72 private List<Double> toList(double[] a) { 73 List<Double> l = new ArrayList<>(); 74 for (double i : a) { 75 l.add(i); 76 } 77 78 return l; 79 } 80 81 private double[] toDoubleArray(List<Double> l) { 82 double[] a = new double[l.size()]; 83 84 int i = 0; 85 for (Double e : l) { 86 a[i++] = e; 87 } 88 return a; 89 } 90 91 private Node.OfDouble fill(double[] array, Node.Builder.OfDouble nb) { 92 nb.begin(array.length); 93 for (double i : array) 94 nb.accept(i); 95 nb.end(); 96 return nb.build(); 97 } 98 99 private Node.OfDouble degenerateTree(PrimitiveIterator.OfDouble it) { 100 if (!it.hasNext()) { 101 return Nodes.node(new double[0]); 102 } 103 104 double i = it.nextDouble(); 105 if (it.hasNext()) { 106 return new Nodes.ConcNode.OfDouble(Nodes.node(new double[] {i}), degenerateTree(it)); 107 } 108 else { 109 return Nodes.node(new double[] {i}); 110 } 111 } 112 113 private Node.OfDouble tree(List<Double> l, Function<List<Double>, Node.OfDouble> m) { 114 if (l.size() < 3) { 115 return m.apply(l); 116 } 117 else { 118 return new Nodes.ConcNode.OfDouble( 119 tree(l.subList(0, l.size() / 2), m), 120 tree(l.subList(l.size() / 2, l.size()), m)); 121 } 122 } 123 124 @Test(dataProvider = "nodes") 125 public void testAsArray(double[] array, Node.OfDouble n) { 126 assertEquals(n.asPrimitiveArray(), array); 127 } 128 129 @Test(dataProvider = "nodes") 130 public void testFlattenAsArray(double[] array, Node.OfDouble n) { 131 assertEquals(Nodes.flattenDouble(n).asPrimitiveArray(), array); 132 } 133 134 @Test(dataProvider = "nodes") 135 public void testCopyTo(double[] array, Node.OfDouble n) { 136 double[] copy = new double[(int) n.count()]; 137 n.copyInto(copy, 0); 138 139 assertEquals(copy, array); 140 } 141 142 @Test(dataProvider = "nodes", groups = { "serialization-hostile" }) 143 public void testForEach(double[] array, Node.OfDouble n) { 144 List<Double> l = new ArrayList<>((int) n.count()); 145 n.forEach((double e) -> { 146 l.add(e); 147 }); 148 149 assertEqualsListDoubleArray(l, array); 150 } 151 152 @Test(dataProvider = "nodes") 153 public void testStreams(double[] array, Node.OfDouble n) { 154 TestData.OfDouble data = TestData.Factory.ofNode("Node", n); 155 156 exerciseOps(data, s -> s); 157 158 exerciseTerminalOps(data, s -> s.toArray()); 159 } 160 161 @Test(dataProvider = "nodes", groups={ "serialization-hostile" }) 162 // throws SOE on serialization of DoubleConcNode[size=1000] 163 public void testSpliterator(double[] array, Node.OfDouble n) { 164 SpliteratorTestHelper.testDoubleSpliterator(n::spliterator); 165 } 166 167 @Test(dataProvider = "nodes") 168 public void testTruncate(double[] array, Node.OfDouble n) { 169 int[] nums = new int[] { 0, 1, array.length / 2, array.length - 1, array.length }; 170 for (int start : nums) 171 for (int end : nums) { 172 if (start < 0 || end < 0 || end < start || end > array.length) 173 continue; 174 Node.OfDouble slice = n.truncate(start, end, Double[]::new); 175 double[] asArray = slice.asPrimitiveArray(); 176 for (int k = start; k < end; k++) 177 assertEquals(array[k], asArray[k - start]); 178 } 179 } 180 }