|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * @test |
| 26 | + * @summary Tests that equals(Object) is consistent with hashCode(), |
| 27 | + * in particular regarding negative versus positive zeros and |
| 28 | + * NaN values. |
| 29 | + */ |
| 30 | + |
| 31 | +import java.awt.geom.AffineTransform; |
| 32 | + |
| 33 | +public class EqualsAndHashCode { |
| 34 | + private static boolean failed; |
| 35 | + |
| 36 | + public static void main(String arg[]) { |
| 37 | + checkReflexiveEquals(); |
| 38 | + checkZeros(); |
| 39 | + checkNotEqual(); |
| 40 | + if (failed) { |
| 41 | + throw new RuntimeException("Some tests failed."); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static void checkReflexiveEquals() { |
| 46 | + AffineTransform t = new AffineTransform(1, 0, 0, 1, Double.NaN, 0); |
| 47 | + if (!t.equals(t)) { |
| 48 | + System.err.println("Transform should be equal to itself."); |
| 49 | + failed = true; |
| 50 | + } |
| 51 | + if (!t.equals(t.clone())) { |
| 52 | + System.err.println("Transform should be equal to its clone."); |
| 53 | + failed = true; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static void checkZeros() { |
| 58 | + AffineTransform positive = new AffineTransform(2, 0, 0, 3, 0, +0.0); |
| 59 | + AffineTransform negative = new AffineTransform(2, 0, 0, 3, 0, -0.0); |
| 60 | + if (!positive.equals(negative)) { |
| 61 | + System.err.println("Transforms should be equal despite the sign difference in zero values."); |
| 62 | + failed = true; |
| 63 | + } else if (positive.hashCode() != negative.hashCode()) { |
| 64 | + System.err.println("Equal transforms should have the same hash code value."); |
| 65 | + failed = true; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void checkNotEqual() { |
| 70 | + AffineTransform t1 = new AffineTransform(2, 0, 0, 3, 2, 0); |
| 71 | + AffineTransform t2 = new AffineTransform(2, 0, 0, 3, 2, 4); |
| 72 | + if (t1.equals(t2)) { |
| 73 | + System.err.println("Expected non-equal transforms."); |
| 74 | + failed = true; |
| 75 | + } |
| 76 | + if (t1.hashCode() == t2.hashCode()) { |
| 77 | + System.err.println("Expected different hash codes."); |
| 78 | + failed = true; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments