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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
|
/*
* Copyright (c) 2006, 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 4172661 8176501
* @summary Tests all public methods of Path2D classes on all 3 variants
* Path2D.Float, Path2D.Double, and GeneralPath.
* REMIND: Note that the hit testing tests will fail
* occasionally due to precision bugs in the various hit
* testing methods in the geometry classes.
* (Failure rates vary from 1 per 100 runs to 1 per thousands).
* See bug 6396047 to track progress on these failures.
*/
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.FlatteningPathIterator;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Random;
import java.util.NoSuchElementException;
public class UnitTest {
public static boolean verbose;
public static final int WIND_NON_ZERO = PathIterator.WIND_NON_ZERO;
public static final int WIND_EVEN_ODD = PathIterator.WIND_EVEN_ODD;
public static int CoordsForType[] = { 2, 2, 4, 6, 0 };
public static AffineTransform TxIdentity = new AffineTransform();
public static AffineTransform TxComplex = makeAT();
public static Shape TestShapes[];
public static SampleShape ShortSampleNonZero;
public static SampleShape ShortSampleEvenOdd;
public static SampleShape LongSampleNonZero;
public static SampleShape LongSampleEvenOdd;
public static Shape EmptyShapeNonZero =
new EmptyShape(WIND_NON_ZERO);
public static Shape EmptyShapeEvenOdd =
new EmptyShape(WIND_EVEN_ODD);
// Note: We pick a shape that is not anywhere near any of
// our test shapes so that the Path2D does not try to collapse
// out the connecting segment - an optimization that is too
// difficult to account for in the AppendedShape code.
public static Shape AppendShape = new Arc2D.Double(1000, 1000, 40, 40,
Math.PI/4, Math.PI,
Arc2D.CHORD);
public static AffineTransform makeAT() {
AffineTransform at = new AffineTransform();
at.scale(0.66, 0.23);
at.rotate(Math.toRadians(35.0));
at.shear(0.78, 1.32);
return at;
}
Random random;
public UnitTest(long randomSeed) {
this.random = new Random(randomSeed);
TestShapes = new Shape[] {
EmptyShapeNonZero,
EmptyShapeEvenOdd,
new Line2D.Double(),
new Line2D.Double(rpc(), rpc(), rpc(), rpc()),
new Line2D.Double(rnc(), rnc(), rnc(), rnc()),
new Rectangle2D.Double(),
new Rectangle2D.Double(rpc(), rpc(), -1, -1),
new Rectangle2D.Double(rpc(), rpc(), rd(), rd()),
new Rectangle2D.Double(rnc(), rnc(), rd(), rd()),
new Ellipse2D.Double(),
new Ellipse2D.Double(rpc(), rpc(), -1, -1),
new Ellipse2D.Double(rpc(), rpc(), rd(), rd()),
new Ellipse2D.Double(rnc(), rnc(), rd(), rd()),
new Arc2D.Double(Arc2D.OPEN),
new Arc2D.Double(Arc2D.CHORD),
new Arc2D.Double(Arc2D.PIE),
new Arc2D.Double(rpc(), rpc(), -1, -1, rt(), rt(), Arc2D.OPEN),
new Arc2D.Double(rpc(), rpc(), -1, -1, rt(), rt(), Arc2D.CHORD),
new Arc2D.Double(rpc(), rpc(), -1, -1, rt(), rt(), Arc2D.PIE),
new Arc2D.Double(rpc(), rpc(), rd(), rd(), rt(), rt(), Arc2D.OPEN),
new Arc2D.Double(rpc(), rpc(), rd(), rd(), rt(), rt(), Arc2D.CHORD),
new Arc2D.Double(rpc(), rpc(), rd(), rd(), rt(), rt(), Arc2D.PIE),
new Arc2D.Double(rnc(), rnc(), rd(), rd(), rt(), rt(), Arc2D.OPEN),
new Arc2D.Double(rnc(), rnc(), rd(), rd(), rt(), rt(), Arc2D.CHORD),
new Arc2D.Double(rnc(), rnc(), rd(), rd(), rt(), rt(), Arc2D.PIE),
new RoundRectangle2D.Double(),
new RoundRectangle2D.Double(rpc(), rpc(), -1, -1, ra(), ra()),
new RoundRectangle2D.Double(rpc(), rpc(), rd(), rd(), ra(), ra()),
new RoundRectangle2D.Double(rnc(), rnc(), rd(), rd(), ra(), ra()),
new QuadCurve2D.Double(),
new QuadCurve2D.Double(rpc(), rpc(), rpc(), rpc(), rpc(), rpc()),
new QuadCurve2D.Double(rnc(), rnc(), rnc(), rnc(), rnc(), rnc()),
new CubicCurve2D.Double(),
new CubicCurve2D.Double(rpc(), rpc(), rpc(), rpc(),
rpc(), rpc(), rpc(), rpc()),
new CubicCurve2D.Double(rnc(), rnc(), rnc(), rnc(),
rnc(), rnc(), rnc(), rnc()),
makeGeneralPath(WIND_NON_ZERO, 1.0),
makeGeneralPath(WIND_EVEN_ODD, 1.0),
makeGeneralPath(WIND_NON_ZERO, -1.0),
makeGeneralPath(WIND_EVEN_ODD, -1.0),
makeJDK8176501(),
// this shape has a special property: some coefficients to the t^3 term
// are *nearly* zero. And analytically they should be zero, but machine
// error prevented it. In these cases cubic polynomials should degenerate
// into quadratic polynomials, but because the coefficient is not exactly
// zero that may not always be handled correctly:
AffineTransform.getRotateInstance(Math.PI / 4).createTransformedShape(
new Ellipse2D.Float(0, 0, 100, 100))
};
int types[] = new int[100];
int i = 0;
types[i++] = PathIterator.SEG_MOVETO;
types[i++] = PathIterator.SEG_LINETO;
types[i++] = PathIterator.SEG_QUADTO;
types[i++] = PathIterator.SEG_CUBICTO;
types[i++] = PathIterator.SEG_CLOSE;
int shortlen = i;
int prevt = types[i-1];
while (i < types.length) {
int t;
do {
t = (int) (random.nextDouble() * 5);
} while (t == prevt &&
(t == PathIterator.SEG_MOVETO ||
t == PathIterator.SEG_CLOSE));
types[i++] = t;
prevt = t;
}
int numcoords = 0;
int numshortcoords = 0;
for (i = 0; i < types.length; i++) {
if (i == shortlen) {
numshortcoords = numcoords;
}
numcoords += CoordsForType[types[i]];
}
double coords[] = new double[numcoords];
for (i = 0; i < coords.length; i++) {
coords[i] = rpc();
}
ShortSampleNonZero = new SampleShape(WIND_NON_ZERO,
types, coords,
shortlen, numshortcoords);
ShortSampleEvenOdd = new SampleShape(WIND_EVEN_ODD,
types, coords,
shortlen, numshortcoords);
LongSampleNonZero = new SampleShape(WIND_NON_ZERO,
types, coords,
types.length, numcoords);
LongSampleEvenOdd = new SampleShape(WIND_EVEN_ODD,
types, coords,
types.length, numcoords);
}
public GeneralPath makeGeneralPath(int windingrule, double sign) {
GeneralPath gp = new GeneralPath(windingrule);
gp.moveTo((float) (sign * rpc()), (float) (sign * rpc()));
gp.lineTo((float) (sign * rpc()), (float) (sign * rpc()));
gp.quadTo((float) (sign * rpc()), (float) (sign * rpc()),
(float) (sign * rpc()), (float) (sign * rpc()));
gp.curveTo((float) (sign * rpc()), (float) (sign * rpc()),
(float) (sign * rpc()), (float) (sign * rpc()),
(float) (sign * rpc()), (float) (sign * rpc()));
gp.closePath();
return gp;
}
/**
* JDK-8176501 focused on a shape whose bounds included a lot of dead space.
* This recreates that shape, and the unit test testGetBounds2D checks the
* accuracy of {@link Shape#getBounds2D()}
*/
public static Path2D makeJDK8176501() {
Path2D.Double path = new Path2D.Double();
path.moveTo(40, 140);
path.curveTo(40, 60, 160, 60, 160, 140);
path.curveTo(160, 220, 40, 220, 40, 140);
path.closePath();
return path;
}
// Due to odd issues with the sizes of errors when the values
// being manipulated are near zero, we try to avoid values
// near zero by ensuring that both the rpc (positive coords)
// stay away from zero and also by ensuring that the rpc+rd
// (positive coords + dimensions) stay away from zero. We
// also ensure that rnc+rd (negative coords + dimension) stay
// suitably negative without approaching zero.
// Random positive coordinate (10 -> 110)
// rpc + rd gives a total range of (30 -> 170)
public double rpc() {
return (random.nextDouble() * 100.0) + 10.0;
}
// Random negative coordinate (-200 -> -100)
// rnc + rd gives a total range of (-180 -> -40)
public double rnc() {
return (random.nextDouble() * 100.0) - 200.0;
}
// Random dimension (20 -> 60)
public double rd() {
return (random.nextDouble() * 40.0) + 20.0;
}
// Random arc width/height (0.1 -> 5.1)
public double ra() {
return (random.nextDouble() * 5.0) + 0.1;
}
// Random arc angle (theta) (PI/4 => 5PI/4)
public double rt() {
return (random.nextDouble() * Math.PI) + Math.PI/4;
}
public static int fltulpdiff(double v1, double v2) {
if (v1 == v2) {
return 0;
}
float vf1 = (float) v1;
float vf2 = (float) v2;
if (vf1 == vf2) {
return 0;
}
float diff = Math.abs(vf1-vf2);
//float ulp = Math.ulp((float) ((vf1 + vf2)/2f));
float ulp = Math.max(Math.ulp(vf1), Math.ulp(vf2));
if (verbose && diff > ulp) {
System.out.println("v1 = "+vf1+", ulp = "+Math.ulp(vf1));
System.out.println("v2 = "+vf2+", ulp = "+Math.ulp(vf2));
System.out.println((diff/ulp)+" ulps");
}
return (int) (diff/ulp);
}
public static int fltulpless(double v1, double v2) {
if (v1 >= v2) {
return 0;
}
float vf1 = (float) v1;
float vf2 = (float) v2;
if (vf1 >= vf2) {
return 0;
}
float diff = Math.abs(vf1-vf2);
//float ulp = Math.ulp((float) ((vf1 + vf2)/2f));
float ulp = Math.max(Math.ulp(vf1), Math.ulp(vf2));
if (verbose && diff > ulp) {
System.out.println("v1 = "+vf1+", ulp = "+Math.ulp(vf1));
System.out.println("v2 = "+vf2+", ulp = "+Math.ulp(vf2));
System.out.println((diff/ulp)+" ulps");
}
return (int) (diff/ulp);
}
public static int dblulpdiff(double v1, double v2) {
if (v1 == v2) {
return 0;
}
double diff = Math.abs(v1-v2);
//double ulp = Math.ulp((v1 + v2)/2.0);
double ulp = Math.max(Math.ulp(v1), Math.ulp(v2));
if (verbose && diff > ulp) {
System.out.println("v1 = "+v1+", ulp = "+Math.ulp(v1));
System.out.println("v2 = "+v2+", ulp = "+Math.ulp(v2));
System.out.println((diff/ulp)+" ulps");
}
return (int) (diff/ulp);
}
public static abstract class Creator {
public abstract Path2D makePath();
public abstract Path2D makePath(int windingrule);
public abstract Path2D makePath(int windingrule, int capacity);
public abstract Path2D makePath(Shape s);
public abstract Path2D makePath(Shape s, AffineTransform at);
public abstract boolean supportsFloatCompose();
public abstract int getRecommendedTxMaxUlp();
public abstract void compare(PathIterator testpi,
PathIterator refpi,
AffineTransform at,
int maxulp);
}
public static class FltCreator extends Creator {
public Path2D makePath() {
return new Path2D.Float();
}
public Path2D makePath(int windingrule) {
return new Path2D.Float(windingrule);
}
public Path2D makePath(int windingrule, int capacity) {
return new Path2D.Float(windingrule, capacity);
}
public Path2D makePath(Shape s) {
return new Path2D.Float(s);
}
public Path2D makePath(Shape s, AffineTransform at) {
return new Path2D.Float(s, at);
}
public boolean supportsFloatCompose() {
return true;
}
public int getRecommendedTxMaxUlp() {
return 5;
}
public void compare(PathIterator testpi,
PathIterator refpi,
AffineTransform at,
int maxulp)
{
if (testpi.getWindingRule() != refpi.getWindingRule()) {
throw new RuntimeException("wrong winding rule");
}
float testcoords[] = new float[6];
float refcoords[] = new float[6];
while (!testpi.isDone()) {
if (refpi.isDone()) {
throw new RuntimeException("too many segments");
}
int testtype = testpi.currentSegment(testcoords);
int reftype = refpi.currentSegment(refcoords);
if (testtype != reftype) {
throw new RuntimeException("different segment types");
}
if (at != null) {
at.transform(refcoords, 0, refcoords, 0,
CoordsForType[reftype]/2);
}
for (int i = 0; i < CoordsForType[testtype]; i++) {
int ulps = fltulpdiff(testcoords[i], refcoords[i]);
if (ulps > maxulp) {
throw new RuntimeException("coords are different: "+
testcoords[i]+" != "+
refcoords[i]+
" ("+ulps+" ulps)");
}
}
testpi.next();
refpi.next();
}
if (!refpi.isDone()) {
throw new RuntimeException("not enough segments");
}
}
}
public static class DblCreator extends Creator {
public Path2D makePath() {
return new Path2D.Double();
}
public Path2D makePath(int windingrule) {
return new Path2D.Double(windingrule);
}
public Path2D makePath(int windingrule, int capacity) {
return new Path2D.Double(windingrule, capacity);
}
public Path2D makePath(Shape s) {
return new Path2D.Double(s);
}
public Path2D makePath(Shape s, AffineTransform at) {
return new Path2D.Double(s, at);
}
public boolean supportsFloatCompose() {
return false;
}
public int getRecommendedTxMaxUlp() {
return 3;
}
public void compare(PathIterator testpi,
PathIterator refpi,
AffineTransform at,
int maxulp)
{
if (testpi.getWindingRule() != refpi.getWindingRule()) {
throw new RuntimeException("wrong winding rule");
}
double testcoords[] = new double[6];
double refcoords[] = new double[6];
while (!testpi.isDone()) {
if (refpi.isDone()) {
throw new RuntimeException("too many segments");
}
int testtype = testpi.currentSegment(testcoords);
int reftype = refpi.currentSegment(refcoords);
if (testtype != reftype) {
throw new RuntimeException("different segment types");
}
if (at != null) {
at.transform(refcoords, 0, refcoords, 0,
CoordsForType[reftype]/2);
}
for (int i = 0; i < CoordsForType[testtype]; i++) {
int ulps = dblulpdiff(testcoords[i], refcoords[i]);
if (ulps > maxulp) {
throw new RuntimeException("coords are different: "+
testcoords[i]+" != "+
refcoords[i]+
" ("+ulps+" ulps)");
}
}
testpi.next();
refpi.next();
}
if (!refpi.isDone()) {
throw new RuntimeException("not enough segments");
}
}
}
public static class GPCreator extends FltCreator {
public Path2D makePath() {
return new GeneralPath();
}
public Path2D makePath(int windingrule) {
return new GeneralPath(windingrule);
}
public Path2D makePath(int windingrule, int capacity) {
return new GeneralPath(windingrule, capacity);
}
public Path2D makePath(Shape s) {
return new GeneralPath(s);
}
public Path2D makePath(Shape s, AffineTransform at) {
GeneralPath gp = new GeneralPath();
PathIterator pi = s.getPathIterator(at);
gp.setWindingRule(pi.getWindingRule());
gp.append(pi, false);
return gp;
}
public boolean supportsFloatCompose() {
return true;
}
}
public static class EmptyShape implements Shape {
private int windingrule;
public EmptyShape(int windingrule) {
this.windingrule = windingrule;
}
public Rectangle getBounds() {
return new Rectangle();
}
public Rectangle2D getBounds2D() {
return new Rectangle();
}
public boolean contains(double x, double y) {
return false;
}
public boolean contains(Point2D p) {
return false;
}
public boolean intersects(double x, double y, double w, double h) {
return false;
}
public boolean intersects(Rectangle2D r) {
return false;
}
public boolean contains(double x, double y, double w, double h) {
return false;
}
public boolean contains(Rectangle2D r) {
return false;
}
public PathIterator getPathIterator(AffineTransform at) {
return new PathIterator() {
public int getWindingRule() {
return windingrule;
}
public boolean isDone() {
return true;
}
public void next() {}
public int currentSegment(float[] coords) {
throw new NoSuchElementException();
}
public int currentSegment(double[] coords) {
throw new NoSuchElementException();
}
};
}
public PathIterator getPathIterator(AffineTransform at,
double flatness)
{
return getPathIterator(at);
}
}
public static class SampleShape implements Shape {
int windingrule;
int theTypes[];
double theCoords[];
int numTypes;
int numCoords;
public SampleShape(int windingrule,
int types[], double coords[],
int numtypes, int numcoords)
{
this.windingrule = windingrule;
this.theTypes = types;
this.theCoords = coords;
this.numTypes = numtypes;
this.numCoords = numcoords;
}
private Shape testshape;
public Shape getTestShape() {
if (testshape == null) {
testshape = new Area(this);
}
return testshape;
}
public Rectangle getBounds() {
return getBounds2D().getBounds();
}
public Rectangle2D getBounds2D() {
return getTestShape().getBounds2D();
}
public boolean contains(double x, double y) {
return getTestShape().contains(x, y);
}
public boolean contains(Point2D p) {
return getTestShape().contains(p);
}
public boolean intersects(double x, double y, double w, double h) {
return getTestShape().intersects(x, y, w, h);
}
public boolean intersects(Rectangle2D r) {
return getTestShape().intersects(r);
}
public boolean contains(double x, double y, double w, double h) {
return getTestShape().contains(x, y, w, h);
}
public boolean contains(Rectangle2D r) {
return getTestShape().contains(r);
}
public PathIterator getPathIterator(final AffineTransform at) {
return new PathIterator() {
int tindex;
int cindex;
public int getWindingRule() {
return windingrule;
}
public boolean isDone() {
return (tindex >= numTypes);
}
public void next() {
cindex += CoordsForType[theTypes[tindex]];
tindex++;
}
public int currentSegment(float[] coords) {
int t = theTypes[tindex];
int n = CoordsForType[t];
if (n > 0) {
// Cast to float first, then transform
// to match accuracy of float paths
for (int i = 0; i < n; i++) {
coords[i] = (float) theCoords[cindex+i];
}
if (at != null) {
at.transform(coords, 0, coords, 0, n/2);
}
}
return t;
}
public int currentSegment(double[] coords) {
int t = theTypes[tindex];
int n = CoordsForType[t];
if (n > 0) {
if (at == null) {
System.arraycopy(theCoords, cindex,
coords, 0, n);
} else {
at.transform(theCoords, cindex,
coords, 0, n/2);
}
}
return t;
}
};
}
public PathIterator getPathIterator(AffineTransform at,
double flatness)
{
return new FlatteningPathIterator(getPathIterator(at), flatness);
}
public String toString() {
Rectangle2D r2d = getBounds2D();
double xmin = r2d.getMinX();
double ymin = r2d.getMinY();
double xmax = r2d.getMaxX();
double ymax = r2d.getMaxY();
return ("SampleShape["+
(windingrule == WIND_NON_ZERO
? "NonZero"
: "EvenOdd")+
", nsegments = "+numTypes+
", ncoords = "+numCoords+
", bounds["+(r2d.getMinX()+", "+r2d.getMinY()+", "+
r2d.getMaxX()+", "+r2d.getMaxY())+"]"+
"]");
}
public Path2D makeFloatPath(Creator c) {
Path2D.Float p2df = (Path2D.Float) c.makePath(windingrule);
int ci = 0;
for (int i = 0; i < numTypes; i++) {
int t = theTypes[i];
switch (t) {
case PathIterator.SEG_MOVETO:
p2df.moveTo((float) theCoords[ci++],
(float) theCoords[ci++]);
break;
case PathIterator.SEG_LINETO:
p2df.lineTo((float) theCoords[ci++],
(float) theCoords[ci++]);
break;
case PathIterator.SEG_QUADTO:
p2df.quadTo((float) theCoords[ci++],
(float) theCoords[ci++],
(float) theCoords[ci++],
(float) theCoords[ci++]);
break;
case PathIterator.SEG_CUBICTO:
p2df.curveTo((float) theCoords[ci++],
(float) theCoords[ci++],
(float) theCoords[ci++],
(float) theCoords[ci++],
(float) theCoords[ci++],
(float) theCoords[ci++]);
break;
case PathIterator.SEG_CLOSE:
p2df.closePath();
break;
default:
throw new InternalError("unrecognized path type: "+t);
}
if (t != PathIterator.SEG_CLOSE) {
Point2D curpnt = p2df.getCurrentPoint();
if (((float) curpnt.getX()) != ((float) theCoords[ci-2]) ||
((float) curpnt.getY()) != ((float) theCoords[ci-1]))
{
throw new RuntimeException("currentpoint failed");
}
}
}
if (ci != numCoords) {
throw new InternalError("numcoords did not match");
}
return p2df;
}
public Path2D makeDoublePath(Creator c) {
Path2D p2d = c.makePath(windingrule);
int ci = 0;
for (int i = 0; i < numTypes; i++) {
int t = theTypes[i];
switch (t) {
case PathIterator.SEG_MOVETO:
p2d.moveTo(theCoords[ci++], theCoords[ci++]);
break;
case PathIterator.SEG_LINETO:
p2d.lineTo(theCoords[ci++], theCoords[ci++]);
break;
case PathIterator.SEG_QUADTO:
p2d.quadTo(theCoords[ci++], theCoords[ci++],
theCoords[ci++], theCoords[ci++]);
break;
case PathIterator.SEG_CUBICTO:
p2d.curveTo(theCoords[ci++], theCoords[ci++],
theCoords[ci++], theCoords[ci++],
theCoords[ci++], theCoords[ci++]);
break;
case PathIterator.SEG_CLOSE:
p2d.closePath();
break;
default:
throw new InternalError("unrecognized path type: "+t);
}
if (t != PathIterator.SEG_CLOSE) {
Point2D curpnt = p2d.getCurrentPoint();
if (((float) curpnt.getX()) != ((float) theCoords[ci-2]) ||
((float) curpnt.getY()) != ((float) theCoords[ci-1]))
{
throw new RuntimeException("currentpoint failed");
}
}
}
if (ci != numCoords) {
throw new InternalError("numcoords did not match");
}
return p2d;
}
}
public static class AppendedShape implements Shape {
Shape s1;
Shape s2;
boolean connect;
public AppendedShape(Shape s1, Shape s2, boolean connect) {
this.s1 = s1;
this.s2 = s2;
this.connect = connect;
}
public Rectangle getBounds() {
return getBounds2D().getBounds();
}
public Rectangle2D getBounds2D() {
return s1.getBounds2D().createUnion(s2.getBounds2D());
}
private Shape testshape;
private Shape getTestShape() {
if (testshape == null) {
testshape = new GeneralPath(this);
}
return testshape;
}
public boolean contains(double x, double y) {
return getTestShape().contains(x, y);
}
public boolean contains(Point2D p) {
return getTestShape().contains(p);
}
public boolean intersects(double x, double y, double w, double h) {
return getTestShape().intersects(x, y, w, h);
}
public boolean intersects(Rectangle2D r) {
return getTestShape().intersects(r);
}
public boolean contains(double x, double y, double w, double h) {
return getTestShape().contains(x, y, w, h);
}
public boolean contains(Rectangle2D r) {
return getTestShape().contains(r);
}
public PathIterator getPathIterator(final AffineTransform at) {
return new AppendingPathIterator(s1, s2, connect, at);
}
public PathIterator getPathIterator(AffineTransform at,
double flatness)
{
return new FlatteningPathIterator(getPathIterator(at), flatness);
}
public static class AppendingPathIterator implements PathIterator {
AffineTransform at;
PathIterator pi;
Shape swaiting;
int windingrule;
boolean connectrequested;
boolean canconnect;
boolean converttoline;
public AppendingPathIterator(Shape s1, Shape s2,
boolean connect,
AffineTransform at)
{
this.at = at;
this.pi = s1.getPathIterator(at);
this.swaiting = s2;
this.windingrule = pi.getWindingRule();
this.connectrequested = connect;
if (pi.isDone()) {
chain();
}
}
public void chain() {
if (swaiting != null) {
pi = swaiting.getPathIterator(at);
swaiting = null;
converttoline = (connectrequested && canconnect);
}
}
public int getWindingRule() {
return windingrule;
}
public boolean isDone() {
return (pi.isDone());
}
public void next() {
converttoline = false;
pi.next();
if (pi.isDone()) {
chain();
}
canconnect = true;
}
public int currentSegment(float[] coords) {
int type = pi.currentSegment(coords);
if (converttoline) {
type = SEG_LINETO;
}
return type;
}
public int currentSegment(double[] coords) {
int type = pi.currentSegment(coords);
if (converttoline) {
type = SEG_LINETO;
}
return type;
}
}
}
public static void checkEmpty(Path2D p2d, int windingrule) {
checkEmpty2(p2d, windingrule);
p2d.setWindingRule(PathIterator.WIND_NON_ZERO);
checkEmpty2(p2d, PathIterator.WIND_NON_ZERO);
p2d.setWindingRule(PathIterator.WIND_EVEN_ODD);
checkEmpty2(p2d, PathIterator.WIND_EVEN_ODD);
}
public static void checkEmpty2(Path2D p2d, int windingrule) {
if (p2d.getWindingRule() != windingrule) {
throw new RuntimeException("wrong winding rule in Path2D");
}
PathIterator pi = p2d.getPathIterator(null);
if (pi.getWindingRule() != windingrule) {
throw new RuntimeException("wrong winding rule in iterator");
}
if (!pi.isDone()) {
throw new RuntimeException("path not empty");
}
}
public static void compare(Creator c, Path2D p2d, Shape ref, int maxulp) {
compare(c, p2d, (Shape) p2d.clone(), null, 0);
compare(c, p2d, ref, null, 0);
compare(c, p2d, ref, TxIdentity, 0);
p2d.transform(TxIdentity);
compare(c, p2d, ref, null, 0);
compare(c, p2d, ref, TxIdentity, 0);
Shape s2 = p2d.createTransformedShape(TxIdentity);
compare(c, s2, ref, null, 0);
compare(c, s2, ref, TxIdentity, 0);
s2 = p2d.createTransformedShape(TxComplex);
compare(c, s2, ref, TxComplex, maxulp);
p2d.transform(TxComplex);
compare(c, p2d, (Shape) p2d.clone(), null, 0);
compare(c, p2d, ref, TxComplex, maxulp);
}
public static void compare(Creator c,
Shape p2d, Shape s,
AffineTransform at, int maxulp)
{
c.compare(p2d.getPathIterator(null), s.getPathIterator(at),
null, maxulp);
c.compare(p2d.getPathIterator(null), s.getPathIterator(null),
at, maxulp);
}
public static void checkBounds(Shape stest, Shape sref) {
checkBounds(stest.getBounds2D(), sref.getBounds2D(),
"2D bounds too small");
/*
checkBounds(stest.getBounds(), sref.getBounds(),
"int bounds too small");
*/
checkBounds(stest.getBounds(), stest.getBounds2D(),
"int bounds too small for 2D bounds");
}
public static void checkBounds(Rectangle2D tBounds,
Rectangle2D rBounds,
String faildesc)
{
if (rBounds.isEmpty()) {
if (!tBounds.isEmpty()) {
throw new RuntimeException("bounds not empty");
}
return;
} else if (tBounds.isEmpty()) {
throw new RuntimeException("bounds empty");
}
double rxmin = rBounds.getMinX();
double rymin = rBounds.getMinY();
double rxmax = rBounds.getMaxX();
double rymax = rBounds.getMaxY();
double txmin = tBounds.getMinX();
double tymin = tBounds.getMinY();
double txmax = tBounds.getMaxX();
double tymax = tBounds.getMaxY();
if (txmin > rxmin || tymin > rymin ||
txmax < rxmax || tymax < rymax)
{
if (verbose) System.out.println("test bounds = "+tBounds);
if (verbose) System.out.println("ref bounds = "+rBounds);
// Allow fudge room of a couple of single precision ulps
double ltxmin = txmin - 5 * Math.max(Math.ulp((float) rxmin),
Math.ulp((float) txmin));
double ltymin = tymin - 5 * Math.max(Math.ulp((float) rymin),
Math.ulp((float) tymin));
double ltxmax = txmax + 5 * Math.max(Math.ulp((float) rxmax),
Math.ulp((float) txmax));
double ltymax = tymax + 5 * Math.max(Math.ulp((float) rymax),
Math.ulp((float) tymax));
if (ltxmin > rxmin || ltymin > rymin ||
ltxmax < rxmax || ltymax < rymax)
{
if (!verbose) System.out.println("test bounds = "+tBounds);
if (!verbose) System.out.println("ref bounds = "+rBounds);
System.out.println("xmin: "+
txmin+" + "+fltulpless(txmin, rxmin)+" = "+
rxmin+" + "+fltulpless(rxmin, txmin));
System.out.println("ymin: "+
tymin+" + "+fltulpless(tymin, rymin)+" = "+
rymin+" + "+fltulpless(rymin, tymin));
System.out.println("xmax: "+
txmax+" + "+fltulpless(txmax, rxmax)+" = "+
rxmax+" + "+fltulpless(rxmax, txmax));
System.out.println("ymax: "+
tymax+" + "+fltulpless(tymax, rymax)+" = "+
rymax+" + "+fltulpless(rymax, tymax));
System.out.println("flt tbounds = ["+
((float) txmin)+", "+((float) tymin)+", "+
((float) txmax)+", "+((float) tymax)+"]");
System.out.println("flt rbounds = ["+
((float) rxmin)+", "+((float) rymin)+", "+
((float) rxmax)+", "+((float) rymax)+"]");
System.out.println("xmin ulp = "+fltulpless(rxmin, txmin));
System.out.println("ymin ulp = "+fltulpless(rymin, tymin));
System.out.println("xmax ulp = "+fltulpless(txmax, rxmax));
System.out.println("ymax ulp = "+fltulpless(tymax, rymax));
throw new RuntimeException(faildesc);
}
}
}
public void checkHits(Shape stest, Shape sref) {
for (int i = 0; i < 10; i++) {
double px = random.nextDouble() * 500 - 250;
double py = random.nextDouble() * 500 - 250;
Point2D pnt = new Point2D.Double(px, py);
double rw = random.nextDouble()*10+0.4;
double rh = random.nextDouble()*10+0.4;
double rx = px - rw/2;
double ry = py - rh/2;
Rectangle2D rect = new Rectangle2D.Double(rx, ry, rw, rh);
Rectangle2D empty = new Rectangle2D.Double(rx, ry, 0, 0);
if (!rect.contains(pnt)) {
throw new InternalError("test point not inside test rect!");
}
if (stest.contains(rx, ry, 0, 0)) {
throw new RuntimeException("contains 0x0 rect");
}
if (stest.contains(empty)) {
throw new RuntimeException("contains empty rect");
}
if (stest.intersects(rx, ry, 0, 0)) {
throw new RuntimeException("intersects 0x0 rect");
}
if (stest.intersects(empty)) {
throw new RuntimeException("intersects empty rect");
}
boolean tContainsXY = stest.contains(px, py);
boolean tContainsPnt = stest.contains(pnt);
boolean tContainsXYWH = stest.contains(rx, ry, rw, rh);
boolean tContainsRect = stest.contains(rect);
boolean tIntersectsXYWH = stest.intersects(rx, ry, rw, rh);
boolean tIntersectsRect = stest.intersects(rect);
if (tContainsXY != tContainsPnt) {
throw new RuntimeException("contains(x,y) != "+
"contains(pnt)");
}
if (tContainsXYWH != tContainsRect) {
throw new RuntimeException("contains(x,y,w,h) != "+
"contains(rect)");
}
if (tIntersectsXYWH != tIntersectsRect) {
throw new RuntimeException("intersects(x,y,w,h) != "+
"intersects(rect)");
}
boolean uContainsXY =
Path2D.contains(stest.getPathIterator(null), px, py);
boolean uContainsXYWH =
Path2D.contains(stest.getPathIterator(null), rx, ry, rw, rh);
boolean uIntersectsXYWH =
Path2D.intersects(stest.getPathIterator(null), rx, ry, rw, rh);
if (tContainsXY != uContainsXY) {
throw new RuntimeException("contains(x,y) "+
"does not match utility");
}
if (tContainsXYWH != uContainsXYWH) {
throw new RuntimeException("contains(x,y,w,h) "+
"does not match utility");
}
if (tIntersectsXYWH != uIntersectsXYWH) {
throw new RuntimeException("intersects(x,y,w,h) "+
"does not match utility");
}
// Make rect slightly smaller to be more conservative for rContains
double srx = rx + 0.1;
double sry = ry + 0.1;
double srw = rw - 0.2;
double srh = rh - 0.2;
Rectangle2D srect = new Rectangle2D.Double(srx, sry, srw, srh);
// Make rect slightly larger to be more liberal for rIntersects
double lrx = rx - 0.1;
double lry = ry - 0.1;
double lrw = rw + 0.2;
double lrh = rh + 0.2;
Rectangle2D lrect = new Rectangle2D.Double(lrx, lry, lrw, lrh);
if (srect.isEmpty()) {
throw new InternalError("smaller rect too small (empty)");
}
if (!lrect.contains(rect)) {
throw new InternalError("test rect not inside larger rect!");
}
if (!rect.contains(srect)) {
throw new InternalError("smaller rect not inside test rect!");
}
boolean rContainsSmaller;
boolean rIntersectsLarger;
boolean rContainsPnt;
if (sref instanceof SampleShape ||
sref instanceof QuadCurve2D ||
sref instanceof CubicCurve2D)
{
// REMIND
// Some of the source shapes are not proving reliable
// enough to do reference verification of the hit
// testing results.
// Quad/CubicCurve2D have spaghetti test methods that could
// very likely contain some bugs. They return a conflicting
// answer in maybe 1 out of 20,000 tests.
// Area causes a conflicting answer maybe 1 out of
// 100 to 1000 runs and it infinite loops maybe 1
// out of 10,000 runs or so.
// So, we use some conservative "safe" answers for
// these shapes and avoid their hit testing methods.
rContainsSmaller = tContainsRect;
rIntersectsLarger = tIntersectsRect;
rContainsPnt = tContainsPnt;
} else {
rContainsSmaller = sref.contains(srect);
rIntersectsLarger = sref.intersects(lrect);
rContainsPnt = sref.contains(px, py);
}
if (tIntersectsRect) {
if (tContainsRect) {
if (!tContainsPnt) {
System.out.println("reference shape = "+sref);
System.out.println("pnt = "+pnt);
System.out.println("rect = "+rect);
System.out.println("tbounds = "+stest.getBounds2D());
throw new RuntimeException("test contains rect, "+
"but not center point");
}
}
// Note: (tContainsPnt || tContainsRect) is same as
// tContainsPnt because of the test above...
if (tContainsPnt) {
if (!rIntersectsLarger) {
System.out.println("reference shape = "+sref);
System.out.println("pnt = "+pnt);
System.out.println("rect = "+rect);
System.out.println("lrect = "+lrect);
System.out.println("tbounds = "+stest.getBounds2D());
System.out.println("rbounds = "+sref.getBounds2D());
throw new RuntimeException("test claims containment, "+
"but no ref intersection");
}
}
} else {
if (tContainsRect) {
throw new RuntimeException("test contains rect, "+
"with no intersection");
}
if (tContainsPnt) {
System.out.println("reference shape = "+sref);
System.out.println("rect = "+rect);
throw new RuntimeException("test contains point, "+
"with no intersection");
}
if (rContainsPnt || rContainsSmaller) {
System.out.println("pnt = "+pnt);
System.out.println("rect = "+rect);
System.out.println("srect = "+lrect);
throw new RuntimeException("test did not intersect, "+
"but ref claims containment");
}
}
}
}
public void test(Creator c) {
testConstructors(c);
testPathConstruction(c);
testAppend(c);
testBounds(c);
testHits(c);
}
public static void testConstructors(Creator c) {
checkEmpty(c.makePath(), WIND_NON_ZERO);
checkEmpty(c.makePath(WIND_NON_ZERO), WIND_NON_ZERO);
checkEmpty(c.makePath(WIND_EVEN_ODD), WIND_EVEN_ODD);
checkEmpty(c.makePath(EmptyShapeNonZero), WIND_NON_ZERO);
checkEmpty(c.makePath(EmptyShapeNonZero, null), WIND_NON_ZERO);
checkEmpty(c.makePath(EmptyShapeNonZero, TxIdentity), WIND_NON_ZERO);
checkEmpty(c.makePath(EmptyShapeEvenOdd), WIND_EVEN_ODD);
checkEmpty(c.makePath(EmptyShapeEvenOdd, null), WIND_EVEN_ODD);
checkEmpty(c.makePath(EmptyShapeEvenOdd, TxIdentity), WIND_EVEN_ODD);
try {
c.makePath(null);
throw new RuntimeException(c+" allowed null Shape in constructor");
} catch (NullPointerException npe) {
// passes
}
try {
c.makePath(null, TxIdentity);
throw new RuntimeException(c+" allowed null Shape in constructor");
} catch (NullPointerException npe) {
// passes
}
for (int i = 0; i < TestShapes.length; i++) {
Shape sref = TestShapes[i];
if (verbose) System.out.println("construct testing "+sref);
compare(c, c.makePath(sref), sref, null, 0);
compare(c, c.makePath(sref), sref, TxIdentity, 0);
compare(c, c.makePath(sref, null), sref, null, 0);
compare(c, c.makePath(sref, null), sref, TxIdentity, 0);
compare(c, c.makePath(sref, TxIdentity), sref, null, 0);
compare(c, c.makePath(sref, TxIdentity), sref, TxIdentity, 0);
compare(c, c.makePath(sref, TxComplex), sref, TxComplex,
c.getRecommendedTxMaxUlp());
}
}
public static void testPathConstruction(Creator c) {
testPathConstruction(c, LongSampleNonZero);
testPathConstruction(c, LongSampleEvenOdd);
}
public static void testPathConstruction(Creator c, SampleShape ref) {
if (c.supportsFloatCompose()) {
compare(c, ref.makeFloatPath(c), ref, c.getRecommendedTxMaxUlp());
}
compare(c, ref.makeDoublePath(c), ref, c.getRecommendedTxMaxUlp());
}
public static void testAppend(Creator c) {
for (int i = 0; i < TestShapes.length; i++) {
Shape sref = TestShapes[i];
if (verbose) System.out.println("append testing "+sref);
PathIterator spi = sref.getPathIterator(null);
Path2D stest = c.makePath(spi.getWindingRule());
stest.append(spi, false);
compare(c, stest, sref, null, 0);
stest.reset();
stest.append(sref, false);
compare(c, stest, sref, null, 0);
stest.reset();
stest.append(sref.getPathIterator(TxComplex), false);
compare(c, stest, sref, TxComplex, 0);
// multiple shape append testing...
if (sref.getBounds2D().isEmpty()) {
// If the first shape is empty, then we really
// are not testing multiple appended shapes,
// we are just testing appending the AppendShape
// to a null path over and over.
// Also note that some empty shapes will spit out
// a single useless SEG_MOVETO that has no affect
// on the outcome, but it makes duplicating the
// behavior that Path2D has in that case difficult
// when the AppenedShape utility class has to
// iterate the exact same segments. So, we will
// just ignore all empty shapes here.
continue;
}
stest.reset();
stest.append(sref, false);
stest.append(AppendShape, false);
compare(c, stest,
new AppendedShape(sref, AppendShape, false),
null, 0);
stest.reset();
stest.append(sref, false);
stest.append(AppendShape, true);
compare(c, stest,
new AppendedShape(sref, AppendShape, true),
null, 0);
stest.reset();
stest.append(sref.getPathIterator(null), false);
stest.append(AppendShape.getPathIterator(null), false);
compare(c, stest,
new AppendedShape(sref, AppendShape, false),
null, 0);
stest.reset();
stest.append(sref.getPathIterator(null), false);
stest.append(AppendShape.getPathIterator(null), true);
compare(c, stest,
new AppendedShape(sref, AppendShape, true),
null, 0);
stest.reset();
stest.append(sref.getPathIterator(TxComplex), false);
stest.append(AppendShape.getPathIterator(TxComplex), false);
compare(c, stest,
new AppendedShape(sref, AppendShape, false),
TxComplex, 0);
stest.reset();
stest.append(sref.getPathIterator(TxComplex), false);
stest.append(AppendShape.getPathIterator(TxComplex), true);
compare(c, stest,
new AppendedShape(sref, AppendShape, true),
TxComplex, 0);
}
}
public static void testBounds(Creator c) {
for (int i = 0; i < TestShapes.length; i++) {
Shape sref = TestShapes[i];
if (verbose) System.out.println("bounds testing "+sref);
Shape stest = c.makePath(sref);
checkBounds(c.makePath(sref), sref);
testGetBounds2D(stest);
}
testBounds(c, ShortSampleNonZero);
testBounds(c, ShortSampleEvenOdd);
testBounds(c, LongSampleNonZero);
testBounds(c, LongSampleEvenOdd);
}
public static void testBounds(Creator c, SampleShape ref) {
if (verbose) System.out.println("bounds testing "+ref);
if (c.supportsFloatCompose()) {
checkBounds(ref.makeFloatPath(c), ref);
}
checkBounds(ref.makeDoublePath(c), ref);
}
/**
* Make sure the {@link Shape#getBounds2D()} returns a Rectangle2D that tightly fits the
* shape data. It shouldn't contain lots of dead space (see JDK 8176501), and it shouldn't
* leave out any shape path. This test relies on the accuracy of
* {@link Shape#intersects(double, double, double, double)}
*/
public static void testGetBounds2D(Shape shape) {
// first: make sure the shape is actually close to the perimeter of shape.getBounds2D().
// this is the crux of JDK 8176501:
Rectangle2D r = shape.getBounds2D();
if (r.getWidth() == 0 || r.getHeight() == 0) {
// this can happen for completely empty paths, which are part of our
// edge test cases in this class.
return;
}
if (verbose) System.out.println("testGetBounds2D "+shape+", "+r);
double xminInterior = r.getMinX() + .000001;
double yminInterior = r.getMinY() + .000001;
double xmaxInterior = r.getMaxX() - .000001;
double ymaxInterior = r.getMaxY() - .000001;
Rectangle2D topStrip = new Rectangle2D.Double(r.getMinX(), r.getMinY(), r.getWidth(), yminInterior - r.getMinY());
Rectangle2D leftStrip = new Rectangle2D.Double(r.getMinX(), r.getMinY(), xminInterior - r.getMinX(), r.getHeight());
Rectangle2D bottomStrip = new Rectangle2D.Double(r.getMinX(), ymaxInterior, r.getWidth(), r.getMaxY() - ymaxInterior);
Rectangle2D rightStrip = new Rectangle2D.Double(xmaxInterior, r.getMinY(), r.getMaxX() - xmaxInterior, r.getHeight());
if (!shape.intersects(topStrip)) {
if (verbose)
System.out.println("topStrip = "+topStrip);
throw new RuntimeException("the shape must intersect the top strip of its bounds");
}
if (!shape.intersects(leftStrip)) {
if (verbose)
System.out.println("leftStrip = " + leftStrip);
throw new RuntimeException("the shape must intersect the left strip of its bounds");
}
if (!shape.intersects(bottomStrip)) {
if (verbose)
System.out.println("bottomStrip = " + bottomStrip);
throw new RuntimeException("the shape must intersect the bottom strip of its bounds");
}
if (!shape.intersects(rightStrip)) {
if (verbose)
System.out.println("rightStrip = " + rightStrip);
throw new RuntimeException("the shape must intersect the right strip of bounds");
}
// Similarly: make sure our shape doesn't exist OUTSIDE of r, either. To my knowledge this has never
// been a problem, but if it did happen this would be an even more serious breach of contract than
// the former case.
double xminExterior = r.getMinX() - .000001;
double yminExterior = r.getMinY() - .000001;
double xmaxExterior = r.getMaxX() + .000001;
double ymaxExterior = r.getMaxY() + .000001;
// k is simply meant to mean "a large number, functionally similar to infinity for this test"
double k = 10000.0;
leftStrip = new Rectangle2D.Double(xminExterior - k, -k, k, 3 * k);
rightStrip = new Rectangle2D.Double(xmaxExterior, -k, k, 3 * k);
topStrip = new Rectangle2D.Double(-k, yminExterior - k, 3 * k, k);
bottomStrip = new Rectangle2D.Double(-k, ymaxExterior, 3 * k, k);
if (shape.intersects(leftStrip)) {
if (verbose)
System.out.println("leftStrip = " + leftStrip);
throw new RuntimeException("the shape must not intersect anything to the left of its bounds");
}
if (shape.intersects(rightStrip)) {
if (verbose)
System.out.println("rightStrip = " + rightStrip);
throw new RuntimeException("the shape must not intersect anything to the right of its bounds");
}
if (shape.intersects(topStrip)) {
if (verbose)
System.out.println("topStrip = " + topStrip);
throw new RuntimeException("the shape must not intersect anything above its bounds");
}
if (shape.intersects(bottomStrip)) {
if (verbose)
System.out.println("bottomStrip = " + bottomStrip);
throw new RuntimeException("the shape must not intersect anything below its bounds");
}
}
public void testHits(Creator c) {
for (int i = 0; i < TestShapes.length; i++) {
Shape sref = TestShapes[i];
if (verbose) System.out.println("hit testing "+sref);
Shape stest = c.makePath(sref);
checkHits(c.makePath(sref), sref);
}
testHits(c, ShortSampleNonZero);
testHits(c, ShortSampleEvenOdd);
// These take too long to construct the Area for reference testing
//testHits(c, LongSampleNonZero);
//testHits(c, LongSampleEvenOdd);
}
public void testHits(Creator c, SampleShape ref) {
if (verbose) System.out.println("hit testing "+ref);
if (c.supportsFloatCompose()) {
checkHits(ref.makeFloatPath(c), ref);
}
checkHits(ref.makeDoublePath(c), ref);
}
public static void main(String argv[]) {
// as specific failures come up we can add them to this array to make sure they're frequently tested:
long[] previousBugSeeds = new long[] {
// these are all failures related to JDK-8176501
4603421469924484958L,
4596019360892069260L,
4604586530476373958L,
4603766396818608126L
};
verbose = (argv.length > 1);
for (long seed : previousBugSeeds) {
test("", seed);
}
int limit = (argv.length > 0) ? 10000 : 1;
for (int i = 0; i < limit; i++) {
long seed = Double.doubleToLongBits(Math.random());
test("loop #" + (i + 1), seed);
}
}
private static void test(String logPrefix, long seed) {
String msg = "seed = " + seed;
if (!logPrefix.isEmpty())
msg = logPrefix + ", " + msg;
System.out.println(msg);
UnitTest t = new UnitTest(seed);
t.test(new GPCreator());
t.test(new FltCreator());
t.test(new DblCreator());
}
}
|