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 );
}
}
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 );
}
}
- duplicates
-
JDK-1250743 Cannot create a BitSet with Integer.MAX_VALUE number of bits.
-
- Closed
-