/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.util.stream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.DoubleConsumer; import java.util.function.Function; import java.util.function.IntConsumer; import java.util.function.LongConsumer; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import static java.util.stream.LambdaTestHelpers.assertContents; import static java.util.stream.LambdaTestHelpers.countTo; import static org.testng.Assert.assertEquals; @Test public class NodeBuilderTest { List sizes = Arrays.asList(0, 1, 4, 16, 256, 1023, 1024, 1025, 2047, 2048, 2049, 1024 * 32 - 1, 1024 * 32, 1024 * 32 + 1); @DataProvider(name = "Node.Builder") public Object[][] createNodeBuilders() { List> ls = new ArrayList<>(); for (int size : sizes) { ls.add(countTo(size)); } List>> ms = Arrays.asList( s -> Nodes.builder(), s -> Nodes.builder(s, LambdaTestHelpers.integerArrayGenerator) ); Object[][] params = new Object[ls.size() * ms.size()][]; int i = 0; for (List l : ls) { for (Function> m : ms) { params[i++] = new Object[]{l, m}; } } return params; } @Test(dataProvider = "Node.Builder", groups = { "serialization-hostile" }) public void testIteration(List l, Function> m) { Node.Builder nb = m.apply(l.size()); nb.begin(l.size()); for (Integer i : l) { nb.accept(i); } nb.end(); Node n = nb.build(); assertEquals(n.count(), l.size()); { List _l = new ArrayList<>(); n.forEach(_l::add); assertContents(_l, l); } } // Node.Builder.OfInt @DataProvider(name = "Node.Builder") public Object[][] createIntNodeBuilders() { List> ls = new ArrayList<>(); for (int size : sizes) { ls.add(countTo(size)); } List>> ms = Arrays.asList( s -> Nodes.intBuilder(), s -> Nodes.intBuilder(s) ); Object[][] params = new Object[ls.size() * ms.size()][]; int i = 0; for (List l : ls) { for (Function> m : ms) { params[i++] = new Object[]{l, m}; } } return params; } @Test(dataProvider = "Node.Builder", groups = { "serialization-hostile" }) public void testIntIteration(List l, Function m) { Node.Builder.OfInt nb = m.apply(l.size()); nb.begin(l.size()); for (Integer i : l) { nb.accept((int) i); } nb.end(); Node.OfInt n = nb.build(); assertEquals(n.count(), l.size()); { List _l = new ArrayList<>(); n.forEach((IntConsumer) _l::add); assertContents(_l, l); } } // Node.Builder.OfLong @DataProvider(name = "Node.Builder") public Object[][] createLongNodeBuilders() { List> ls = new ArrayList<>(); for (int size : sizes) { List l = new ArrayList<>(); for (long i = 0; i < size; i++) { l.add(i); } ls.add(l); } List>> ms = Arrays.asList( s -> Nodes.longBuilder(), s -> Nodes.longBuilder(s) ); Object[][] params = new Object[ls.size() * ms.size()][]; int i = 0; for (List l : ls) { for (Function> m : ms) { params[i++] = new Object[]{l, m}; } } return params; } @Test(dataProvider = "Node.Builder") public void testLongIteration(List l, Function m) { Node.Builder.OfLong nb = m.apply(l.size()); nb.begin(l.size()); for (Long i : l) { nb.accept((long) i); } nb.end(); Node.OfLong n = nb.build(); assertEquals(n.count(), l.size()); { List _l = new ArrayList<>(); n.forEach((LongConsumer) _l::add); assertContents(_l, l); } } // Node.Builder.OfDouble @DataProvider(name = "Node.Builder") public Object[][] createDoubleNodeBuilders() { List> ls = new ArrayList<>(); for (int size : sizes) { List l = new ArrayList<>(); for (long i = 0; i < size; i++) { l.add((double) i); } ls.add(l); } List>> ms = Arrays.asList( s -> Nodes.doubleBuilder(), s -> Nodes.doubleBuilder(s) ); Object[][] params = new Object[ls.size() * ms.size()][]; int i = 0; for (List l : ls) { for (Function> m : ms) { params[i++] = new Object[]{l, m}; } } return params; } @Test(dataProvider = "Node.Builder") public void testDoubleIteration(List l, Function m) { Node.Builder.OfDouble nb = m.apply(l.size()); nb.begin(l.size()); for (Double i : l) { nb.accept((double) i); } nb.end(); Node.OfDouble n = nb.build(); assertEquals(n.count(), l.size()); { List _l = new ArrayList<>(); n.forEach((DoubleConsumer) _l::add); assertContents(_l, l); } } }