Path: blob/master/test/jdk/java/lang/Integer/ToString.java
41149 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 813650026* @summary Test Integer.toString method27*/2829public class ToString {3031public static void main(String[] args) throws Exception {32test("-2147483648", Integer.MIN_VALUE);33test("2147483647", Integer.MAX_VALUE);34test("0", 0);3536// Wiggle around the exponentially increasing base.37final int LIMIT = (1 << 15);38int base = 10000;39while (base < Integer.MAX_VALUE / 10) {40for (int d = -LIMIT; d < LIMIT; d++) {41int c = base + d;42if (c > 0) {43buildAndTest(c);44}45}46base *= 10;47}4849for (int c = 1; c < LIMIT; c++) {50buildAndTest(Integer.MAX_VALUE - LIMIT + c);51}52}5354private static void buildAndTest(int c) {55if (c <= 0) {56throw new IllegalArgumentException("Test bug: can only handle positives, " + c);57}5859StringBuilder sbN = new StringBuilder();60StringBuilder sbP = new StringBuilder();6162int t = c;63while (t > 0) {64char digit = (char) ('0' + (t % 10));65sbN.append(digit);66sbP.append(digit);67t = t / 10;68}6970sbN.append("-");71sbN.reverse();72sbP.reverse();7374test(sbN.toString(), -c);75test(sbP.toString(), c);76}7778private static void test(String expected, int value) {79String actual = Integer.toString(value);80if (!expected.equals(actual)) {81throw new RuntimeException("Expected " + expected + ", but got " + actual);82}83}84}858687