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