1/*
2 * Copyright (c) 2013, 2017, 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 */
23package java.util.stream;
24
25import java.util.ArrayList;
26import java.util.Arrays;
27import java.util.List;
28import java.util.PrimitiveIterator;
29import java.util.Spliterators;
30import java.util.SpliteratorTestHelper;
31import java.util.function.Function;
32
33import org.testng.annotations.DataProvider;
34import org.testng.annotations.Test;
35
36@Test
37public class LongNodeTest extends OpTestCase {
38
39    @DataProvider(name = "nodes")
40    public Object[][] createSizes() {
41        List<Object[]> params = new ArrayList<>();
42
43        for (int size : Arrays.asList(0, 1, 4, 15, 16, 17, 127, 128, 129, 1000)) {
44            long[] array = new long[size];
45            for (int i = 0; i < array.length; i++) {
46                array[i] = i;
47            }
48
49            List<Node<Long>> nodes = new ArrayList<>();
50
51            nodes.add(Nodes.node(array));
52            nodes.add(degenerateTree(Spliterators.iterator(Arrays.spliterator(array))));
53            nodes.add(tree(toList(array), l -> Nodes.node(toLongArray(l))));
54            nodes.add(fill(array, Nodes.longBuilder(array.length)));
55            nodes.add(fill(array, Nodes.longBuilder()));
56
57            for (Node<Long> node : nodes) {
58                params.add(new Object[]{array, node});
59            }
60
61        }
62
63        return params.toArray(new Object[0][]);
64    }
65
66    private static void assertEqualsListLongArray(List<Long> list, long[] array) {
67        assertEquals(list.size(), array.length);
68        for (int i = 0; i < array.length; i++)
69            assertEquals(array[i], (long) list.get(i));
70    }
71
72    private List<Long> toList(long[] a) {
73        List<Long> l = new ArrayList<>();
74        for (long i : a) {
75            l.add(i);
76        }
77
78        return l;
79    }
80
81    private long[] toLongArray(List<Long> l) {
82        long[] a = new long[l.size()];
83
84        int i = 0;
85        for (Long e : l) {
86            a[i++] = e;
87        }
88        return a;
89    }
90
91    private Node.OfLong fill(long[] array, Node.Builder.OfLong nb) {
92        nb.begin(array.length);
93        for (long i : array)
94            nb.accept(i);
95        nb.end();
96        return nb.build();
97    }
98
99    private Node.OfLong degenerateTree(PrimitiveIterator.OfLong it) {
100        if (!it.hasNext()) {
101            return Nodes.node(new long[0]);
102        }
103
104        long i = it.nextLong();
105        if (it.hasNext()) {
106            return new Nodes.ConcNode.OfLong(Nodes.node(new long[] {i}), degenerateTree(it));
107        }
108        else {
109            return Nodes.node(new long[] {i});
110        }
111    }
112
113    private Node.OfLong tree(List<Long> l, Function<List<Long>, Node.OfLong> m) {
114        if (l.size() < 3) {
115            return m.apply(l);
116        }
117        else {
118            return new Nodes.ConcNode.OfLong(
119                    tree(l.subList(0, l.size() / 2), m),
120                    tree(l.subList(l.size() / 2, l.size()), m));
121        }
122    }
123
124    @Test(dataProvider = "nodes")
125    public void testAsArray(long[] array, Node.OfLong n) {
126        assertEquals(n.asPrimitiveArray(), array);
127    }
128
129    @Test(dataProvider = "nodes")
130    public void testFlattenAsArray(long[] array, Node.OfLong n) {
131        assertEquals(Nodes.flattenLong(n).asPrimitiveArray(), array);
132    }
133
134    @Test(dataProvider = "nodes")
135    public void testCopyTo(long[] array, Node.OfLong n) {
136        long[] copy = new long[(int) n.count()];
137        n.copyInto(copy, 0);
138
139        assertEquals(copy, array);
140    }
141
142    @Test(dataProvider = "nodes", groups = { "serialization-hostile" })
143    public void testForEach(long[] array, Node.OfLong n) {
144        List<Long> l = new ArrayList<>((int) n.count());
145        n.forEach((long e) -> {
146            l.add(e);
147        });
148
149        assertEqualsListLongArray(l, array);
150    }
151
152    @Test(dataProvider = "nodes")
153    public void testStreams(long[] array, Node.OfLong n) {
154        TestData.OfLong data = TestData.Factory.ofNode("Node", n);
155
156        exerciseOps(data, s -> s);
157
158        exerciseTerminalOps(data, s -> s.toArray());
159    }
160
161    @Test(dataProvider = "nodes")
162    public void testSpliterator(long[] array, Node.OfLong n) {
163        SpliteratorTestHelper.testLongSpliterator(n::spliterator);
164    }
165
166    @Test(dataProvider = "nodes")
167    public void testTruncate(long[] array, Node.OfLong n) {
168        int[] nums = new int[] { 0, 1, array.length / 2, array.length - 1, array.length };
169        for (int start : nums)
170            for (int end : nums) {
171                if (start < 0 || end < 0 || end < start || end > array.length)
172                    continue;
173                Node.OfLong slice = n.truncate(start, end, Long[]::new);
174                long[] asArray = slice.asPrimitiveArray();
175                for (int k = start; k < end; k++)
176                    assertEquals(array[k], asArray[k - start]);
177            }
178    }
179}
180