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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util.stream;
  26 
  27 import java.util.ArrayList;
  28 import java.util.Arrays;
  29 import java.util.List;
  30 import java.util.function.DoubleConsumer;
  31 import java.util.function.Function;
  32 import java.util.function.IntConsumer;
  33 import java.util.function.LongConsumer;
  34 
  35 import org.testng.annotations.DataProvider;
  36 import org.testng.annotations.Test;
  37 
  38 import static java.util.stream.LambdaTestHelpers.assertContents;
  39 import static java.util.stream.LambdaTestHelpers.countTo;
  40 import static org.testng.Assert.assertEquals;
  41 
  42 @Test
  43 public class NodeBuilderTest {
  44 
  45     List<Integer> sizes = Arrays.asList(0, 1, 4, 16, 256,
  46                                         1023, 1024, 1025,
  47                                         2047, 2048, 2049,
  48                                         1024 * 32 - 1, 1024 * 32, 1024 * 32 + 1);
  49 
  50     @DataProvider(name = "Node.Builder")
  51     public Object[][] createNodeBuilders() {
  52         List<List<Integer>> ls = new ArrayList<>();
  53         for (int size : sizes) {
  54             ls.add(countTo(size));
  55         }
  56 
  57         List<Function<Integer, Node.Builder<Integer>>> ms = Arrays.asList(
  58                 s -> Nodes.builder(),
  59                 s -> Nodes.builder(s, LambdaTestHelpers.integerArrayGenerator)
  60         );
  61 
  62         Object[][] params = new Object[ls.size() * ms.size()][];
  63         int i = 0;
  64         for (List<Integer> l : ls) {
  65             for (Function<Integer, Node.Builder<Integer>> m : ms) {
  66                 params[i++] = new Object[]{l, m};
  67             }
  68         }
  69 
  70         return params;
  71     }
  72 
  73     @Test(dataProvider = "Node.Builder", groups = { "serialization-hostile" })
  74     public void testIteration(List<Integer> l, Function<Integer, Node.Builder<Integer>> m) {
  75         Node.Builder<Integer> nb = m.apply(l.size());
  76         nb.begin(l.size());
  77         for (Integer i : l) {
  78             nb.accept(i);
  79         }
  80         nb.end();
  81 
  82         Node<Integer> n = nb.build();
  83         assertEquals(n.count(), l.size());
  84 
  85         {
  86             List<Integer> _l = new ArrayList<>();
  87             n.forEach(_l::add);
  88 
  89             assertContents(_l, l);
  90         }
  91     }
  92 
  93     // Node.Builder.OfInt
  94 
  95     @DataProvider(name = "Node.Builder<Integer>")
  96     public Object[][] createIntNodeBuilders() {
  97         List<List<Integer>> ls = new ArrayList<>();
  98         for (int size : sizes) {
  99             ls.add(countTo(size));
 100         }
 101 
 102         List<Function<Integer, Node.Builder<Integer>>> ms = Arrays.asList(
 103                 s -> Nodes.intBuilder(),
 104                 s -> Nodes.intBuilder(s)
 105         );
 106 
 107         Object[][] params = new Object[ls.size() * ms.size()][];
 108         int i = 0;
 109         for (List<Integer> l : ls) {
 110             for (Function<Integer, Node.Builder<Integer>> m : ms) {
 111                 params[i++] = new Object[]{l, m};
 112             }
 113         }
 114 
 115         return params;
 116     }
 117 
 118     @Test(dataProvider = "Node.Builder<Integer>", groups = { "serialization-hostile" })
 119     public void testIntIteration(List<Integer> l, Function<Integer, Node.Builder.OfInt> m) {
 120         Node.Builder.OfInt nb = m.apply(l.size());
 121         nb.begin(l.size());
 122         for (Integer i : l) {
 123             nb.accept((int) i);
 124         }
 125         nb.end();
 126 
 127         Node.OfInt n = nb.build();
 128         assertEquals(n.count(), l.size());
 129 
 130         {
 131             List<Integer> _l = new ArrayList<>();
 132             n.forEach((IntConsumer) _l::add);
 133 
 134             assertContents(_l, l);
 135         }
 136 
 137     }
 138 
 139     // Node.Builder.OfLong
 140 
 141     @DataProvider(name = "Node.Builder<Long>")
 142     public Object[][] createLongNodeBuilders() {
 143         List<List<Long>> ls = new ArrayList<>();
 144         for (int size : sizes) {
 145             List<Long> l = new ArrayList<>();
 146             for (long i = 0; i < size; i++) {
 147                 l.add(i);
 148             }
 149             ls.add(l);
 150         }
 151 
 152         List<Function<Integer, Node.Builder<Long>>> ms = Arrays.asList(
 153                 s -> Nodes.longBuilder(),
 154                 s -> Nodes.longBuilder(s)
 155         );
 156 
 157         Object[][] params = new Object[ls.size() * ms.size()][];
 158         int i = 0;
 159         for (List<Long> l : ls) {
 160             for (Function<Integer, Node.Builder<Long>> m : ms) {
 161                 params[i++] = new Object[]{l, m};
 162             }
 163         }
 164 
 165         return params;
 166     }
 167 
 168     @Test(dataProvider = "Node.Builder<Long>")
 169     public void testLongIteration(List<Long> l, Function<Integer, Node.Builder.OfLong> m) {
 170         Node.Builder.OfLong nb = m.apply(l.size());
 171         nb.begin(l.size());
 172         for (Long i : l) {
 173             nb.accept((long) i);
 174         }
 175         nb.end();
 176 
 177         Node.OfLong n = nb.build();
 178         assertEquals(n.count(), l.size());
 179 
 180         {
 181             List<Long> _l = new ArrayList<>();
 182             n.forEach((LongConsumer) _l::add);
 183 
 184             assertContents(_l, l);
 185         }
 186 
 187     }
 188 
 189     // Node.Builder.OfDouble
 190 
 191     @DataProvider(name = "Node.Builder<Double>")
 192     public Object[][] createDoubleNodeBuilders() {
 193         List<List<Double>> ls = new ArrayList<>();
 194         for (int size : sizes) {
 195             List<Double> l = new ArrayList<>();
 196             for (long i = 0; i < size; i++) {
 197                 l.add((double) i);
 198             }
 199             ls.add(l);
 200         }
 201 
 202         List<Function<Integer, Node.Builder<Double>>> ms = Arrays.asList(
 203                 s -> Nodes.doubleBuilder(),
 204                 s -> Nodes.doubleBuilder(s)
 205         );
 206 
 207         Object[][] params = new Object[ls.size() * ms.size()][];
 208         int i = 0;
 209         for (List<Double> l : ls) {
 210             for (Function<Integer, Node.Builder<Double>> m : ms) {
 211                 params[i++] = new Object[]{l, m};
 212             }
 213         }
 214 
 215         return params;
 216     }
 217 
 218     @Test(dataProvider = "Node.Builder<Double>")
 219     public void testDoubleIteration(List<Double> l, Function<Integer, Node.Builder.OfDouble> m) {
 220         Node.Builder.OfDouble nb = m.apply(l.size());
 221         nb.begin(l.size());
 222         for (Double i : l) {
 223             nb.accept((double) i);
 224         }
 225         nb.end();
 226 
 227         Node.OfDouble n = nb.build();
 228         assertEquals(n.count(), l.size());
 229 
 230         {
 231             List<Double> _l = new ArrayList<>();
 232             n.forEach((DoubleConsumer) _l::add);
 233 
 234             assertContents(_l, l);
 235         }
 236 
 237     }
 238 }