1 /*
   2  * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4136371 5017980 6576055
  27  * @summary Test Long.decode method
  28  * @author madbot
  29  * @author Joseph D. Darcy
  30  */
  31 
  32 import java.math.BigInteger;
  33 
  34 /**
  35  * There are six methods in java.lang.Integer which transform strings
  36  * into a long or Long value:
  37  *
  38  * public Long(String s)
  39  * public static Long decode(String nm)
  40  * public static long parseLong(String s, int radix)
  41  * public static long parseLong(String s)
  42  * public static Long valueOf(String s, int radix)
  43  * public static Long valueOf(String s)
  44  *
  45  * The other five methods are tested elsewhere.
  46  */
  47 public class Decode {
  48 
  49     private static void check(String val, long expected) {
  50         long n = (Long.decode(val)).longValue();
  51         if (n != expected)
  52             throw new RuntimeException("Long.decode failed. String:" +
  53                                                 val + " Result:" + n);
  54     }
  55 
  56     private static void checkFailure(String val, String message) {
  57         try {
  58             long n = (Long.decode(val)).longValue();
  59             throw new RuntimeException(message);
  60         } catch (NumberFormatException e) { /* Okay */}
  61     }
  62 
  63     public static void main(String[] args) throws Exception {
  64         check(new String(""+Long.MIN_VALUE), Long.MIN_VALUE);
  65         check(new String(""+Long.MAX_VALUE), Long.MAX_VALUE);
  66 
  67         check("10",   10L);
  68         check("0x10", 16L);
  69         check("0X10", 16L);
  70         check("010",  8L);
  71         check("#10",  16L);
  72 
  73         check("+10",   10L);
  74         check("+0x10", 16L);
  75         check("+0X10", 16L);
  76         check("+010",  8L);
  77         check("+#10",  16L);
  78 
  79         check("-10",   -10L);
  80         check("-0x10", -16L);
  81         check("-0X10", -16L);
  82         check("-010",  -8L);
  83         check("-#10",  -16L);
  84 
  85         check(Long.toString(Long.MIN_VALUE), Long.MIN_VALUE);
  86         check(Long.toString(Long.MAX_VALUE), Long.MAX_VALUE);
  87 
  88         checkFailure("0x-10",   "Long.decode allows negative sign in wrong position.");
  89         checkFailure("0x+10",   "Long.decode allows positive sign in wrong position.");
  90 
  91         checkFailure("+",       "Raw plus sign allowed.");
  92         checkFailure("-",       "Raw minus sign allowed.");
  93 
  94         checkFailure(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE).toString(),
  95                      "Out of range");
  96         checkFailure(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE).toString(),
  97                      "Out of range");
  98 
  99         checkFailure("", "Empty String");
 100 
 101         try {
 102             Long.decode(null);
 103             throw new RuntimeException("Long.decode(null) expected to throw NPE");
 104         } catch (NullPointerException npe) {/* Okay */}
 105     }
 106 }