test/java/lang/Long/Unsigned.java

Print this page
rev 9020 : 8030814: Long.parseUnsignedLong should throw exception on too large input
Summary: Change test for overflow of unsigned long
Reviewed-by: TBD
Contributed-by: Dmitry Nadezhin <dmitry.nadezhin@oracle.com>


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4504839 4215269 6322074
  27  * @summary Basic tests for unsigned operations
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import java.math.*;
  32 
  33 public class Unsigned {
  34     public static void main(String... args) {
  35         int errors = 0;
  36 
  37         errors += testRoundtrip();
  38         errors += testByteToUnsignedLong();
  39         errors += testShortToUnsignedLong();
  40         errors += testUnsignedCompare();
  41         errors += testToStringUnsigned();
  42         errors += testParseUnsignedLong();
  43         errors += testDivideAndRemainder();
  44 
  45         if (errors > 0) {
  46             throw new RuntimeException(errors + " errors found in unsigned operations.");


 293         }
 294 
 295         String[] outOfRange = {
 296             null,
 297             "",
 298             "-1",
 299             TWO.pow(64).toString(),
 300         };
 301 
 302         for(String s : outOfRange) {
 303             try {
 304                 long result = Long.parseUnsignedLong(s);
 305                 errors++; // Should not reach here
 306                 System.err.printf("Unexpected got %d from an unsigned conversion of %s",
 307                                   result, s);
 308             } catch(NumberFormatException nfe) {
 309                 ; // Correct result
 310             }
 311         }
 312 





































 313         return errors;
 314     }
 315 
 316     private static int testDivideAndRemainder() {
 317         int errors = 0;
 318         long MAX_UNSIGNED_INT = Integer.toUnsignedLong(0xffff_ffff);
 319 
 320         BigInteger[] inRange = {
 321             BigInteger.valueOf(0L),
 322             BigInteger.valueOf(1L),
 323             BigInteger.valueOf(10L),
 324             BigInteger.valueOf(2147483646L),   // Integer.MAX_VALUE - 1
 325             BigInteger.valueOf(2147483647L),   // Integer.MAX_VALUE
 326             BigInteger.valueOf(2147483648L),   // Integer.MAX_VALUE + 1
 327 
 328             BigInteger.valueOf(MAX_UNSIGNED_INT - 1L),
 329             BigInteger.valueOf(MAX_UNSIGNED_INT),
 330 
 331             BigInteger.valueOf(Long.MAX_VALUE - 1L),
 332             BigInteger.valueOf(Long.MAX_VALUE),




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4504839 4215269 6322074 8030814
  27  * @summary Basic tests for unsigned operations
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import java.math.*;
  32 
  33 public class Unsigned {
  34     public static void main(String... args) {
  35         int errors = 0;
  36 
  37         errors += testRoundtrip();
  38         errors += testByteToUnsignedLong();
  39         errors += testShortToUnsignedLong();
  40         errors += testUnsignedCompare();
  41         errors += testToStringUnsigned();
  42         errors += testParseUnsignedLong();
  43         errors += testDivideAndRemainder();
  44 
  45         if (errors > 0) {
  46             throw new RuntimeException(errors + " errors found in unsigned operations.");


 293         }
 294 
 295         String[] outOfRange = {
 296             null,
 297             "",
 298             "-1",
 299             TWO.pow(64).toString(),
 300         };
 301 
 302         for(String s : outOfRange) {
 303             try {
 304                 long result = Long.parseUnsignedLong(s);
 305                 errors++; // Should not reach here
 306                 System.err.printf("Unexpected got %d from an unsigned conversion of %s",
 307                                   result, s);
 308             } catch(NumberFormatException nfe) {
 309                 ; // Correct result
 310             }
 311         }
 312 
 313         // test case from 8030814 report
 314         try {
 315             String input = "1234567890abcdef1";
 316             long result = Long.parseUnsignedLong(input, 16);
 317             errors++; // Should not reach here
 318             System.err.printf("Unexpected got %d from an unsigned conversion of %s\n",
 319                     result, input);
 320         } catch (NumberFormatException nfe) {
 321             ; // Correct result
 322         }
 323 
 324         // other tests for out of unsigned long range
 325         BigInteger maxUnsignedLong =
 326                 BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE);
 327         for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
 328             BigInteger quotient = maxUnsignedLong.divide(BigInteger.valueOf(radix));
 329             BigInteger b = quotient.multiply(BigInteger.valueOf(radix + 2));
 330             int cmp = b.compareTo(maxUnsignedLong);
 331             boolean exceptionExpected = cmp > 0;
 332             String s = b.toString(radix);
 333             long result;
 334             try {
 335                 result = Long.parseUnsignedLong(s, radix);
 336                 if (exceptionExpected) {
 337                     System.err.printf("Unexpected result %d for Long.parseUnsignedLong(%s,%d)\n",
 338                             result, s, radix);
 339                     errors++;
 340                 }
 341             } catch (NumberFormatException nfe) {
 342                 if (!exceptionExpected) {
 343                     System.err.printf("Unexpected exception %s for Long.parseUnsignedLong(%s,%d)\n",
 344                             nfe.toString(), s, radix);
 345                     errors++;
 346                 }
 347             }
 348         }
 349 
 350         return errors;
 351     }
 352 
 353     private static int testDivideAndRemainder() {
 354         int errors = 0;
 355         long MAX_UNSIGNED_INT = Integer.toUnsignedLong(0xffff_ffff);
 356 
 357         BigInteger[] inRange = {
 358             BigInteger.valueOf(0L),
 359             BigInteger.valueOf(1L),
 360             BigInteger.valueOf(10L),
 361             BigInteger.valueOf(2147483646L),   // Integer.MAX_VALUE - 1
 362             BigInteger.valueOf(2147483647L),   // Integer.MAX_VALUE
 363             BigInteger.valueOf(2147483648L),   // Integer.MAX_VALUE + 1
 364 
 365             BigInteger.valueOf(MAX_UNSIGNED_INT - 1L),
 366             BigInteger.valueOf(MAX_UNSIGNED_INT),
 367 
 368             BigInteger.valueOf(Long.MAX_VALUE - 1L),
 369             BigInteger.valueOf(Long.MAX_VALUE),