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.function.DoubleConsumer;
29 import java.util.function.Function;
30 import java.util.function.IntConsumer;
31 import java.util.function.LongConsumer;
32 
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Test;
35 
36 import static java.util.stream.LambdaTestHelpers.assertContents;
37 import static java.util.stream.LambdaTestHelpers.countTo;
38 import static org.testng.Assert.assertEquals;
39 
40 @Test
41 public class NodeBuilderTest {
42 
43     List<Integer> sizes = Arrays.asList(0, 1, 4, 16, 256,
44                                         1023, 1024, 1025,
45                                         2047, 2048, 2049,
46                                         1024 * 32 - 1, 1024 * 32, 1024 * 32 + 1);
47 
48     @DataProvider(name = "Node.Builder")
createNodeBuilders()49     public Object[][] createNodeBuilders() {
50         List<List<Integer>> ls = new ArrayList<>();
51         for (int size : sizes) {
52             ls.add(countTo(size));
53         }
54 
55         List<Function<Integer, Node.Builder<Integer>>> ms = Arrays.asList(
56                 s -> Nodes.builder(),
57                 s -> Nodes.builder(s, LambdaTestHelpers.integerArrayGenerator)
58         );
59 
60         Object[][] params = new Object[ls.size() * ms.size()][];
61         int i = 0;
62         for (List<Integer> l : ls) {
63             for (Function<Integer, Node.Builder<Integer>> m : ms) {
64                 params[i++] = new Object[]{l, m};
65             }
66         }
67 
68         return params;
69     }
70 
71     @Test(dataProvider = "Node.Builder", groups = { "serialization-hostile" })
testIteration(List<Integer> l, Function<Integer, Node.Builder<Integer>> m)72     public void testIteration(List<Integer> l, Function<Integer, Node.Builder<Integer>> m) {
73         Node.Builder<Integer> nb = m.apply(l.size());
74         nb.begin(l.size());
75         for (Integer i : l) {
76             nb.accept(i);
77         }
78         nb.end();
79 
80         Node<Integer> n = nb.build();
81         assertEquals(n.count(), l.size());
82 
83         {
84             List<Integer> _l = new ArrayList<>();
85             n.forEach(_l::add);
86 
87             assertContents(_l, l);
88         }
89     }
90 
91     // Node.Builder.OfInt
92 
93     @DataProvider(name = "Node.Builder<Integer>")
createIntNodeBuilders()94     public Object[][] createIntNodeBuilders() {
95         List<List<Integer>> ls = new ArrayList<>();
96         for (int size : sizes) {
97             ls.add(countTo(size));
98         }
99 
100         List<Function<Integer, Node.Builder<Integer>>> ms = Arrays.asList(
101                 s -> Nodes.intBuilder(),
102                 s -> Nodes.intBuilder(s)
103         );
104 
105         Object[][] params = new Object[ls.size() * ms.size()][];
106         int i = 0;
107         for (List<Integer> l : ls) {
108             for (Function<Integer, Node.Builder<Integer>> m : ms) {
109                 params[i++] = new Object[]{l, m};
110             }
111         }
112 
113         return params;
114     }
115 
116     @Test(dataProvider = "Node.Builder<Integer>", groups = { "serialization-hostile" })
testIntIteration(List<Integer> l, Function<Integer, Node.Builder.OfInt> m)117     public void testIntIteration(List<Integer> l, Function<Integer, Node.Builder.OfInt> m) {
118         Node.Builder.OfInt nb = m.apply(l.size());
119         nb.begin(l.size());
120         for (Integer i : l) {
121             nb.accept((int) i);
122         }
123         nb.end();
124 
125         Node.OfInt n = nb.build();
126         assertEquals(n.count(), l.size());
127 
128         {
129             List<Integer> _l = new ArrayList<>();
130             n.forEach((IntConsumer) _l::add);
131 
132             assertContents(_l, l);
133         }
134 
135     }
136 
137     // Node.Builder.OfLong
138 
139     @DataProvider(name = "Node.Builder<Long>")
createLongNodeBuilders()140     public Object[][] createLongNodeBuilders() {
141         List<List<Long>> ls = new ArrayList<>();
142         for (int size : sizes) {
143             List<Long> l = new ArrayList<>();
144             for (long i = 0; i < size; i++) {
145                 l.add(i);
146             }
147             ls.add(l);
148         }
149 
150         List<Function<Integer, Node.Builder<Long>>> ms = Arrays.asList(
151                 s -> Nodes.longBuilder(),
152                 s -> Nodes.longBuilder(s)
153         );
154 
155         Object[][] params = new Object[ls.size() * ms.size()][];
156         int i = 0;
157         for (List<Long> l : ls) {
158             for (Function<Integer, Node.Builder<Long>> m : ms) {
159                 params[i++] = new Object[]{l, m};
160             }
161         }
162 
163         return params;
164     }
165 
166     @Test(dataProvider = "Node.Builder<Long>")
testLongIteration(List<Long> l, Function<Integer, Node.Builder.OfLong> m)167     public void testLongIteration(List<Long> l, Function<Integer, Node.Builder.OfLong> m) {
168         Node.Builder.OfLong nb = m.apply(l.size());
169         nb.begin(l.size());
170         for (Long i : l) {
171             nb.accept((long) i);
172         }
173         nb.end();
174 
175         Node.OfLong n = nb.build();
176         assertEquals(n.count(), l.size());
177 
178         {
179             List<Long> _l = new ArrayList<>();
180             n.forEach((LongConsumer) _l::add);
181 
182             assertContents(_l, l);
183         }
184 
185     }
186 
187     // Node.Builder.OfDouble
188 
189     @DataProvider(name = "Node.Builder<Double>")
createDoubleNodeBuilders()190     public Object[][] createDoubleNodeBuilders() {
191         List<List<Double>> ls = new ArrayList<>();
192         for (int size : sizes) {
193             List<Double> l = new ArrayList<>();
194             for (long i = 0; i < size; i++) {
195                 l.add((double) i);
196             }
197             ls.add(l);
198         }
199 
200         List<Function<Integer, Node.Builder<Double>>> ms = Arrays.asList(
201                 s -> Nodes.doubleBuilder(),
202                 s -> Nodes.doubleBuilder(s)
203         );
204 
205         Object[][] params = new Object[ls.size() * ms.size()][];
206         int i = 0;
207         for (List<Double> l : ls) {
208             for (Function<Integer, Node.Builder<Double>> m : ms) {
209                 params[i++] = new Object[]{l, m};
210             }
211         }
212 
213         return params;
214     }
215 
216     @Test(dataProvider = "Node.Builder<Double>")
testDoubleIteration(List<Double> l, Function<Integer, Node.Builder.OfDouble> m)217     public void testDoubleIteration(List<Double> l, Function<Integer, Node.Builder.OfDouble> m) {
218         Node.Builder.OfDouble nb = m.apply(l.size());
219         nb.begin(l.size());
220         for (Double i : l) {
221             nb.accept((double) i);
222         }
223         nb.end();
224 
225         Node.OfDouble n = nb.build();
226         assertEquals(n.count(), l.size());
227 
228         {
229             List<Double> _l = new ArrayList<>();
230             n.forEach((DoubleConsumer) _l::add);
231 
232             assertContents(_l, l);
233         }
234 
235     }
236 }
237