changeset 8363:9251c488cf27

Prepare for JDK-8012645 TL integration
author henryjen
date Wed, 24 Apr 2013 23:10:19 -0700
parents e0868a1de269
children 4c67fc5c66ce
files src/share/classes/java/util/Random.java src/share/classes/java/util/jar/JarFile.java test-ng/tests/org/openjdk/tests/java/util/BitsetStreamTest.java test/java/util/BitSet/BitSetStreamTest.java test/java/util/Random/RandomStreamTest.java test/java/util/zip/ZipFile/StreamZipEntriesTest.java
diffstat 6 files changed, 87 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Random.java	Wed Apr 24 10:42:58 2013 -0700
+++ b/src/share/classes/java/util/Random.java	Wed Apr 24 23:10:19 2013 -0700
@@ -29,7 +29,6 @@
 import java.util.stream.DoubleStream;
 import java.util.stream.IntStream;
 import java.util.stream.LongStream;
-import java.util.stream.Streams;
 
 import sun.misc.Unsafe;
 
--- a/src/share/classes/java/util/jar/JarFile.java	Wed Apr 24 10:42:58 2013 -0700
+++ b/src/share/classes/java/util/jar/JarFile.java	Wed Apr 24 23:10:19 2013 -0700
@@ -29,6 +29,8 @@
 import java.lang.ref.SoftReference;
 import java.net.URL;
 import java.util.*;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
 import java.util.zip.*;
 import java.security.CodeSigner;
 import java.security.cert.Certificate;
@@ -251,6 +253,22 @@
         };
     }
 
+    @Override
+    public Stream<JarEntry> stream() {
+        final Enumeration<JarEntry> entries = entries();
+        return StreamSupport.stream(Spliterators.spliterator(new Iterator<JarEntry>() {
+            @Override
+            public boolean hasNext() {
+                return entries.hasMoreElements();
+            }
+
+            @Override
+            public JarEntry next() {
+                return entries.nextElement();
+            }
+        }, size(), Spliterator.ORDERED));
+    }
+
     private class JarFileEntry extends JarEntry {
         JarFileEntry(ZipEntry ze) {
             super(ze);
--- a/test-ng/tests/org/openjdk/tests/java/util/BitsetStreamTest.java	Wed Apr 24 10:42:58 2013 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,70 +0,0 @@
-/*
- * 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;
-
-import java.util.PrimitiveIterator;
-import java.util.stream.Stream;
-import java.util.stream.StreamTestDataProvider;
-import org.testng.annotations.Test;
-
-import java.util.BitSet;
-import java.util.stream.Collectors;
-import java.util.stream.TestData;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-
-/**
- * BitsetStreamTest
- *
- * @author Brian Goetz
- */
-@Test
-public class BitsetStreamTest {
-    public void testBitsetStream() {
-        assertEquals(0, new BitSet().stream().reduce(0, (s, i) -> s+1));
-        BitSet bs = new BitSet();
-        bs.set(3);
-        assertEquals(1, bs.stream().reduce(0, (s, i) -> s + 1));
-    }
-
-    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
-    public void testBitsetStream(String name, TestData<Integer, Stream<Integer>> data) {
-        BitSet bs = new BitSet();
-        for (int i : data.stream().mapToInt(e -> (int) e).toArray())
-            bs.set(i);
-
-        assertEquals(bs.cardinality(), data.stream().collect(Collectors.toSet()).size());
-        assertEquals(bs.cardinality(), bs.stream().reduce(0, (s, i) -> s+1));
-
-        PrimitiveIterator.OfInt it = bs.stream().iterator();
-        for (int i=bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
-            assertTrue(it.hasNext());
-            assertEquals(it.nextInt(), i);
-        }
-        assertFalse(it.hasNext());
-    }
-}
--- a/test/java/util/BitSet/BitSetStreamTest.java	Wed Apr 24 10:42:58 2013 -0700
+++ b/test/java/util/BitSet/BitSetStreamTest.java	Wed Apr 24 23:10:19 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * 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
@@ -21,6 +21,7 @@
  * questions.
  */
 
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 import java.lang.Integer;
@@ -28,40 +29,85 @@
 import java.lang.System;
 import java.util.BitSet;
 import java.util.OptionalInt;
+import java.util.PrimitiveIterator;
 import java.util.Random;
+import java.util.function.IntSupplier;
+import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
 /**
  * @test
+ * @summary test BitSet stream
+ * @bug 8012645
  * @run testng BitSetStreamTest
- * @summary test BitSet stream
  */
 public class BitSetStreamTest {
+    static class Fibs implements IntSupplier {
+        private int n1 = 0;
+        private int n2 = 1;
+
+        static int fibs(int n) {
+            Fibs f = new Fibs();
+            while (n-- > 0) f.getAsInt();
+            return f.getAsInt();
+        }
+
+        public int getAsInt() { int s = n1; n1 = n2; n2 = s + n1; return s; }
+    }
+
+    private static final Object[][] testcases = new Object[][] {
+        { "none", IntStream.empty() },
+        { "index 0", IntStream.of(0) },
+        { "index 255", IntStream.of(255) },
+        { "every bit", IntStream.range(0, 255) },
+        { "step 2", IntStream.range(0, 255, 2) },
+        { "step 3", IntStream.range(0, 255, 3) },
+        { "step 5", IntStream.range(0, 255, 5) },
+        { "step 7", IntStream.range(0, 255, 7) },
+        { "1, 10, 100, 1000", IntStream.of(1, 10, 100, 1000) },
+        { "25 fibs", IntStream.generate(new Fibs()).limit(25) }
+    };
+
+    @DataProvider(name = "cases")
+    public static Object[][] produceCases() {
+        return testcases;
+    }
 
     @Test
-    public void testEdgeCases() {
-        final IntStream empty = new BitSet().stream();
-        empty.forEach(i -> fail("stream from empty bitset should be empty"));
+    public void testFibs() {
+        Fibs f = new Fibs();
+        assertEquals(0, f.getAsInt());
+        assertEquals(1, f.getAsInt());
+        assertEquals(1, f.getAsInt());
+        assertEquals(2, f.getAsInt());
+        assertEquals(3, f.getAsInt());
+        assertEquals(5, f.getAsInt());
+        assertEquals(8, f.getAsInt());
+        assertEquals(13, f.getAsInt());
+        assertEquals(987, Fibs.fibs(16));
+    }
 
-        final IntStream one = new BitSet(1).stream(); // 0001
-        one.forEach(i -> assertEquals(i, 0)); // bit at index 0 is set
+    @Test(dataProvider = "cases")
+    public void testBitsetStream(String name, IntStream data) {
+        BitSet bs = new BitSet();
+        long setBits = data.distinct()
+                           .peek(i -> bs.set(i))
+                           .count();
 
-        final IntStream two = new BitSet(1).stream(); // 0010
-        two.forEach(i -> assertEquals(i, 1)); // bit at index 1 is set
+        assertEquals(bs.cardinality(), setBits);
+        assertEquals(bs.cardinality(), bs.stream().reduce(0, (s, i) -> s+1));
 
-        final IntStream four = new BitSet(1).stream(); // 0100
-        four.forEach(i -> assertEquals(i, 2)); // bit at index 2 is set
-
-        final IntStream six = new BitSet(0x06).stream(); // 0110
-        six.forEach(i -> {
-            // only bits at index 1 and 2 are set
-            if (i != 1 && i != 2) {
-                fail("unexpected bit set at index " + i);
-            }
-        });
+        PrimitiveIterator.OfInt it = bs.stream().iterator();
+        for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
+            assertTrue(it.hasNext());
+            assertEquals(it.nextInt(), i);
+        }
+        assertFalse(it.hasNext());
     }
 
     @Test
@@ -86,5 +132,4 @@
             }
         }
     }
-
 }
--- a/test/java/util/Random/RandomStreamTest.java	Wed Apr 24 10:42:58 2013 -0700
+++ b/test/java/util/Random/RandomStreamTest.java	Wed Apr 24 23:10:19 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * 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
--- a/test/java/util/zip/ZipFile/StreamZipEntriesTest.java	Wed Apr 24 10:42:58 2013 -0700
+++ b/test/java/util/zip/ZipFile/StreamZipEntriesTest.java	Wed Apr 24 23:10:19 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
+ * 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
@@ -32,6 +32,7 @@
 import java.lang.Object;
 import java.lang.System;
 import java.util.jar.JarFile;
+import java.util.jar.JarEntry;
 import java.util.stream.Stream;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
@@ -58,7 +59,7 @@
     @Test
     public void testStreamJar() throws IOException {
         try (JarFile jf = new JarFile(new File(System.getProperty("test.src", "."), "input.jar"))) {
-            jf.stream().forEach(e -> assertTrue(e instanceof ZipEntry));
+            jf.stream().forEach(e -> assertTrue(e instanceof JarEntry));
 
             Object elements[] = jf.stream().toArray();
             assertEquals(3, elements.length);