1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.core;
18
19import junit.framework.TestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21
22/**
23 * Tests for functionality of class Integer to parse integers.
24 */
25public class ParseIntTest extends TestCase {
26
27    @SmallTest
28    public void testParseInt() throws Exception {
29        assertEquals(0, Integer.parseInt("0", 10));
30        assertEquals(473, Integer.parseInt("473", 10));
31        assertEquals(0, Integer.parseInt("-0", 10));
32        assertEquals(-255, Integer.parseInt("-FF", 16));
33        assertEquals(102, Integer.parseInt("1100110", 2));
34        assertEquals(2147483647, Integer.parseInt("2147483647", 10));
35        assertEquals(-2147483648, Integer.parseInt("-2147483648", 10));
36
37        try {
38            Integer.parseInt("2147483648", 10);
39            fail();
40        } catch (NumberFormatException e) {
41            // ok
42        }
43
44        try {
45            Integer.parseInt("-2147483649", 10);
46            fail();
47        } catch (NumberFormatException e) {
48            // ok
49        }
50
51        // One digit too many
52        try {
53            Integer.parseInt("21474836470", 10);
54            fail();
55        } catch (NumberFormatException e) {
56            // ok
57        }
58
59        try {
60            Integer.parseInt("-21474836480", 10);
61            fail();
62        } catch (NumberFormatException e) {
63            // ok
64        }
65
66        try {
67            Integer.parseInt("21474836471", 10);
68            fail();
69        } catch (NumberFormatException e) {
70            // ok
71        }
72
73        try {
74            Integer.parseInt("-21474836481", 10);
75            fail();
76        } catch (NumberFormatException e) {
77            // ok
78        }
79
80        try {
81            Integer.parseInt("214748364710", 10);
82            fail();
83        } catch (NumberFormatException e) {
84            // ok
85        }
86
87        try {
88            Integer.parseInt("-214748364811", 10);
89            fail();
90        } catch (NumberFormatException e) {
91            // ok
92        }
93
94        try {
95            Integer.parseInt("99", 8);
96            fail();
97        } catch (NumberFormatException e) {
98            // ok
99        }
100
101        try {
102            Integer.parseInt("Kona", 10);
103            fail();
104        } catch (NumberFormatException e) {
105            // ok
106        }
107
108        assertEquals(411787, Integer.parseInt("Kona", 27));
109    }
110}
111