changeset 6659:b8879209d4f6

Fixed bug where {Int}SpinedBuffer was reporting one more than the actual natural number of splits.
author psandoz
date Sat, 01 Dec 2012 15:33:46 +0100
parents 509db112d745
children bd2d3a3e1f8b
files src/share/classes/java/util/stream/op/SpinedBuffer.java src/share/classes/java/util/stream/primitive/IntSpinedBuffer.java test-ng/tests/org/openjdk/tests/java/util/stream/op/SpinedBufferTest.java
diffstat 3 files changed, 180 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/op/SpinedBuffer.java	Sat Dec 01 15:33:46 2012 +0100
+++ b/src/share/classes/java/util/stream/op/SpinedBuffer.java	Sat Dec 01 15:33:46 2012 +0100
@@ -399,7 +399,7 @@
             }
             else {
                 return notLastChunk
-                       ? spineIndex - spineOffset + (chunkIndex > 1 ? 1 : 0)
+                       ? spineIndex - spineOffset - 1 + (chunkIndex > 0 ? 1 : 0)
                        : (endOffset - offset > 1) ? 1 : 0;
             }
         }
--- a/src/share/classes/java/util/stream/primitive/IntSpinedBuffer.java	Sat Dec 01 15:33:46 2012 +0100
+++ b/src/share/classes/java/util/stream/primitive/IntSpinedBuffer.java	Sat Dec 01 15:33:46 2012 +0100
@@ -435,7 +435,7 @@
             }
             else {
                 return notLastChunk
-                       ? spineIndex - spineOffset + (chunkIndex > 1 ? 1 : 0)
+                       ? spineIndex - spineOffset - 1 + (chunkIndex > 0 ? 1 : 0)
                        : (endOffset - offset > 1) ? 1 : 0;
             }
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-ng/tests/org/openjdk/tests/java/util/stream/op/SpinedBufferTest.java	Sat Dec 01 15:33:46 2012 +0100
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2012, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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 org.openjdk.tests.java.util.stream.op;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import java.lang.System;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.function.Block;
+import java.util.stream.Spliterator;
+import java.util.stream.op.Node;
+import java.util.stream.op.Nodes;
+import java.util.stream.op.SpinedBuffer;
+import java.util.stream.op.SpinedBufferHelper;
+import java.util.stream.primitive.IntIterator;
+import java.util.stream.primitive.IntSpinedBuffer;
+import java.util.stream.primitive.IntSpliterator;
+import java.util.stream.primitive.Primitives;
+
+import static org.testng.Assert.assertEquals;
+
+@Test
+public class SpinedBufferTest {
+
+    // Create sizes around the boundary of spines
+    ArrayList<Integer> sizes = Primitives.range(0, 32).map(i -> (int)SpinedBufferHelper.totalSizeForSpineIndex(i)).
+            boxed().uniqueElements().
+            flatMap((Block<? super Integer> s, Integer i) -> {
+                s.accept(i - 2);
+                s.accept(i - 1);
+                s.accept(i);
+                s.accept(i + 1);
+                s.accept(i + 2);
+            }).
+            into(new ArrayList<Integer>());
+
+    @Test
+    public void testSpineSize() {
+        for (int i = 0; i < 32; i++) {
+            int s = 0;
+            for (int j = 0; j <= i; j++) {
+                s += SpinedBufferHelper.sizeForSpineIndex(j);
+            }
+
+            assertEquals(SpinedBufferHelper.totalSizeForSpineIndex(i), s);
+        }
+    }
+
+    // SpinedBuffer
+
+    @DataProvider(name = "SpinedBuffer")
+    public Object[][] createSpinedBuffer() {
+        List<Object[]> params = new ArrayList<>();
+
+        for (int size : sizes) {
+            int[] array = Primitives.range(0, size).toArray();
+            SpinedBuffer<Integer> sb = new SpinedBuffer<>();
+            Arrays.stream(array).boxed().forEach(sb);
+
+            params.add(new Object[]{array, sb});
+        }
+
+        return params.toArray(new Object[0][]);
+    }
+
+    @Test(dataProvider = "SpinedBuffer")
+    public void testSize(int[] array, SpinedBuffer<Integer> sb) {
+        assertEquals(sb.size(), array.length);
+
+        int lastSpineIndex = SpinedBufferHelper.spinesSizeForSize(array.length) ;
+        assertEquals(sb.spliterator().getNaturalSplits(), lastSpineIndex == 0 ? 1 : lastSpineIndex);
+    }
+
+    @Test(dataProvider = "SpinedBuffer")
+    public void testLastSplit(int[] array, SpinedBuffer<Integer> sb) {
+        Spliterator<Integer> lastSplit = sb.spliterator();
+        int n = lastSplit.getNaturalSplits();
+        int splitSizeSum = 0;
+        for (int i = 0; i < n; i++) {
+            splitSizeSum += lastSplit.split().getSizeIfKnown();
+        }
+        long sizeOfLastSplit = lastSplit.getSizeIfKnown();
+        splitSizeSum += sizeOfLastSplit;
+
+        assertEquals(splitSizeSum, array.length);
+
+        List<Integer> contentOfLastSplit = new ArrayList<>();
+        Iterator<Integer> it = lastSplit.iterator();
+        if (it.hasNext()) {
+            contentOfLastSplit.add(it.next());
+        }
+        lastSplit.forEach(contentOfLastSplit::add);
+
+        assertEquals(contentOfLastSplit.size(), sizeOfLastSplit);
+
+        List<Integer> end = Arrays.stream(array).boxed().skip(array.length - sizeOfLastSplit).
+                into(new ArrayList<Integer>());
+        assertEquals(contentOfLastSplit, end);
+    }
+
+    // IntSpinedBuffer
+
+    @DataProvider(name = "IntSpinedBuffer")
+    public Object[][] createIntSpinedBuffer() {
+        List<Object[]> params = new ArrayList<>();
+
+        for (int size : sizes) {
+            int[] array = Primitives.range(0, size).toArray();
+            IntSpinedBuffer sb = new IntSpinedBuffer();
+            Arrays.stream(array).forEach(sb);
+
+            params.add(new Object[]{array, sb});
+        }
+
+        return params.toArray(new Object[0][]);
+    }
+
+    @Test(dataProvider = "IntSpinedBuffer")
+    public void testIntSize(int[] array, IntSpinedBuffer sb) {
+        assertEquals(sb.size(), array.length);
+
+        int lastSpineIndex = SpinedBufferHelper.spinesSizeForSize(array.length);
+        assertEquals(sb.spliterator().getNaturalSplits(), lastSpineIndex == 0 ? 1 : lastSpineIndex);
+    }
+
+    @Test(dataProvider = "IntSpinedBuffer")
+    public void testIntLastSplit(int[] array, IntSpinedBuffer sb) {
+        IntSpliterator lastSplit = sb.spliterator();
+        int n = lastSplit.getNaturalSplits();
+        int splitSizeSum = 0;
+        for (int i = 0; i < n; i++) {
+            splitSizeSum += lastSplit.split().getSizeIfKnown();
+        }
+        long sizeOfLastSplit = lastSplit.getSizeIfKnown();
+        splitSizeSum += sizeOfLastSplit;
+
+        assertEquals(splitSizeSum, array.length);
+
+        List<Integer> contentOfLastSplit = new ArrayList<>();
+        IntIterator it = lastSplit.iterator();
+        if (it.hasNext()) {
+            contentOfLastSplit.add(it.nextInt());
+        }
+        lastSplit.forEach(contentOfLastSplit::add);
+
+        assertEquals(contentOfLastSplit.size(), sizeOfLastSplit);
+
+        List<Integer> end = Arrays.stream(array).boxed().skip(array.length - sizeOfLastSplit).
+                into(new ArrayList<Integer>());
+        assertEquals(contentOfLastSplit, end);
+    }
+}