1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Elena Semukhina
19 */
20
21package org.apache.harmony.tests.java.math;
22
23import java.math.BigInteger;
24
25import junit.framework.TestCase;
26
27/**
28 * Class:   java.math.BigInteger
29 * Method: hashCode()
30 */
31public class BigIntegerHashCodeTest extends TestCase {
32    /**
33     * Test hash codes for the same object
34     */
35    public void testSameObject() {
36        String value1 = "12378246728727834290276457386374882976782849";
37        String value2 = "-5634562095872038262928728727834290276457386374882976782849";
38        BigInteger aNumber1 = new BigInteger(value1);
39        BigInteger aNumber2 = new BigInteger(value2);
40        int code1 = aNumber1.hashCode();
41        aNumber1.add(aNumber2).shiftLeft(125);
42        aNumber1.subtract(aNumber2).shiftRight(125);
43        aNumber1.multiply(aNumber2).toByteArray();
44        aNumber1.divide(aNumber2).bitLength();
45        aNumber1.gcd(aNumber2).pow(7);
46        int code2 = aNumber1.hashCode();
47        assertTrue("hash codes for the same object differ", code1 == code2);
48    }
49
50    /**
51     * Test hash codes for equal objects.
52     */
53    public void testEqualObjects() {
54        String value1 = "12378246728727834290276457386374882976782849";
55        String value2 = "12378246728727834290276457386374882976782849";
56        BigInteger aNumber1 = new BigInteger(value1);
57        BigInteger aNumber2 = new BigInteger(value2);
58        int code1 = aNumber1.hashCode();
59        int code2 = aNumber2.hashCode();
60        if (aNumber1.equals(aNumber2)) {
61            assertTrue("hash codes for equal objects are unequal", code1 == code2);
62        }
63    }
64
65    /**
66     * Test hash codes for unequal objects.
67     * The codes are unequal.
68     */
69    public void testUnequalObjectsUnequal() {
70        String value1 = "12378246728727834290276457386374882976782849";
71        String value2 = "-5634562095872038262928728727834290276457386374882976782849";
72        BigInteger aNumber1 = new BigInteger(value1);
73        BigInteger aNumber2 = new BigInteger(value2);
74        int code1 = aNumber1.hashCode();
75        int code2 = aNumber2.hashCode();
76        if (!aNumber1.equals(aNumber2)) {
77            assertTrue("hash codes for unequal objects are equal", code1 != code2);
78        }
79    }
80}
81