• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015, 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 package test.java.lang.Integer;
25 
26 /*
27  * @test
28  * @bug 8136500
29  * @summary Test Integer.toString method
30  */
31 
32 public class ToString {
33 
main(String[] args)34     public static void main(String[] args) throws Exception {
35         test("-2147483648", Integer.MIN_VALUE);
36         test("2147483647",  Integer.MAX_VALUE);
37         test("0", 0);
38 
39         // Wiggle around the exponentially increasing base.
40         final int LIMIT = (1 << 15);
41         int base = 10000;
42         while (base < Integer.MAX_VALUE / 10) {
43             for (int d = -LIMIT; d < LIMIT; d++) {
44                 int c = base + d;
45                 if (c > 0) {
46                     buildAndTest(c);
47                 }
48             }
49             base *= 10;
50         }
51 
52         for (int c = 1; c < LIMIT; c++) {
53             buildAndTest(Integer.MAX_VALUE - LIMIT + c);
54         }
55     }
56 
buildAndTest(int c)57     private static void buildAndTest(int c) {
58         if (c <= 0) {
59             throw new IllegalArgumentException("Test bug: can only handle positives, " + c);
60         }
61 
62         StringBuilder sbN = new StringBuilder();
63         StringBuilder sbP = new StringBuilder();
64 
65         int t = c;
66         while (t > 0) {
67             char digit = (char) ('0' + (t % 10));
68             sbN.append(digit);
69             sbP.append(digit);
70             t = t / 10;
71         }
72 
73         sbN.append("-");
74         sbN.reverse();
75         sbP.reverse();
76 
77         test(sbN.toString(), -c);
78         test(sbP.toString(), c);
79     }
80 
test(String expected, int value)81     private static void test(String expected, int value) {
82         String actual = Integer.toString(value);
83         if (!expected.equals(actual)) {
84             throw new RuntimeException("Expected " + expected + ", but got " + actual);
85         }
86     }
87 }
88