Path: blob/master/test/jdk/sun/java2d/pipe/RegionOps.java
41152 views
/*1* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test %W% %E%25* @bug 650487426* @summary This test verifies the operation (and performance) of the27* various CAG operations on the internal Region class.28* @modules java.desktop/sun.java2d.pipe29* @run main RegionOps30*/3132import java.awt.Rectangle;33import java.awt.geom.Area;34import java.awt.geom.AffineTransform;35import java.awt.image.BufferedImage;36import java.util.Random;37import sun.java2d.pipe.Region;3839public class RegionOps {40public static final int DEFAULT_NUMREGIONS = 50;41public static final int DEFAULT_MINSUBRECTS = 1;42public static final int DEFAULT_MAXSUBRECTS = 10;4344public static final int MINCOORD = -20;45public static final int MAXCOORD = 20;4647public static boolean useArea;4849static int numops;50static int numErrors;51static Random rand = new Random();52static boolean skipCheck;53static boolean countErrors;5455static {56// Instantiating BufferedImage initializes sun.java2d57BufferedImage bimg =58new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);59}6061public static void usage(String error) {62if (error != null) {63System.err.println("Error: "+error);64}65System.err.println("Usage: java RegionOps "+66"[-regions N] [-rects M] "+67"[-[min|max]rects M] [-area]");68System.err.println(" "+69"[-add|union] [-sub|diff] "+70"[-int[ersect]] [-xor]");71System.err.println(" "+72"[-seed S] [-nocheck] [-count[errors]] [-help]");73System.exit((error != null) ? 1 : 0);74}7576public static void error(RectListImpl a, RectListImpl b, String problem) {77System.err.println("Operating on: "+a);78if (b != null) {79System.err.println("and: "+b);80}81if (countErrors) {82System.err.println(problem);83numErrors++;84} else {85throw new RuntimeException(problem);86}87}8889public static void main(String argv[]) {90int numregions = DEFAULT_NUMREGIONS;91int minsubrects = DEFAULT_MINSUBRECTS;92int maxsubrects = DEFAULT_MAXSUBRECTS;93boolean doUnion = false;94boolean doIntersect = false;95boolean doSubtract = false;96boolean doXor = false;9798for (int i = 0; i < argv.length; i++) {99String arg = argv[i];100if (arg.equalsIgnoreCase("-regions")) {101if (i+1 >= argv.length) {102usage("missing arg for -regions");103}104numregions = Integer.parseInt(argv[++i]);105} else if (arg.equalsIgnoreCase("-rects")) {106if (i+1 >= argv.length) {107usage("missing arg for -rects");108}109minsubrects = maxsubrects = Integer.parseInt(argv[++i]);110} else if (arg.equalsIgnoreCase("-minrects")) {111if (i+1 >= argv.length) {112usage("missing arg for -minrects");113}114minsubrects = Integer.parseInt(argv[++i]);115} else if (arg.equalsIgnoreCase("-maxrects")) {116if (i+1 >= argv.length) {117usage("missing arg for -maxrects");118}119maxsubrects = Integer.parseInt(argv[++i]);120} else if (arg.equalsIgnoreCase("-area")) {121useArea = true;122} else if (arg.equalsIgnoreCase("-add") ||123arg.equalsIgnoreCase("-union"))124{125doUnion = true;126} else if (arg.equalsIgnoreCase("-sub") ||127arg.equalsIgnoreCase("-diff"))128{129doSubtract = true;130} else if (arg.equalsIgnoreCase("-int") ||131arg.equalsIgnoreCase("-intersect"))132{133doIntersect = true;134} else if (arg.equalsIgnoreCase("-xor")) {135doXor = true;136} else if (arg.equalsIgnoreCase("-seed")) {137if (i+1 >= argv.length) {138usage("missing arg for -seed");139}140rand.setSeed(Long.decode(argv[++i]).longValue());141} else if (arg.equalsIgnoreCase("-nocheck")) {142skipCheck = true;143} else if (arg.equalsIgnoreCase("-count") ||144arg.equalsIgnoreCase("-counterrors"))145{146countErrors = true;147} else if (arg.equalsIgnoreCase("-help")) {148usage(null);149} else {150usage("Unknown argument: "+arg);151}152}153154if (maxsubrects < minsubrects) {155usage("maximum number of subrectangles less than minimum");156}157158if (minsubrects <= 0) {159usage("minimum number of subrectangles must be positive");160}161162if (!doUnion && !doSubtract && !doIntersect && !doXor) {163doUnion = doSubtract = doIntersect = doXor = true;164}165166long start = System.currentTimeMillis();167RectListImpl rlist[] = new RectListImpl[numregions];168int totalrects = 0;169for (int i = 0; i < rlist.length; i++) {170RectListImpl rli = RectListImpl.getInstance();171int numsubrects =172minsubrects + rand.nextInt(maxsubrects - minsubrects + 1);173for (int j = 0; j < numsubrects; j++) {174addRectTo(rli);175totalrects++;176}177rlist[i] = rli;178}179long end = System.currentTimeMillis();180System.out.println((end-start)+"ms to create "+181rlist.length+" regions containing "+182totalrects+" subrectangles");183184start = System.currentTimeMillis();185for (int i = 0; i < rlist.length; i++) {186RectListImpl a = rlist[i];187testTranslate(a);188for (int j = i; j < rlist.length; j++) {189RectListImpl b = rlist[j];190if (doUnion) testUnion(a, b);191if (doSubtract) testDifference(a, b);192if (doIntersect) testIntersection(a, b);193if (doXor) testExclusiveOr(a, b);194}195}196end = System.currentTimeMillis();197System.out.println(numops+" ops took "+(end-start)+"ms");198199if (numErrors > 0) {200throw new RuntimeException(numErrors+" errors encountered");201}202}203204public static void addRectTo(RectListImpl rli) {205int lox = MINCOORD + rand.nextInt(MAXCOORD - MINCOORD + 1);206int hix = MINCOORD + rand.nextInt(MAXCOORD - MINCOORD + 1);207int loy = MINCOORD + rand.nextInt(MAXCOORD - MINCOORD + 1);208int hiy = MINCOORD + rand.nextInt(MAXCOORD - MINCOORD + 1);209rli.addRect(lox, loy, hix, hiy);210}211212public static void checkEqual(RectListImpl a, RectListImpl b,213String optype)214{215if (a.hashCode() != b.hashCode()) {216error(a, b, "hashcode failed for "+optype);217}218if (!a.equals(b)) {219error(a, b, "equals failed for "+optype);220}221}222223public static void testTranslate(RectListImpl a) {224RectListImpl maxTrans =225a.getTranslation(Integer.MAX_VALUE, Integer.MAX_VALUE)226.getTranslation(Integer.MAX_VALUE, Integer.MAX_VALUE)227.getTranslation(Integer.MAX_VALUE, Integer.MAX_VALUE);228if (!maxTrans.checkTransEmpty()) {229error(maxTrans, null, "overflow translated RectList not empty");230}231RectListImpl minTrans =232a.getTranslation(Integer.MIN_VALUE, Integer.MIN_VALUE)233.getTranslation(Integer.MIN_VALUE, Integer.MIN_VALUE)234.getTranslation(Integer.MIN_VALUE, Integer.MIN_VALUE);235if (!minTrans.checkTransEmpty()) {236error(minTrans, null, "overflow translated RectList not empty");237}238testTranslate(a, Integer.MAX_VALUE, Integer.MAX_VALUE, false,239MINCOORD, 0, MINCOORD, 0);240testTranslate(a, Integer.MAX_VALUE, Integer.MIN_VALUE, false,241MINCOORD, 0, 0, MAXCOORD);242testTranslate(a, Integer.MIN_VALUE, Integer.MAX_VALUE, false,2430, MAXCOORD, MINCOORD, 0);244testTranslate(a, Integer.MIN_VALUE, Integer.MIN_VALUE, false,2450, MAXCOORD, 0, MAXCOORD);246for (int dy = -100; dy <= 100; dy += 50) {247for (int dx = -100; dx <= 100; dx += 50) {248testTranslate(a, dx, dy, true,249MINCOORD, MAXCOORD,250MINCOORD, MAXCOORD);251}252}253}254255public static void testTranslate(RectListImpl a, int dx, int dy,256boolean isNonDestructive,257int xmin, int xmax,258int ymin, int ymax)259{260RectListImpl theTrans = a.getTranslation(dx, dy); numops++;261if (skipCheck) return;262RectListImpl unTrans = theTrans.getTranslation(-dx, -dy);263if (isNonDestructive) checkEqual(a, unTrans, "Translate");264for (int x = xmin; x < xmax; x++) {265for (int y = ymin; y < ymax; y++) {266boolean inside = a.contains(x, y);267if (theTrans.contains(x+dx, y+dy) != inside) {268error(a, null, "translation failed for "+269dx+", "+dy+" at "+x+", "+y);270}271}272}273}274275public static void testUnion(RectListImpl a, RectListImpl b) {276RectListImpl aUb = a.getUnion(b); numops++;277RectListImpl bUa = b.getUnion(a); numops++;278if (skipCheck) return;279checkEqual(aUb, bUa, "Union");280testUnion(a, b, aUb);281testUnion(a, b, bUa);282}283284public static void testUnion(RectListImpl a, RectListImpl b,285RectListImpl theUnion)286{287for (int x = MINCOORD; x < MAXCOORD; x++) {288for (int y = MINCOORD; y < MAXCOORD; y++) {289boolean inside = (a.contains(x, y) || b.contains(x, y));290if (theUnion.contains(x, y) != inside) {291error(a, b, "union failed at "+x+", "+y);292}293}294}295}296297public static void testDifference(RectListImpl a, RectListImpl b) {298RectListImpl aDb = a.getDifference(b); numops++;299RectListImpl bDa = b.getDifference(a); numops++;300if (skipCheck) return;301// Note that difference is not commutative so we cannot check equals302// checkEqual(a, b, "Difference");303testDifference(a, b, aDb);304testDifference(b, a, bDa);305}306307public static void testDifference(RectListImpl a, RectListImpl b,308RectListImpl theDifference)309{310for (int x = MINCOORD; x < MAXCOORD; x++) {311for (int y = MINCOORD; y < MAXCOORD; y++) {312boolean inside = (a.contains(x, y) && !b.contains(x, y));313if (theDifference.contains(x, y) != inside) {314error(a, b, "difference failed at "+x+", "+y);315}316}317}318}319320public static void testIntersection(RectListImpl a, RectListImpl b) {321RectListImpl aIb = a.getIntersection(b); numops++;322RectListImpl bIa = b.getIntersection(a); numops++;323if (skipCheck) return;324checkEqual(aIb, bIa, "Intersection");325testIntersection(a, b, aIb);326testIntersection(a, b, bIa);327}328329public static void testIntersection(RectListImpl a, RectListImpl b,330RectListImpl theIntersection)331{332for (int x = MINCOORD; x < MAXCOORD; x++) {333for (int y = MINCOORD; y < MAXCOORD; y++) {334boolean inside = (a.contains(x, y) && b.contains(x, y));335if (theIntersection.contains(x, y) != inside) {336error(a, b, "intersection failed at "+x+", "+y);337}338}339}340}341342public static void testExclusiveOr(RectListImpl a, RectListImpl b) {343RectListImpl aXb = a.getExclusiveOr(b); numops++;344RectListImpl bXa = b.getExclusiveOr(a); numops++;345if (skipCheck) return;346checkEqual(aXb, bXa, "ExclusiveOr");347testExclusiveOr(a, b, aXb);348testExclusiveOr(a, b, bXa);349}350351public static void testExclusiveOr(RectListImpl a, RectListImpl b,352RectListImpl theExclusiveOr)353{354for (int x = MINCOORD; x < MAXCOORD; x++) {355for (int y = MINCOORD; y < MAXCOORD; y++) {356boolean inside = (a.contains(x, y) != b.contains(x, y));357if (theExclusiveOr.contains(x, y) != inside) {358error(a, b, "xor failed at "+x+", "+y);359}360}361}362}363364public abstract static class RectListImpl {365public static RectListImpl getInstance() {366if (useArea) {367return new AreaImpl();368} else {369return new RegionImpl();370}371}372373public abstract void addRect(int lox, int loy, int hix, int hiy);374375public abstract RectListImpl getTranslation(int dx, int dy);376377public abstract RectListImpl getIntersection(RectListImpl rli);378public abstract RectListImpl getExclusiveOr(RectListImpl rli);379public abstract RectListImpl getDifference(RectListImpl rli);380public abstract RectListImpl getUnion(RectListImpl rli);381382// Used for making sure that 3xMAX translates yields an empty region383public abstract boolean checkTransEmpty();384385public abstract boolean contains(int x, int y);386387public abstract int hashCode();388public abstract boolean equals(RectListImpl other);389}390391public static class AreaImpl extends RectListImpl {392Area theArea;393394public AreaImpl() {395}396397public AreaImpl(Area a) {398theArea = a;399}400401public void addRect(int lox, int loy, int hix, int hiy) {402Area a2 = new Area(new Rectangle(lox, loy, hix-lox, hiy-loy));403if (theArea == null) {404theArea = a2;405} else {406theArea.add(a2);407}408}409410public RectListImpl getTranslation(int dx, int dy) {411AffineTransform at = AffineTransform.getTranslateInstance(dx, dy);412return new AreaImpl(theArea.createTransformedArea(at));413}414415public RectListImpl getIntersection(RectListImpl rli) {416Area a2 = new Area(theArea);417a2.intersect(((AreaImpl) rli).theArea);418return new AreaImpl(a2);419}420421public RectListImpl getExclusiveOr(RectListImpl rli) {422Area a2 = new Area(theArea);423a2.exclusiveOr(((AreaImpl) rli).theArea);424return new AreaImpl(a2);425}426427public RectListImpl getDifference(RectListImpl rli) {428Area a2 = new Area(theArea);429a2.subtract(((AreaImpl) rli).theArea);430return new AreaImpl(a2);431}432433public RectListImpl getUnion(RectListImpl rli) {434Area a2 = new Area(theArea);435a2.add(((AreaImpl) rli).theArea);436return new AreaImpl(a2);437}438439// Used for making sure that 3xMAX translates yields an empty region440public boolean checkTransEmpty() {441// Area objects will actually survive 3 MAX translates so just442// pretend that it had the intended effect...443return true;444}445446public boolean contains(int x, int y) {447return theArea.contains(x, y);448}449450public int hashCode() {451// Area does not override hashCode...452return 0;453}454455public boolean equals(RectListImpl other) {456return theArea.equals(((AreaImpl) other).theArea);457}458459public String toString() {460return theArea.toString();461}462}463464public static class RegionImpl extends RectListImpl {465Region theRegion;466467public RegionImpl() {468}469470public RegionImpl(Region r) {471theRegion = r;472}473474public void addRect(int lox, int loy, int hix, int hiy) {475Region r2 = Region.getInstanceXYXY(lox, loy, hix, hiy);476if (theRegion == null) {477theRegion = r2;478} else {479theRegion = theRegion.getUnion(r2);480}481}482483public RectListImpl getTranslation(int dx, int dy) {484return new RegionImpl(theRegion.getTranslatedRegion(dx, dy));485}486487public RectListImpl getIntersection(RectListImpl rli) {488Region r2 = ((RegionImpl) rli).theRegion;489r2 = theRegion.getIntersection(r2);490return new RegionImpl(r2);491}492493public RectListImpl getExclusiveOr(RectListImpl rli) {494Region r2 = ((RegionImpl) rli).theRegion;495r2 = theRegion.getExclusiveOr(r2);496return new RegionImpl(r2);497}498499public RectListImpl getDifference(RectListImpl rli) {500Region r2 = ((RegionImpl) rli).theRegion;501r2 = theRegion.getDifference(r2);502return new RegionImpl(r2);503}504505public RectListImpl getUnion(RectListImpl rli) {506Region r2 = ((RegionImpl) rli).theRegion;507r2 = theRegion.getUnion(r2);508return new RegionImpl(r2);509}510511// Used for making sure that 3xMAX translates yields an empty region512public boolean checkTransEmpty() {513// Region objects should be empty after 3 MAX translates...514return theRegion.isEmpty();515}516517public boolean contains(int x, int y) {518return theRegion.contains(x, y);519}520521public int hashCode() {522return theRegion.hashCode();523}524525public boolean equals(RectListImpl other) {526return theRegion.equals(((RegionImpl) other).theRegion);527}528529public String toString() {530return theRegion.toString();531}532}533}534535536