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 * @version $Revision$ 20 */ 21 22package org.apache.harmony.math.tests.java.math; 23 24import dalvik.annotation.TestTargetClass; 25import dalvik.annotation.TestTargets; 26import dalvik.annotation.TestLevel; 27import dalvik.annotation.TestTargetNew; 28 29import java.math.BigInteger; 30 31import junit.framework.TestCase; 32@TestTargetClass(BigInteger.class) 33/** 34 * Class: java.math.BigInteger 35 * Method: hashCode() 36 */ 37public class BigIntegerHashCodeTest extends TestCase { 38 /** 39 * Test hash codes for the same object 40 */ 41 @TestTargetNew( 42 level = TestLevel.PARTIAL_COMPLETE, 43 notes = "This is a complete subset of tests for hashCode method.", 44 method = "hashCode", 45 args = {} 46 ) 47 public void testSameObject() { 48 String value1 = "12378246728727834290276457386374882976782849"; 49 String value2 = "-5634562095872038262928728727834290276457386374882976782849"; 50 BigInteger aNumber1 = new BigInteger(value1); 51 BigInteger aNumber2 = new BigInteger(value2); 52 int code1 = aNumber1.hashCode(); 53 aNumber1.add(aNumber2).shiftLeft(125); 54 aNumber1.subtract(aNumber2).shiftRight(125); 55 aNumber1.multiply(aNumber2).toByteArray(); 56 aNumber1.divide(aNumber2).bitLength(); 57 aNumber1.gcd(aNumber2).pow(7); 58 int code2 = aNumber1.hashCode(); 59 assertTrue("hash codes for the same object differ", code1 == code2); 60 } 61 62 /** 63 * Test hash codes for equal objects. 64 */ 65 @TestTargetNew( 66 level = TestLevel.PARTIAL_COMPLETE, 67 notes = "This is a complete subset of tests for hashCode method.", 68 method = "hashCode", 69 args = {} 70 ) 71 public void testEqualObjects() { 72 String value1 = "12378246728727834290276457386374882976782849"; 73 String value2 = "12378246728727834290276457386374882976782849"; 74 BigInteger aNumber1 = new BigInteger(value1); 75 BigInteger aNumber2 = new BigInteger(value2); 76 int code1 = aNumber1.hashCode(); 77 int code2 = aNumber2.hashCode(); 78 if (aNumber1.equals(aNumber2)) { 79 assertTrue("hash codes for equal objects are unequal", code1 == code2); 80 } 81 } 82 83 /** 84 * Test hash codes for unequal objects. 85 * The codes are unequal. 86 */ 87 @TestTargetNew( 88 level = TestLevel.PARTIAL_COMPLETE, 89 notes = "This is a complete subset of tests for hashCode method.", 90 method = "hashCode", 91 args = {} 92 ) 93 public void testUnequalObjectsUnequal() { 94 String value1 = "12378246728727834290276457386374882976782849"; 95 String value2 = "-5634562095872038262928728727834290276457386374882976782849"; 96 BigInteger aNumber1 = new BigInteger(value1); 97 BigInteger aNumber2 = new BigInteger(value2); 98 int code1 = aNumber1.hashCode(); 99 int code2 = aNumber2.hashCode(); 100 if (!aNumber1.equals(aNumber2)) { 101 assertTrue("hash codes for unequal objects are equal", code1 != code2); 102 } 103 } 104} 105