1 /* 2 * Copyright (c) 2012, 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.Collections; 28 import java.util.Iterator; 29 import java.util.List; 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 NodeTest extends OpTestCase { 37 38 @DataProvider(name = "nodes") createSizes()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 Integer[] array = new Integer[size]; 44 for (int i = 0; i < array.length; i++) { 45 array[i] = i; 46 } 47 48 List<Node<Integer>> nodes = new ArrayList<>(); 49 nodes.add(Nodes.node(array)); 50 nodes.add(Nodes.node(Arrays.asList(array))); 51 nodes.add(degenerateTree(Arrays.asList(array).iterator())); 52 nodes.add(tree(Arrays.asList(array), l -> Nodes.node(l.toArray(new Integer[l.size()])))); 53 nodes.add(tree(Arrays.asList(array), l -> Nodes.node(l))); 54 nodes.add(fill(array, Nodes.builder(array.length, LambdaTestHelpers.integerArrayGenerator))); 55 nodes.add(fill(array, Nodes.builder())); 56 57 for (int i = 0; i < nodes.size(); i++) { 58 params.add(new Object[]{array, nodes.get(i)}); 59 } 60 61 } 62 63 return params.toArray(new Object[0][]); 64 } 65 fill(Integer[] array, Node.Builder<Integer> nb)66 Node<Integer> fill(Integer[] array, Node.Builder<Integer> nb) { 67 nb.begin(array.length); 68 for (Integer i : array) { 69 nb.accept(i); 70 } 71 nb.end(); 72 return nb.build(); 73 } 74 degenerateTree(Iterator<Integer> it)75 Node<Integer> degenerateTree(Iterator<Integer> it) { 76 if (!it.hasNext()) { 77 return Nodes.node(Collections.emptyList()); 78 } 79 80 Integer i = it.next(); 81 if (it.hasNext()) { 82 return new Nodes.ConcNode<Integer>(Nodes.node(new Integer[] {i}), degenerateTree(it)); 83 } 84 else { 85 return Nodes.node(new Integer[]{i}); 86 } 87 } 88 tree(List<Integer> l, Function<List<Integer>, Node<Integer>> m)89 Node<Integer> tree(List<Integer> l, Function<List<Integer>, Node<Integer>> m) { 90 if (l.size() < 3) { 91 return m.apply(l); 92 } 93 else { 94 return new Nodes.ConcNode<>( 95 tree(l.subList(0, l.size() / 2), m), 96 tree(l.subList(l.size() / 2, l.size()), m )); 97 } 98 } 99 100 @Test(dataProvider = "nodes") testAsArray(Integer[] array, Node<Integer> n)101 public void testAsArray(Integer[] array, Node<Integer> n) { 102 assertEquals(n.asArray(LambdaTestHelpers.integerArrayGenerator), array); 103 } 104 105 @Test(dataProvider = "nodes") testFlattenAsArray(Integer[] array, Node<Integer> n)106 public void testFlattenAsArray(Integer[] array, Node<Integer> n) { 107 assertEquals(Nodes.flatten(n, LambdaTestHelpers.integerArrayGenerator) 108 .asArray(LambdaTestHelpers.integerArrayGenerator), array); 109 } 110 111 @Test(dataProvider = "nodes") testCopyTo(Integer[] array, Node<Integer> n)112 public void testCopyTo(Integer[] array, Node<Integer> n) { 113 Integer[] copy = new Integer[(int) n.count()]; 114 n.copyInto(copy, 0); 115 116 assertEquals(copy, array); 117 } 118 119 @Test(dataProvider = "nodes", groups = { "serialization-hostile" }) testForEach(Integer[] array, Node<Integer> n)120 public void testForEach(Integer[] array, Node<Integer> n) { 121 List<Integer> l = new ArrayList<>((int) n.count()); 122 n.forEach(e -> l.add(e)); 123 124 assertEquals(l.toArray(), array); 125 } 126 127 @Test(dataProvider = "nodes") testStreams(Integer[] array, Node<Integer> n)128 public void testStreams(Integer[] array, Node<Integer> n) { 129 TestData<Integer, Stream<Integer>> data = TestData.Factory.ofRefNode("Node", n); 130 131 exerciseOps(data, s -> s); 132 133 exerciseTerminalOps(data, s -> s.toArray()); 134 } 135 136 @Test(dataProvider = "nodes") testSpliterator(Integer[] array, Node<Integer> n)137 public void testSpliterator(Integer[] array, Node<Integer> n) { 138 SpliteratorTestHelper.testSpliterator(n::spliterator); 139 } 140 141 @Test(dataProvider = "nodes") testTruncate(Integer[] array, Node<Integer> n)142 public void testTruncate(Integer[] array, Node<Integer> n) { 143 int[] nums = new int[] { 0, 1, array.length / 2, array.length - 1, array.length }; 144 for (int start : nums) 145 for (int end : nums) { 146 if (start < 0 || end < 0 || end < start || end > array.length) 147 continue; 148 Node<Integer> slice = n.truncate(start, end, Integer[]::new); 149 Integer[] asArray = slice.asArray(Integer[]::new); 150 for (int k = start; k < end; k++) 151 assertEquals(array[k], asArray[k - start]); 152 } 153 } 154 } 155