test/java/lang/Integer/Unsigned.java

Print this page
rev 10594 : 8055251: Re-examine Integer.parseInt and Long.parseLong methods


 286             10L,
 287             2147483646L,   // MAX_VALUE - 1
 288             2147483647L,   // MAX_VALUE
 289             2147483648L,   // MAX_VALUE + 1
 290 
 291             MAX_UNSIGNED_INT - 1L,
 292             MAX_UNSIGNED_INT,
 293         };
 294 
 295         for(long value : inRange) {
 296             for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
 297                 String longString = Long.toString(value, radix);
 298                 int intResult = Integer.parseUnsignedInt(longString, radix);
 299 
 300                 if (Integer.toUnsignedLong(intResult) != value) {
 301                     errors++;
 302                     System.err.printf("Bad roundtrip conversion of %d in base %d" +
 303                                       "\tconverting back ''%s'' resulted in %d%n",
 304                                       value, radix, longString,  intResult);
 305                 }











 306             }
 307         }
 308 
 309         String[] outOfRange = {
 310             null,
 311             "",
 312             "-1",
 313             Long.toString(MAX_UNSIGNED_INT + 1L),
 314             Long.toString(Long.MAX_VALUE)
 315         };
 316 
 317         for(String s : outOfRange) {
 318             try {
 319                 int result = Integer.parseUnsignedInt(s);
 320                 errors++; // Should not reach here
 321                 System.err.printf("Unexpected got %d from an unsigned conversion of %s",
 322                                   result, s);
 323             } catch(NumberFormatException nfe) {
 324                 ; // Correct result
 325             }




 286             10L,
 287             2147483646L,   // MAX_VALUE - 1
 288             2147483647L,   // MAX_VALUE
 289             2147483648L,   // MAX_VALUE + 1
 290 
 291             MAX_UNSIGNED_INT - 1L,
 292             MAX_UNSIGNED_INT,
 293         };
 294 
 295         for(long value : inRange) {
 296             for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
 297                 String longString = Long.toString(value, radix);
 298                 int intResult = Integer.parseUnsignedInt(longString, radix);
 299 
 300                 if (Integer.toUnsignedLong(intResult) != value) {
 301                     errors++;
 302                     System.err.printf("Bad roundtrip conversion of %d in base %d" +
 303                                       "\tconverting back ''%s'' resulted in %d%n",
 304                                       value, radix, longString,  intResult);
 305                 }
 306 
 307                 // test offset based parse method
 308                 intResult = Integer.parseUnsignedInt("prefix" + longString + "suffix",
 309                         "prefix".length(), "prefix".length() + longString.length(), radix);
 310 
 311                 if (Integer.toUnsignedLong(intResult) != value) {
 312                     errors++;
 313                     System.err.printf("Bad roundtrip conversion of %d in base %d" +
 314                             "\tconverting back ''%s'' resulted in %d%n",
 315                             value, radix, longString,  intResult);
 316                 }
 317             }
 318         }
 319 
 320         String[] outOfRange = {
 321             null,
 322             "",
 323             "-1",
 324             Long.toString(MAX_UNSIGNED_INT + 1L),
 325             Long.toString(Long.MAX_VALUE)
 326         };
 327 
 328         for(String s : outOfRange) {
 329             try {
 330                 int result = Integer.parseUnsignedInt(s);
 331                 errors++; // Should not reach here
 332                 System.err.printf("Unexpected got %d from an unsigned conversion of %s",
 333                                   result, s);
 334             } catch(NumberFormatException nfe) {
 335                 ; // Correct result
 336             }