01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.harmony.tests.java.math;
21:
22: import java.math.BigInteger;
23:
24: import junit.framework.TestCase;
25:
26: /**
27: * Class: java.math.BigInteger
28: * Method: hashCode()
29: */
30: public class BigIntegerHashCodeTest extends TestCase {
31: /**
32: * Test hash codes for the same object
33: */
34: public void testSameObject() {
35: String value1 = "12378246728727834290276457386374882976782849";
36: String value2 = "-5634562095872038262928728727834290276457386374882976782849";
37: BigInteger aNumber1 = new BigInteger(value1);
38: BigInteger aNumber2 = new BigInteger(value2);
39: int code1 = aNumber1.hashCode();
40: aNumber1.add(aNumber2).shiftLeft(125);
41: aNumber1.subtract(aNumber2).shiftRight(125);
42: aNumber1.multiply(aNumber2).toByteArray();
43: aNumber1.divide(aNumber2).bitLength();
44: aNumber1.gcd(aNumber2).pow(7);
45: int code2 = aNumber1.hashCode();
46: assertTrue("hash codes for the same object differ",
47: 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",
62: code1 == code2);
63: }
64: }
65:
66: /**
67: * Test hash codes for unequal objects.
68: * The codes are unequal.
69: */
70: public void testUnequalObjectsUnequal() {
71: String value1 = "12378246728727834290276457386374882976782849";
72: String value2 = "-5634562095872038262928728727834290276457386374882976782849";
73: BigInteger aNumber1 = new BigInteger(value1);
74: BigInteger aNumber2 = new BigInteger(value2);
75: int code1 = aNumber1.hashCode();
76: int code2 = aNumber2.hashCode();
77: if (!aNumber1.equals(aNumber2)) {
78: assertTrue("hash codes for unequal objects are equal",
79: code1 != code2);
80: }
81: }
82: }
|