BigIntegerTest.java revision 495c740217c5ab4ed117c030e9bf72919c47809e
1/*
2 * Copyright (C) 2010 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 libcore.java.math;
18
19import java.math.BigInteger;
20
21public class BigIntegerTest extends junit.framework.TestCase {
22    // http://code.google.com/p/android/issues/detail?id=18452
23    public void test_hashCode() {
24        BigInteger firstBig  = new BigInteger("30003543667898318853");
25        BigInteger secondBig = new BigInteger("3298535022597");
26        BigInteger andedBigs = firstBig.and(secondBig);
27        BigInteger toCompareBig = BigInteger.valueOf(andedBigs.longValue());
28        assertEquals(andedBigs, toCompareBig);
29        assertEquals(andedBigs.hashCode(), toCompareBig.hashCode());
30    }
31
32    // http://b/2981072 - off-by-one error in BigInteger.valueOf
33    public void test_valueOf() {
34        // I assume here that we'll never cache more than 1024 values.
35        // (At the moment, we cache -1 to 10.)
36        for (int i = -1024; i <= 1024; ++i) {
37            assertEquals(i, BigInteger.valueOf(i).intValue());
38        }
39    }
40
41    // http://code.google.com/p/android/issues/detail?id=7036
42    public void test_invalidBigIntegerStringConversions() {
43        // Check we don't disallow related reasonable strings...
44        new BigInteger("1", 10);
45        new BigInteger("1a", 16);
46        new BigInteger("-1", 10);
47        new BigInteger("-1a", 16);
48        new BigInteger("\u0661", 10);
49        new BigInteger("\u0661a", 16);
50        new BigInteger("-\u0661", 10);
51        new BigInteger("-\u0661a", 16);
52        // This is allowed from Java 7 on.
53        new BigInteger("+1");
54        // Now check the invalid cases...
55        try {
56            new BigInteger("-a"); // no digits from other bases.
57            fail();
58        } catch (NumberFormatException expected) {
59        }
60        try {
61            new BigInteger("-1a"); // no trailing digits from other bases.
62            fail();
63        } catch (NumberFormatException expected) {
64        }
65        try {
66            new BigInteger("-1 hello"); // no trailing junk at all.
67            fail();
68        } catch (NumberFormatException expected) {
69        }
70        try {
71            new BigInteger("--1"); // only one sign.
72            fail();
73        } catch (NumberFormatException expected) {
74        }
75        try {
76            new BigInteger(""); // at least one digit.
77            fail();
78        } catch (NumberFormatException expected) {
79        }
80        try {
81            new BigInteger("-"); // at least one digit, even after a sign.
82            fail();
83        } catch (NumberFormatException expected) {
84        }
85    }
86}
87