1 /* 2 * Copyright (c) 2001, 2007, Oracle and/or its affiliates. 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 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 4489146 5017980 27 * @summary tests String constructors of BigInteger 28 * @author Joseph D. Darcy 29 */ 30 package test.java.math.BigInteger; 31 32 import java.math.*; 33 34 import org.testng.Assert; 35 import org.testng.annotations.Test; 36 37 // Android-changed: Replace error printing with asserts. 38 public class StringConstructor { 39 40 @Test testStringConstructor()41 public void testStringConstructor() { 42 // Good strings 43 constructWithoutError("0", 0L); 44 constructWithoutError("000000000000000000", 0L); 45 constructWithoutError("1", 1L); 46 constructWithoutError("-1", -1L); 47 constructWithoutError("+1", +1L); 48 constructWithoutError( "123456789123456789", 123456789123456789L); 49 constructWithoutError("+123456789123456789", 123456789123456789L); 50 constructWithoutError("-123456789123456789", -123456789123456789L); 51 constructWithoutError(Integer.toString(Integer.MIN_VALUE), 52 (long)Integer.MIN_VALUE); 53 constructWithoutError(Integer.toString(Integer.MAX_VALUE), 54 (long)Integer.MAX_VALUE); 55 constructWithoutError(Long.toString(Long.MIN_VALUE), 56 Long.MIN_VALUE); 57 constructWithoutError(Long.toString(Long.MAX_VALUE), 58 Long.MAX_VALUE); 59 60 // Bad strings 61 constructWithError(""); 62 constructWithError("-"); 63 constructWithError("+"); 64 constructWithError("--"); 65 constructWithError("++"); 66 constructWithError("-000-0"); 67 constructWithError("+000+0"); 68 constructWithError("+000-0"); 69 constructWithError("--1234567890"); 70 constructWithError("++1234567890"); 71 constructWithError("-0-12345678"); 72 constructWithError("+0+12345678"); 73 constructWithError("--12345678-12345678-12345678"); 74 constructWithError("++12345678+12345678+12345678"); 75 constructWithError("12345-"); 76 constructWithError("12345+"); 77 } 78 79 // this method adapted from ../BigDecimal/StringConstructor.java constructWithError(String badString)80 private static void constructWithError(String badString) { 81 try { 82 BigInteger bi = new BigInteger(badString); 83 Assert.fail(badString + " accepted"); 84 } catch(NumberFormatException e) { 85 // expected 86 } 87 } 88 constructWithoutError(String goodString, long value)89 private static void constructWithoutError(String goodString, long value) { 90 BigInteger bi = new BigInteger(goodString); 91 Assert.assertEquals(bi.longValue(), value, 92 String.format("From ``%s'' expected %d, got %s.\n", goodString, value, bi)); 93 } 94 95 } 96