1 /*
   2  * Copyright 2001-2007 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 4489146 5017980
  27  * @summary tests String constructors of BigInteger
  28  * @author Joseph D. Darcy
  29  */
  30 import java.math.*;
  31 
  32 public class StringConstructor {
  33 
  34 
  35     public static void main(String [] argv) {
  36         // Good strings
  37         constructWithoutError("0", 0L);
  38         constructWithoutError("000000000000000000", 0L);
  39         constructWithoutError("1", 1L);
  40         constructWithoutError("-1", -1L);
  41         constructWithoutError("+1", +1L);
  42         constructWithoutError( "123456789123456789", 123456789123456789L);
  43         constructWithoutError("+123456789123456789", 123456789123456789L);
  44         constructWithoutError("-123456789123456789", -123456789123456789L);
  45         constructWithoutError(Integer.toString(Integer.MIN_VALUE),
  46                               (long)Integer.MIN_VALUE);
  47         constructWithoutError(Integer.toString(Integer.MAX_VALUE),
  48                               (long)Integer.MAX_VALUE);
  49         constructWithoutError(Long.toString(Long.MIN_VALUE),
  50                               Long.MIN_VALUE);
  51         constructWithoutError(Long.toString(Long.MAX_VALUE),
  52                               Long.MAX_VALUE);
  53 
  54         // Bad strings
  55         constructWithError("");
  56         constructWithError("-");
  57         constructWithError("+");
  58         constructWithError("--");
  59         constructWithError("++");
  60         constructWithError("-000-0");
  61         constructWithError("+000+0");
  62         constructWithError("+000-0");
  63         constructWithError("--1234567890");
  64         constructWithError("++1234567890");
  65         constructWithError("-0-12345678");
  66         constructWithError("+0+12345678");
  67         constructWithError("--12345678-12345678-12345678");
  68         constructWithError("++12345678+12345678+12345678");
  69         constructWithError("12345-");
  70         constructWithError("12345+");
  71     }
  72 
  73     // this method adapted from ../BigDecimal/StringConstructor.java
  74     private static void constructWithError(String badString) {
  75         try {
  76             BigInteger bi = new BigInteger(badString);
  77             throw new RuntimeException(badString + " accepted");
  78         } catch(NumberFormatException e) {
  79         }
  80     }
  81 
  82     private static void constructWithoutError(String goodString, long value) {
  83             BigInteger bi = new BigInteger(goodString);
  84             if(bi.longValue() != value) {
  85                 System.err.printf("From ``%s'' expected %d, got %s.\n", goodString, value, bi);
  86                 throw new RuntimeException();
  87             }
  88     }
  89 
  90 }