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