001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025:
026: import junit.framework.Assert;
027:
028: import org.apache.commons.math.complex.Complex;
029:
030: /**
031: * @version $Revision: 348888 $ $Date: 2005-11-24 23:21:25 -0700 (Thu, 24 Nov 2005) $
032: */
033: public class TestUtils {
034: /**
035: *
036: */
037: private TestUtils() {
038: super ();
039: }
040:
041: public static void assertEquals(double expected, double actual,
042: double delta) {
043: assertEquals(null, expected, actual, delta);
044: }
045:
046: /**
047: * Verifies that expected and actual are within delta, or are both NaN or
048: * infinities of the same sign.
049: */
050: public static void assertEquals(String msg, double expected,
051: double actual, double delta) {
052: // check for NaN
053: if (Double.isNaN(expected)) {
054: Assert.assertTrue("" + actual + " is not NaN.", Double
055: .isNaN(actual));
056: } else {
057: Assert.assertEquals(msg, expected, actual, delta);
058: }
059: }
060:
061: /*
062: * Verifies that the two arguments are exactly the same, either
063: * both NaN or infinities of same sign, or identical floating point values.
064: */
065: public static void assertSame(double expected, double actual) {
066: assertEquals(expected, actual, 0);
067: }
068:
069: /**
070: * Verifies that real and imaginary parts of the two complex arguments
071: * are exactly the same. Also ensures that NaN / infinite components match.
072: */
073: public static void assertSame(Complex expected, Complex actual) {
074: assertSame(expected.getReal(), actual.getReal());
075: assertSame(expected.getImaginary(), actual.getImaginary());
076: }
077:
078: /**
079: * Verifies that real and imaginary parts of the two complex arguments
080: * differ by at most delta. Also ensures that NaN / infinite components match.
081: */
082: public static void assertEquals(Complex expected, Complex actual,
083: double delta) {
084: assertEquals(expected.getReal(), actual.getReal(), delta);
085: assertEquals(expected.getImaginary(), actual.getImaginary(),
086: delta);
087: }
088:
089: /**
090: * Verifies that two double arrays have equal entries, up to tolerance
091: */
092: public static void assertEquals(double a[], double b[],
093: double tolerance) {
094: Assert.assertEquals(a.length, b.length);
095: for (int i = 0; i < a.length; i++) {
096: Assert.assertEquals(a[i], b[i], tolerance);
097: }
098: }
099:
100: public static Object serializeAndRecover(Object o) {
101:
102: Object result = null;
103:
104: File tmp = null;
105: FileOutputStream fo = null;
106: FileInputStream fi = null;
107:
108: try {
109: // serialize the Object
110: tmp = File.createTempFile("test", ".ser");
111: fo = new FileOutputStream(tmp);
112: ObjectOutputStream so = new ObjectOutputStream(fo);
113: so.writeObject(o);
114: so.flush();
115: fo.close();
116:
117: // deserialize the Book
118: fi = new FileInputStream(tmp);
119: ObjectInputStream si = new ObjectInputStream(fi);
120: result = si.readObject();
121: } catch (Exception ex) {
122:
123: } finally {
124: if (fo != null) {
125: try {
126: fo.close();
127: } catch (IOException ex) {
128: }
129: }
130:
131: if (fi != null) {
132: try {
133: fi.close();
134: } catch (IOException ex) {
135: }
136: }
137: }
138:
139: if (tmp != null) {
140: tmp.delete();
141: }
142:
143: return result;
144: }
145:
146: /**
147: * Verifies that serialization preserves equals and hashCode
148: *
149: * @param object
150: */
151: public static void checkSerializedEquality(Object object) {
152: Object object2 = serializeAndRecover(object);
153: Assert.assertEquals("Equals check", object, object2);
154: Assert.assertEquals("HashCode check", object.hashCode(),
155: object2.hashCode());
156: }
157:
158: public static void assertRelativelyEquals(double expected,
159: double actual, double relativeError) {
160: assertRelativelyEquals(null, expected, actual, relativeError);
161: }
162:
163: public static void assertRelativelyEquals(String msg,
164: double expected, double actual, double relativeError) {
165: if (Double.isNaN(expected)) {
166: Assert.assertTrue(msg, Double.isNaN(actual));
167: } else if (Double.isNaN(actual)) {
168: Assert.assertTrue(msg, Double.isNaN(expected));
169: } else if (Double.isInfinite(actual)
170: || Double.isInfinite(expected)) {
171: Assert.assertEquals(expected, actual, relativeError);
172: } else if (expected == 0.0) {
173: Assert.assertEquals(msg, actual, expected, relativeError);
174: } else {
175: double x = Math.abs((expected - actual) / expected);
176: Assert.assertEquals(msg, 0.0, x, relativeError);
177: }
178: }
179: }
|