Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1250743

Cannot create a BitSet with Integer.MAX_VALUE number of bits.

XMLWordPrintable

    • 1.1
    • sparc
    • solaris_2.4
    • Not verified

      Cannot create a BitSet with Integer.MAX_VALUE number of bits, receive a
      NegativeArrayIndexException. The only valid problem would be an out of memory
      error.

      Steps to reproduce
      Compile and run the attached code
      /* %W% %G%
       * @author Kevin A. Smith
       * BitSet constructor tests. These test creation of a bit set.
       */
      import java.io.PrintStream;
      import java.util.BitSet;

      public class CtorTests implements Test {
         final int SET_SIZE = 64;
         final int MEM_LIMIT = 0x06FFFFFF;

         /* Test interface */
         public Status run( String[] args, PrintStream out ) {

            /* put all tests into a try ... catch block */
            try {
            
               // create bit set with Integer.MAX_VALUE bits
               out.println( "b9: create BitSet with " +
                           Integer.MAX_VALUE +
                           " bits." );
               BitSet b9 = new BitSet( Integer.MAX_VALUE );
               out.println( "b9.size: " + b9.size() );
         
            

            } // end try
            catch( Exception e ) {
               out.println( e + "( " + e.getMessage() +" )" );
            }
            catch( Error e ) {
               out.println( e + "( " + e.getMessage() +" )" );
               
            }
            // End of test
         }

         /* Standalone interface */
         public static void main( String[] argv ) {
            CtorTests test = new CtorTests();
            status = test.run( argv, System.out );
           
            
         }

      }

      The description field as copied from bug report 1251102 follows:

      When creating a BitSet with Integer.MAX_VALUE bits, receive an exception:
      NegativeArraySizeException. This makes no sense! The only error that is sensible
      here is an OutOfMemoryError.

      Steps to reproduce
      Compile and run the attached code

      /* %W% %G%
       * @author Kevin A. Smith
       * BitSet Equivalence Class partitioning, constructor tests.
       * These test creation of a bit set.
       */
      import java.io.PrintStream;

      import java.util.BitSet;

      public class CtorTests2 implements Test {
         final int SET_SIZE = 64;
         final int MAX_ELEMENTS = 33554432;

         /* Test interface */
         public Status run( String[] args, PrintStream out ) {
            int i;
            int setSize = SET_SIZE;
            int maxElements = MAX_ELEMENTS;
            String testRunning= "";

            /* Parse test arguments */
            for( i = 0; i < args.length; ++i ) {

               /* Initial size of BitSet(1) */
               if( args[i].equals( "-SetSize" ) ) {
                  setSize = Integer.parseInt( args[++i] );
               }
               else if( args[i].equals( "-MaxElements" ) ) {
                  maxElements = Integer.parseInt( args[++i] );
               }
               
            }
         
            /* put all tests into a try ... catch block */
            try {

               // Test BitSet0102: create bit set with Integer.MAX_VALUE bits
               testRunning = "BitSet0102";
               try {
                  BitSet bitSet0102 = new BitSet( Integer.MAX_VALUE );
                  out.println( "BitSet0102.size: " + bitSet0102.size() );
                  if( bitSet0102.size() != Integer.MAX_VALUE ) {
                     out.println( "BitSet0102: bad size" );
                  }
               }
               catch( OutOfMemoryError e ) {
                  out.println( "BitSet0102: Not Enough Memory to create" );
               }
         
            } // end try for all tests
            catch( Exception e ) {
               out.println( testRunning + ": " +
                                       e + "( " + e.getMessage() +" )" );
            }
            catch( Error e ) {
               out.println( testRunning + ": " +
                                       e + "( " + e.getMessage() +" )" );
               
            }

            // End of test
            return status;
         }

         /* Standalone interface */
         public static void main( String[] argv ) {
            CtorTests2 test = new CtorTests2();
            status = test.run( argv, System.out );
            
         }

      }


      MORE TEST CODE:

      import java.util.BitSet;

      class BitSetTest {
          public static void main(String[] args) {
      System.out.println("starting tests");
      try {
      BitSet b = new BitSet(Integer.MAX_VALUE);
      throw new RuntimeException();
      } catch (OutOfMemoryError e) {}
      try {
      BitSet b = new BitSet(-100);
      throw new RuntimeException();
      } catch (IllegalArgumentException e) {}
      BitSet b = new BitSet(10);
      try {
      b.get(-1);
      throw new RuntimeException();
      } catch (IndexOutOfBoundsException e) {}
      b.set(100);
      if (b.get(100) != true) throw new RuntimeException();
      try {
      if (b.get(Integer.MAX_VALUE-100000) != false) {
      throw new RuntimeException();
      }
      } catch (OutOfMemoryError e) {}
      try {
      b.set(-1);
      throw new RuntimeException();
      } catch (IndexOutOfBoundsException e) {}
      try {
      b.set(Integer.MAX_VALUE-100000);
      throw new RuntimeException();
      } catch (OutOfMemoryError e) {}
      try {
      b.clear(-1);
      throw new RuntimeException();
      } catch (IndexOutOfBoundsException e) {}
      try {
      b.clear(Integer.MAX_VALUE-100000);
      throw new RuntimeException();
      } catch (OutOfMemoryError e) {}
      b.set(100000);
      if (b.get(100000) != true) throw new RuntimeException();
      System.out.println("completed successfully");
          }
      }

            tlindholsunw Timothy Lindholm (Inactive)
            kasmithsunw Kevin Smith (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: