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>

@@ -21,11 +21,11 @@
  * questions.
  */
 
 /*
  * @test
- * @bug 4504839 4215269 6322074
+ * @bug 4504839 4215269 6322074 8030814
  * @summary Basic tests for unsigned operations
  * @author Joseph D. Darcy
  */
 
 import java.math.*;

@@ -308,10 +308,47 @@
             } catch(NumberFormatException nfe) {
                 ; // Correct result
             }
         }
 
+        // test case from 8030814 report
+        try {
+            String input = "1234567890abcdef1";
+            long result = Long.parseUnsignedLong(input, 16);
+            errors++; // Should not reach here
+            System.err.printf("Unexpected got %d from an unsigned conversion of %s\n",
+                    result, input);
+        } catch (NumberFormatException nfe) {
+            ; // Correct result
+        }
+
+        // other tests for out of unsigned long range
+        BigInteger maxUnsignedLong =
+                BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE);
+        for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
+            BigInteger quotient = maxUnsignedLong.divide(BigInteger.valueOf(radix));
+            BigInteger b = quotient.multiply(BigInteger.valueOf(radix + 2));
+            int cmp = b.compareTo(maxUnsignedLong);
+            boolean exceptionExpected = cmp > 0;
+            String s = b.toString(radix);
+            long result;
+            try {
+                result = Long.parseUnsignedLong(s, radix);
+                if (exceptionExpected) {
+                    System.err.printf("Unexpected result %d for Long.parseUnsignedLong(%s,%d)\n",
+                            result, s, radix);
+                    errors++;
+                }
+            } catch (NumberFormatException nfe) {
+                if (!exceptionExpected) {
+                    System.err.printf("Unexpected exception %s for Long.parseUnsignedLong(%s,%d)\n",
+                            nfe.toString(), s, radix);
+                    errors++;
+                }
+            }
+        }
+
         return errors;
     }
 
     private static int testDivideAndRemainder() {
         int errors = 0;