001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package java.awt.geom;
018:
019: import java.awt.SerializeTestCase;
020: import java.awt.Tools;
021: import java.util.Arrays;
022:
023: public class GeomTestCase extends SerializeTestCase {
024:
025: public GeomTestCase(String name) {
026: super (name);
027: }
028:
029: void fail(String message, Object expected, Object actual) {
030: if (message == null) {
031: fail("expected: <" + expected + "> but was: <" + actual
032: + ">");
033: } else {
034: fail(message + " expected: <" + expected + "> but was: <"
035: + actual + ">");
036: }
037: }
038:
039: String concat(String msg1, String msg2) {
040: if (msg1 == null) {
041: return msg2;
042: }
043: if (msg2 == null) {
044: return msg2;
045: }
046: return msg1 + ". " + msg2;
047: }
048:
049: public void assertEquals(String message, Line2D a1, Line2D a2) {
050: if (!a1.getClass().equals(a2.getClass())
051: || !a1.getP1().equals(a2.getP1())
052: || !a1.getP2().equals(a2.getP2())) {
053: fail(message, a1, a2);
054: }
055: }
056:
057: public void assertEquals(Line2D a1, Line2D a2) {
058: assertEquals(null, a1, a2);
059:
060: }
061:
062: public void assertEquals(String message, Arc2D a1, Arc2D a2,
063: double delta) {
064: if (!a1.getClass().equals(a2.getClass())
065: || Math.abs(a1.getX() - a2.getX()) > delta
066: || Math.abs(a1.getY() - a2.getY()) > delta
067: || Math.abs(a1.getWidth() - a2.getWidth()) > delta
068: || Math.abs(a1.getHeight() - a2.getHeight()) > delta
069: || Math.abs(a1.getAngleStart() - a2.getAngleStart()) > delta
070: || Math.abs(a1.getAngleExtent() - a2.getAngleExtent()) > delta) {
071: fail(message, arcToStr(a1), arcToStr(a2));
072: }
073: }
074:
075: public void assertEquals(String message, Arc2D a1, Arc2D a2) {
076: assertEquals(message, a1, a2, 0.0);
077: }
078:
079: public void assertEquals(Arc2D a1, Arc2D a2, double delta) {
080: assertEquals(null, a1, a2, delta);
081: }
082:
083: public void assertEquals(Arc2D a1, Arc2D a2) {
084: assertEquals(null, a1, a2, 0.0);
085: }
086:
087: public void assertEquals(String message, RoundRectangle2D a1,
088: RoundRectangle2D a2) {
089: if (!a1.getClass().equals(a2.getClass())
090: || a1.getX() != a2.getX() || a1.getY() != a2.getY()
091: || a1.getWidth() != a2.getWidth()
092: || a1.getHeight() != a2.getHeight()
093: || a1.getArcWidth() != a2.getArcWidth()
094: || a1.getArcHeight() != a2.getArcHeight()) {
095: fail(message, a1, a2);
096: }
097: }
098:
099: public void assertEquals(RoundRectangle2D a1, RoundRectangle2D a2) {
100: assertEquals(null, a1, a2);
101: }
102:
103: public void assertEquals(String message, Point2D a1, Point2D a2,
104: double delta) {
105: if (!a1.getClass().equals(a2.getClass())
106: || Math.abs(a1.getX() - a2.getX()) > delta
107: || Math.abs(a1.getY() - a2.getY()) > delta) {
108: fail(message, a1, a2);
109: }
110: }
111:
112: public void assertEquals(Point2D a1, Point2D a2, double delta) {
113: assertEquals(null, a1, a2, delta);
114: }
115:
116: public boolean equalRects(Rectangle2D a1, Rectangle2D a2,
117: double delta) {
118: return !(Math.abs(a1.getX() - a2.getX()) > delta
119: || Math.abs(a1.getY() - a2.getY()) > delta
120: || Math.abs(a1.getWidth() - a2.getWidth()) > delta || Math
121: .abs(a1.getHeight() - a2.getHeight()) > delta);
122: }
123:
124: public void assertEquals(String message, Rectangle2D a1,
125: Rectangle2D a2, double delta) {
126: if (!a1.getClass().equals(a2.getClass())
127: || !equalRects(a1, a2, delta)) {
128: fail(message, a1, a2);
129: }
130: }
131:
132: public void assertEquals(Rectangle2D a1, Rectangle2D a2,
133: double delta) {
134: assertEquals(null, a1, a2, delta);
135: }
136:
137: public void assertEquals(String msg, double[] a1, double a2[],
138: int length, double delta) {
139: if (a1.length < length) {
140: fail(concat(msg, "Wrong array length " + a1.length
141: + ", needed at least " + length));
142: }
143: if (a2.length < length) {
144: fail(concat(msg, "Wrong array length " + a2.length
145: + ", needed at least " + length));
146: }
147: for (int i = 0; i < length; i++) {
148: if (a1[i] != a2[i]) {
149: fail(msg, Arrays.toString(a1), Arrays.toString(a2));
150: }
151: }
152: }
153:
154: public void assertEquals(double[] a1, double a2[], int length,
155: double delta) {
156: assertEquals(null, a1, a2, length, delta);
157: }
158:
159: public void assertEquals(String msg, float[] a1, float a2[],
160: int length, float delta) {
161: if (a1.length < length) {
162: fail(concat(msg, "Wrong array length " + a1.length
163: + ", needed at least " + length));
164: }
165: if (a2.length < length) {
166: fail(concat(msg, "Wrong array length " + a2.length
167: + ", needed at least " + length));
168: }
169: for (int i = 0; i < length; i++) {
170: if (a1[i] != a2[i]) {
171: fail(msg, Arrays.toString(a1), Arrays.toString(a2));
172: }
173: }
174: }
175:
176: public void assertEquals(float[] a1, float a2[], int length,
177: float delta) {
178: assertEquals(null, a1, a2, length, delta);
179: }
180:
181: public void assertEquals(String msg, int[] a1, int a2[], int length) {
182: if (a1.length < length) {
183: fail(concat(msg, "Wrong array length " + a1.length
184: + ", needed at least " + length));
185: }
186: if (a2.length < length) {
187: fail(concat(msg, "Wrong array length " + a2.length
188: + ", needed at least " + length));
189: }
190: for (int i = 0; i < length; i++) {
191: if (a1[i] != a2[i]) {
192: fail(msg, Arrays.toString(a1), Arrays.toString(a2));
193: }
194: }
195: }
196:
197: public void assertEquals(int[] a1, int a2[], int length) {
198: assertEquals(null, a1, a2, length);
199: }
200:
201: /*
202: void assertEquals(String message, GeneralPath a1, GeneralPath a2, double delta) {
203: if (!a1.getClass().equals(a2.getClass()) ||
204: a1.getWindingRule() != a2.getWindingRule() ||
205: !Tools.PathIterator.equals(a1.getPathIterator(null), a2.getPathIterator(null), delta))
206: {
207: fail(message, a1, a2);
208: }
209: }
210:
211: void assertEquals(GeneralPath a1, GeneralPath a2, float delta) {
212: assertEquals(null, a1, a2, delta);
213: }
214:
215: void assertEquals(String message, PathIterator a1, PathIterator a2, double delta) {
216: if (!a1.getClass().equals(a2.getClass()) ||
217: a1.getWindingRule() != a2.getWindingRule() ||
218: !Tools.PathIterator.equals(a1, a2, delta))
219: {
220: fail(message, a1, a2);
221: }
222: }
223:
224: void assertEquals(String message, GeneralPath a1, GeneralPath a2, float delta) {
225: if (!a1.getClass().equals(a2.getClass()) ||
226: a1.getWindingRule() != a2.getWindingRule() ||
227: !Tools.PathIterator.equals(a1.getPathIterator(null), a2.getPathIterator(null), delta))
228: {
229: fail(message, a1, a2);
230: }
231: }
232:
233: void assertEquals(GeneralPath a1, GeneralPath a2, double delta) {
234: assertEquals(null, a1, a2, delta);
235: }
236: */
237:
238: void assertEquals(GeneralPath a1, GeneralPath a2, double delta) {
239: assertEquals(null, a1.getPathIterator(null), a2
240: .getPathIterator(null), delta);
241: }
242:
243: void assertEquals(GeneralPath a1, GeneralPath a2, float delta) {
244: assertEquals(null, a1.getPathIterator(null), a2
245: .getPathIterator(null), delta);
246: }
247:
248: void assertEquals(String message, PathIterator a1, PathIterator a2,
249: double delta) {
250: if (!a1.getClass().equals(a2.getClass())
251: || !Tools.PathIterator.equals(a1, a2, delta)) {
252: fail(message, a1, a2);
253: }
254: }
255:
256: void assertEquals(String message, PathIterator a1, PathIterator a2,
257: float delta) {
258: if (!a1.getClass().equals(a2.getClass())
259: || !Tools.PathIterator.equals(a1, a2, delta)) {
260: fail(message, a1, a2);
261: }
262: }
263:
264: void assertEquals(PathIterator a1, PathIterator a2, double delta) {
265: assertEquals(null, a1, a2, delta);
266: }
267:
268: void assertEquals(PathIterator a1, PathIterator a2, float delta) {
269: assertEquals(null, a1, a2, delta);
270: }
271:
272: void assertEquals(String message, QuadCurve2D a1, QuadCurve2D a2,
273: double delta) {
274: if (!a1.getClass().equals(a2.getClass())
275: || Math.abs(a1.getX1() - a2.getX1()) > delta
276: || Math.abs(a1.getY1() - a2.getY1()) > delta
277: || Math.abs(a1.getX2() - a2.getX2()) > delta
278: || Math.abs(a1.getY2() - a2.getY2()) > delta
279: || Math.abs(a1.getCtrlX() - a2.getCtrlX()) > delta
280: || Math.abs(a1.getCtrlY() - a2.getCtrlY()) > delta) {
281: fail(message, quadToStr(a1), quadToStr(a2));
282: }
283: }
284:
285: void assertEquals(QuadCurve2D a1, QuadCurve2D a2, double delta) {
286: assertEquals(null, a1, a2, delta);
287: }
288:
289: void assertEquals(String message, CubicCurve2D a1, CubicCurve2D a2,
290: double delta) {
291: if (!a1.getClass().equals(a2.getClass())
292: || Math.abs(a1.getX1() - a2.getX1()) > delta
293: || Math.abs(a1.getY1() - a2.getY1()) > delta
294: || Math.abs(a1.getX2() - a2.getX2()) > delta
295: || Math.abs(a1.getY2() - a2.getY2()) > delta
296: || Math.abs(a1.getCtrlX1() - a2.getCtrlX1()) > delta
297: || Math.abs(a1.getCtrlY1() - a2.getCtrlY1()) > delta
298: || Math.abs(a1.getCtrlX2() - a2.getCtrlX2()) > delta
299: || Math.abs(a1.getCtrlY2() - a2.getCtrlY2()) > delta) {
300: fail(message, cubicToStr(a1), cubicToStr(a2));
301: }
302: }
303:
304: void assertEquals(CubicCurve2D a1, CubicCurve2D a2, double delta) {
305: assertEquals(null, a1, a2, delta);
306: }
307:
308: String doubleToStr(double a) {
309: if (a == (int) a) {
310: return "" + (int) a;
311: }
312: return "" + a;
313: }
314:
315: String arcToStr(Arc2D a) {
316: return a.getClass().getName() + "[x=" + a.getX() + ",y="
317: + a.getY() + ",width=" + a.getWidth() + ",height="
318: + a.getHeight() + ",start=" + a.getAngleStart()
319: + ",extent=" + a.getAngleExtent() + "]";
320: }
321:
322: String quadToStr(QuadCurve2D a) {
323: return a.getClass().getName() + "[x1=" + a.getX1() + ",y1="
324: + a.getY1() + ",x2=" + a.getCtrlX() + ",y2="
325: + a.getCtrlY() + ",x3=" + a.getX2() + ",y3="
326: + a.getY2() + "]";
327: }
328:
329: String cubicToStr(CubicCurve2D a) {
330: return a.getClass().getName() + "[x1=" + a.getX1() + ",y1="
331: + a.getY1() + ",x2=" + a.getCtrlX1() + ",y2="
332: + a.getCtrlY1() + ",x3=" + a.getCtrlX2() + ",y3="
333: + a.getCtrlY2() + ",x4=" + a.getX2() + ",y4="
334: + a.getY2() + "]";
335: }
336:
337: }
|