181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes/*
281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  Licensed to the Apache Software Foundation (ASF) under one or more
381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  contributor license agreements.  See the NOTICE file distributed with
481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  this work for additional information regarding copyright ownership.
581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  The ASF licenses this file to You under the Apache License, Version 2.0
681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  (the "License"); you may not use this file except in compliance with
781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  the License.  You may obtain a copy of the License at
881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *
981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
1081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *
1181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  Unless required by applicable law or agreed to in writing, software
1281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  distributed under the License is distributed on an "AS IS" BASIS,
1381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  See the License for the specific language governing permissions and
1581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes *  limitations under the License.
1681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */
1781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes/**
1881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * @author Elena Semukhina
1981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */
2081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes
2181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughespackage org.apache.harmony.tests.java.math;
2281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes
2381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughesimport java.math.BigInteger;
2481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes
2581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughesimport junit.framework.TestCase;
2681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes
2781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes/**
2881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Class:   java.math.BigInteger
2981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Method: hashCode()
3081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */
3181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughespublic class BigIntegerHashCodeTest extends TestCase {
3281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes    /**
3381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes     * Test hash codes for the same object
3481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes     */
3581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes    public void testSameObject() {
3681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        String value1 = "12378246728727834290276457386374882976782849";
3781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        String value2 = "-5634562095872038262928728727834290276457386374882976782849";
3881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        BigInteger aNumber1 = new BigInteger(value1);
3981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        BigInteger aNumber2 = new BigInteger(value2);
4081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        int code1 = aNumber1.hashCode();
4181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        aNumber1.add(aNumber2).shiftLeft(125);
4281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        aNumber1.subtract(aNumber2).shiftRight(125);
4381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        aNumber1.multiply(aNumber2).toByteArray();
4481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        aNumber1.divide(aNumber2).bitLength();
4581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        aNumber1.gcd(aNumber2).pow(7);
4681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        int code2 = aNumber1.hashCode();
4781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        assertTrue("hash codes for the same object differ", code1 == code2);
4881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes    }
4981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes
5081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes    /**
5181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes     * Test hash codes for equal objects.
5281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes     */
5381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes    public void testEqualObjects() {
5481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        String value1 = "12378246728727834290276457386374882976782849";
5581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        String value2 = "12378246728727834290276457386374882976782849";
5681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        BigInteger aNumber1 = new BigInteger(value1);
5781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        BigInteger aNumber2 = new BigInteger(value2);
5881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        int code1 = aNumber1.hashCode();
5981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        int code2 = aNumber2.hashCode();
6081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        if (aNumber1.equals(aNumber2)) {
6181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes            assertTrue("hash codes for equal objects are unequal", code1 == code2);
6281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        }
6381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes    }
6481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes
6581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes    /**
6681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes     * Test hash codes for unequal objects.
6781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes     * The codes are unequal.
6881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes     */
6981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes    public void testUnequalObjectsUnequal() {
7081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        String value1 = "12378246728727834290276457386374882976782849";
7181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        String value2 = "-5634562095872038262928728727834290276457386374882976782849";
7281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        BigInteger aNumber1 = new BigInteger(value1);
7381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        BigInteger aNumber2 = new BigInteger(value2);
7481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        int code1 = aNumber1.hashCode();
7581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        int code2 = aNumber2.hashCode();
7681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        if (!aNumber1.equals(aNumber2)) {
7781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes            assertTrue("hash codes for unequal objects are equal", code1 != code2);
7881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes        }
7981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes    }
8081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes}
81