1 /*
   2  * Copyright (c) 2012, 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 IntNodeTest 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             int[] array = new int[size];
  45             for (int i = 0; i < array.length; i++) {
  46                 array[i] = i;
  47             }
  48 
  49             List<Node<Integer>> 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(toIntArray(l))));
  54             nodes.add(fill(array, Nodes.intBuilder(array.length)));
  55             nodes.add(fill(array, Nodes.intBuilder()));
  56 
  57             for (Node<Integer> 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 assertEqualsListIntArray(List<Integer> list, int[] array) {
  67         assertEquals(list.size(), array.length);
  68         for (int i = 0; i < array.length; i++)
  69             assertEquals(array[i], (int) list.get(i));
  70     }
  71 
  72     private List<Integer> toList(int[] a) {
  73         List<Integer> l = new ArrayList<>();
  74         for (int i : a) {
  75             l.add(i);
  76         }
  77 
  78         return l;
  79     }
  80 
  81     private int[] toIntArray(List<Integer> l) {
  82         int[] a = new int[l.size()];
  83 
  84         int i = 0;
  85         for (Integer e : l) {
  86             a[i++] = e;
  87         }
  88         return a;
  89     }
  90 
  91     private Node.OfInt fill(int[] array, Node.Builder.OfInt nb) {
  92         nb.begin(array.length);
  93         for (int i : array)
  94             nb.accept(i);
  95         nb.end();
  96         return nb.build();
  97     }
  98 
  99     private Node.OfInt degenerateTree(PrimitiveIterator.OfInt it) {
 100         if (!it.hasNext()) {
 101             return Nodes.node(new int[0]);
 102         }
 103 
 104         int i = it.nextInt();
 105         if (it.hasNext()) {
 106             return new Nodes.ConcNode.OfInt(Nodes.node(new int[] {i}), degenerateTree(it));
 107         }
 108         else {
 109             return Nodes.node(new int[] {i});
 110         }
 111     }
 112 
 113     private Node.OfInt tree(List<Integer> l, Function<List<Integer>, Node.OfInt> m) {
 114         if (l.size() < 3) {
 115             return m.apply(l);
 116         }
 117         else {
 118             return new Nodes.ConcNode.OfInt(
 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(int[] array, Node.OfInt n) {
 126         assertEquals(n.asPrimitiveArray(), array);
 127     }
 128 
 129     @Test(dataProvider = "nodes")
 130     public void testFlattenAsArray(int[] array, Node.OfInt n) {
 131         assertEquals(Nodes.flattenInt(n).asPrimitiveArray(), array);
 132     }
 133 
 134     @Test(dataProvider = "nodes")
 135     public void testCopyTo(int[] array, Node.OfInt n) {
 136         int[] copy = new int[(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(int[] array, Node.OfInt n) {
 144         List<Integer> l = new ArrayList<>((int) n.count());
 145         n.forEach((int e) -> {
 146             l.add(e);
 147         });
 148 
 149         assertEqualsListIntArray(l, array);
 150     }
 151 
 152     @Test(dataProvider = "nodes")
 153     public void testStreams(int[] array, Node.OfInt n) {
 154         TestData.OfInt data = TestData.Factory.ofNode("Node", n);
 155 
 156         exerciseOps(data, s -> s);
 157         exerciseTerminalOps(data, s -> s.toArray());
 158     }
 159 
 160     @Test(dataProvider = "nodes")
 161     public void testSpliterator(int[] array, Node.OfInt n) {
 162         SpliteratorTestHelper.testIntSpliterator(n::spliterator);
 163     }
 164 
 165     @Test(dataProvider = "nodes")
 166     public void testTruncate(int[] array, Node.OfInt n) {
 167         int[] nums = new int[] { 0, 1, array.length / 2, array.length - 1, array.length };
 168         for (int start : nums)
 169             for (int end : nums) {
 170                 if (start < 0 || end < 0 || end < start || end > array.length)
 171                     continue;
 172                 Node.OfInt slice = n.truncate(start, end, Integer[]::new);
 173                 int[] asArray = slice.asPrimitiveArray();
 174                 for (int k = start; k < end; k++)
 175                     assertEquals(array[k], asArray[k - start]);
 176             }
 177     }
 178 }