changeset 11723:85380cfe4f0f

8076442: Cannot fully read BitSet.stream() if bit Integer.MAX_VALUE is set Reviewed-by: alanb, henryjen
author chegar
date Tue, 07 Apr 2015 10:33:08 +0100
parents b1eeff4dac1b
children 7808bef3104c
files src/java.base/share/classes/java/util/BitSet.java test/java/util/BitSet/BitSetStreamTest.java
diffstat 2 files changed, 5 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/BitSet.java	Mon Apr 06 13:41:10 2015 -0700
+++ b/src/java.base/share/classes/java/util/BitSet.java	Tue Apr 07 10:33:08 2015 +0100
@@ -1229,7 +1229,7 @@
             public int nextInt() {
                 if (next != -1) {
                     int ret = next;
-                    next = nextSetBit(next+1);
+                    next = (next == Integer.MAX_VALUE) ? -1 : nextSetBit(next+1);
                     return ret;
                 } else {
                     throw new NoSuchElementException();
--- a/test/java/util/BitSet/BitSetStreamTest.java	Mon Apr 06 13:41:10 2015 -0700
+++ b/test/java/util/BitSet/BitSetStreamTest.java	Tue Apr 07 10:33:08 2015 +0100
@@ -43,7 +43,7 @@
 /**
  * @test
  * @summary test BitSet stream
- * @bug 8012645
+ * @bug 8012645 8076442
  * @run testng BitSetStreamTest
  */
 public class BitSetStreamTest {
@@ -70,6 +70,7 @@
         { "step 5", IntStream.range(0, 255).map(f -> f * 5) },
         { "step 7", IntStream.range(0, 255).map(f -> f * 7) },
         { "1, 10, 100, 1000", IntStream.of(1, 10, 100, 1000) },
+        { "max int", IntStream.of(Integer.MAX_VALUE) },
         { "25 fibs", IntStream.generate(new Fibs()).limit(25) }
     };
 
@@ -106,6 +107,8 @@
         for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
             assertTrue(it.hasNext());
             assertEquals(it.nextInt(), i);
+            if (i == Integer.MAX_VALUE)
+                break; // or (i+1) would overflow
         }
         assertFalse(it.hasNext());
     }