File: EqualsHashcode.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (475 lines) | stat: -rw-r--r-- 14,316 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 6210287
 * @summary Verifies that the various utility shapes implement
 *          the equals(Object) and hashCode methods adequately
 */

import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Vector;

public class EqualsHashcode {
    public static final int NUMTESTS = 1000;

    public static void main(String argv[]) {
        new FloatRectangleTester().test();
        new DoubleRectangleTester().test();

        new FloatEllipseTester().test();
        new DoubleEllipseTester().test();

        new FloatArcTester().test();
        new DoubleArcTester().test();

        new FloatRoundRectTester().test();
        new DoubleRoundRectTester().test();
    }

    /**
     * Base utility class for random parameters for testing
     */
    public static abstract class Val {
        protected String name;

        protected Val(String name) {
            this.name = name;
        }

        public abstract Object save();
        public abstract void restore(Object save);

        public abstract void randomize();
        public abstract void perturb();
    }

    /**
     * Base subclass for parameters with "special" values (Infinity, NaN, etc.)
     */
    public static abstract class SpecialVal extends Val {
        protected SpecialVal(String name) {
            super(name);
        }

        public abstract void setSpecial();

        public abstract boolean isNaN();
    }

    /**
     * Floating point parameter
     */
    public static class FloatVal extends SpecialVal {
        private float v;

        public FloatVal(String name) {
            super(name);
        }

        public float getVal() {
            return v;
        }

        public String toString() {
            return name+" = "+v+" (flt)";
        }

        public Object save() {
            return new Float(v);
        }

        public void restore(Object o) {
            v = ((Float) o).floatValue();
        }

        public void randomize() {
            v = (float) (Math.random() * 100);
        }

        public void perturb() {
            v = v + 1;
        }

        public boolean hasSpecialCases() {
            return true;
        }

        public void setSpecial() {
            switch ((int) (Math.random() * 3)) {
            case 0:
                v = Float.NaN;
                break;
            case 1:
                v = Float.POSITIVE_INFINITY;
                break;
            case 2:
                v = Float.NEGATIVE_INFINITY;
                break;
            default:
                throw new InternalError();
            }
        }

        public boolean isNaN() {
            return (v != v);
        }
    }

    /**
     * Double precision parameter
     */
    public static class DoubleVal extends SpecialVal {
        private double v;

        public DoubleVal(String name) {
            super(name);
        }

        public double getVal() {
            return v;
        }

        public String toString() {
            return name+" = "+v+" (dbl)";
        }

        public Object save() {
            return new Double(v);
        }

        public void restore(Object o) {
            v = ((Double) o).doubleValue();
        }

        public void randomize() {
            v = Math.random() * 100;
        }

        public void perturb() {
            v = v + 1;
        }

        public boolean hasSpecialCases() {
            return true;
        }

        public void setSpecial() {
            switch ((int) (Math.random() * 3)) {
            case 0:
                v = Double.NaN;
                break;
            case 1:
                v = Double.POSITIVE_INFINITY;
                break;
            case 2:
                v = Double.NEGATIVE_INFINITY;
                break;
            default:
                throw new InternalError();
            }
        }

        public boolean isNaN() {
            return (v != v);
        }
    }

    /**
     * Integer value with a specified min/max range.
     */
    public static class IntRangeVal extends Val {
        public int v;
        public int min;
        public int max;

        public IntRangeVal(String name, int min, int max) {
            super(name);
            this.min = min;
            this.max = max;
        }

        public int getVal() {
            return v;
        }

        public String toString() {
            return name+" = "+v;
        }

        public Object save() {
            return new Integer(v);
        }

        public void restore(Object o) {
            v = ((Integer) o).intValue();
        }

        public void randomize() {
            v = min + (int) (Math.random() * (max-min+1));
        }

        public void perturb() {
            v = v + 1;
            if (v > max) {
                v = min;
            }
        }
    }

    /**
     * Base class for testing a given type of shape.
     * Subclasses must register all of their "parameters" which
     * need to be randomized, specialized, and perturbed for
     * testing.
     * Subclasses must also implement makeShape() which makes
     * a new shape object according to the current values of
     * all of their parameters.
     */
    public static abstract class ShapeTester {
        public Vector params = new Vector();

        public void addParam(Val v) {
            params.add(v);
        }

        public Val[] getParamArray() {
            Val ret[] = new Val[params.size()];
            for (int i = 0; i < params.size(); i++) {
                ret[i] = (Val) params.get(i);
            }
            return ret;
        }

        public void error(String desc) {
            Val params[] = getParamArray();
            for (int j = 0; j < params.length; j++) {
                System.err.println(params[j]);
            }
            throw new RuntimeException(desc);
        }

        public abstract Object makeShape();

        public void test() {
            Val params[] = getParamArray();
            for (int i = 0; i < NUMTESTS; i++) {
                // First, randomize all parameters
                for (int j = 0; j < params.length; j++) {
                    params[j].randomize();
                }

                // Now make 2 copies from the same params and verify equals()
                Object o1 = makeShape();
                if (!o1.equals(o1)) {
                    error("Shapes not equal to itself!");
                }
                Object o2 = makeShape();
                if (!o1.equals(o2)) {
                    error("Identical shapes not equal!");
                }
                if (o1.hashCode() != o2.hashCode()) {
                    error("Identical hashes not equal!");
                }

                // Now perturb the params 1 by 1 and verify !equals()
                for (int j = 0; j < params.length; j++) {
                    Val param = params[j];
                    Object save = param.save();

                    param.perturb();
                    Object o3 = makeShape();
                    if (o1.equals(o3)) {
                        error("Perturbed shape still equal!");
                    }

                    // If param has "special values", test them as well
                    if (param instanceof SpecialVal) {
                        SpecialVal sparam = (SpecialVal) param;
                        sparam.setSpecial();
                        Object o4 = makeShape();
                        if (o1.equals(o4)) {
                            error("Specialized shape still equal!");
                        }
                        Object o5 = makeShape();
                        // objects equal iff param is not a NaN
                        if (o4.equals(o5) == sparam.isNaN()) {
                            error("Identical specialized shapes not equal!");
                        }
                        // hash codes always equal, even if NaN
                        // (Note: equals()/hashCode() contract allows this)
                        if (o4.hashCode() != o5.hashCode()) {
                            error("Identical specialized hashes not equal!");
                        }
                    }

                    // Restore original value of param and make sure
                    param.restore(save);
                    Object o6 = makeShape();
                    if (!o1.equals(o6)) {
                        error("Restored shape not equal!");
                    }
                    if (o1.hashCode() != o6.hashCode()) {
                        error("Restored hash not equal!");
                    }
                }
            }
        }
    }

    /**
     * Base tester class for objects with floating point xywh bounds
     */
    public static abstract class FloatBoundedShape extends ShapeTester {
        public FloatVal x = new FloatVal("x");
        public FloatVal y = new FloatVal("y");
        public FloatVal w = new FloatVal("w");
        public FloatVal h = new FloatVal("h");

        public FloatBoundedShape() {
            addParam(x);
            addParam(y);
            addParam(w);
            addParam(h);
        }
    }

    /**
     * Base tester class for objects with double precision xywh bounds
     */
    public static abstract class DoubleBoundedShape extends ShapeTester {
        public DoubleVal x = new DoubleVal("x");
        public DoubleVal y = new DoubleVal("y");
        public DoubleVal w = new DoubleVal("w");
        public DoubleVal h = new DoubleVal("h");

        public DoubleBoundedShape() {
            addParam(x);
            addParam(y);
            addParam(w);
            addParam(h);
        }
    }

    public static class FloatRectangleTester extends FloatBoundedShape {
        public Object makeShape() {
            return new Rectangle2D.Float(x.getVal(), y.getVal(),
                                         w.getVal(), h.getVal());
        }
    }

    public static class DoubleRectangleTester extends DoubleBoundedShape {
        public Object makeShape() {
            return new Rectangle2D.Double(x.getVal(), y.getVal(),
                                          w.getVal(), h.getVal());
        }
    }

    public static class FloatEllipseTester extends FloatBoundedShape {
        public Object makeShape() {
            return new Ellipse2D.Float(x.getVal(), y.getVal(),
                                       w.getVal(), h.getVal());
        }
    }

    public static class DoubleEllipseTester extends DoubleBoundedShape {
        public Object makeShape() {
            return new Ellipse2D.Double(x.getVal(), y.getVal(),
                                        w.getVal(), h.getVal());
        }
    }

    public static class FloatArcTester extends FloatBoundedShape {
        public FloatVal start = new FloatVal("start");
        public FloatVal extent = new FloatVal("extent");
        public IntRangeVal type = new IntRangeVal("type", 0, 2);

        public FloatArcTester() {
            addParam(start);
            addParam(extent);
            addParam(type);
        }

        public Object makeShape() {
            return new Arc2D.Float(x.getVal(), y.getVal(),
                                   w.getVal(), h.getVal(),
                                   start.getVal(), extent.getVal(),
                                   type.getVal());
        }
    }

    public static class DoubleArcTester extends DoubleBoundedShape {
        public DoubleVal start = new DoubleVal("start");
        public DoubleVal extent = new DoubleVal("extent");
        public IntRangeVal type = new IntRangeVal("type", 0, 2);

        public DoubleArcTester() {
            addParam(start);
            addParam(extent);
            addParam(type);
        }

        public Object makeShape() {
            return new Arc2D.Double(x.getVal(), y.getVal(),
                                    w.getVal(), h.getVal(),
                                    start.getVal(), extent.getVal(),
                                    type.getVal());
        }
    }

    public static class FloatRoundRectTester extends FloatBoundedShape {
        public FloatVal arcw = new FloatVal("arcw");
        public FloatVal arch = new FloatVal("arch");

        public FloatRoundRectTester() {
            addParam(arcw);
            addParam(arch);
        }

        public Object makeShape() {
            return new RoundRectangle2D.Float(x.getVal(), y.getVal(),
                                              w.getVal(), h.getVal(),
                                              arcw.getVal(), arch.getVal());
        }
    }

    public static class DoubleRoundRectTester extends DoubleBoundedShape {
        public DoubleVal arcw = new DoubleVal("arcw");
        public DoubleVal arch = new DoubleVal("arch");

        public DoubleRoundRectTester() {
            addParam(arcw);
            addParam(arch);
        }

        public Object makeShape() {
            return new RoundRectangle2D.Double(x.getVal(), y.getVal(),
                                               w.getVal(), h.getVal(),
                                               arcw.getVal(), arch.getVal());
        }
    }
}