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