    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.apache.commons.math;
    18
    19  import java.io.File;
    20  import java.io.FileInputStream;
    21  import java.io.FileOutputStream;
    22  import java.io.IOException;
    23  import java.io.ObjectInputStream;
    24  import java.io.ObjectOutputStream;
    25
    26  import junit.framework.Assert;
    27
    28  import org.apache.commons.math.complex.Complex;
    29
    30  /***
    31   * @version $Revision: 348888 $ $Date: 2005-11-24 23:21:25 -0700 (Thu, 24 Nov 2005) $
    32   */
    33  public class TestUtils {
    34      /***
    35       *
    36       */
    37      private TestUtils() {
    38          super();
    39      }
    40
    41      public static void assertEquals(double expected, double actual, double delta) {
    42          assertEquals(null, expected, actual, delta);
    43      }
    44
    45      /***
    46       * Verifies that expected and actual are within delta, or are both NaN or
    47       * infinities of the same sign.
    48       */
    49      public static void assertEquals(String msg, double expected, double actual, double delta) {
    50          // check for NaN
    51          if(Double.isNaN(expected)){
    52              Assert.assertTrue("" + actual + " is not NaN.",
    53                  Double.isNaN(actual));
    54          } else {
    55              Assert.assertEquals(msg, expected, actual, delta);
    56          }
    57      }
    58
    59      /*
    60       * Verifies that the two arguments are exactly the same, either
    61       * both NaN or infinities of same sign, or identical floating point values.
    62       */
    63      public static void assertSame(double expected, double actual) {
    64       assertEquals(expected, actual, 0);
    65      }
    66
    67      /***
    68       * Verifies that real and imaginary parts of the two complex arguments
    69       * are exactly the same.  Also ensures that NaN / infinite components match.
    70       */
    71      public static void assertSame(Complex expected, Complex actual) {
    72          assertSame(expected.getReal(), actual.getReal());
    73          assertSame(expected.getImaginary(), actual.getImaginary());
    74      }
    75
    76      /***
    77       * Verifies that real and imaginary parts of the two complex arguments
    78       * differ by at most delta.  Also ensures that NaN / infinite components match.
    79       */
    80      public static void assertEquals(Complex expected, Complex actual, double delta) {
    81          assertEquals(expected.getReal(), actual.getReal(), delta);
    82          assertEquals(expected.getImaginary(), actual.getImaginary(), delta);
    83      }
    84
    85      /***
    86       * Verifies that two double arrays have equal entries, up to tolerance
    87       */
    88      public static void assertEquals(double a[], double b[], double tolerance) {
    89          Assert.assertEquals(a.length, b.length);
    90          for (int i = 0; i < a.length; i++) {
    91              Assert.assertEquals(a[i], b[i], tolerance);
    92          }
    93      }
    94
    95      public static Object serializeAndRecover(Object o){
    96
    97          Object result = null;
    98
    99          File tmp = null;
    100         FileOutputStream fo = null;
    101         FileInputStream fi = null;
    102
    103         try {
    104             // serialize the Object
    105             tmp = File.createTempFile("test",".ser");
    106             fo = new FileOutputStream(tmp);
    107             ObjectOutputStream so = new ObjectOutputStream(fo);
    108             so.writeObject(o);
    109             so.flush();
    110             fo.close();
    111
    112             // deserialize the Book
    113             fi = new FileInputStream(tmp);
    114             ObjectInputStream si = new ObjectInputStream(fi);
    115             result = si.readObject();
    116         } catch (Exception ex) {
    117
    118         } finally {
    119             if (fo != null) {
    120                 try {
    121                     fo.close();
    122                 } catch (IOException ex) {
    123                 }
    124             }
    125
    126             if (fi != null) {
    127                 try {
    128                     fi.close();
    129                 } catch (IOException ex) {
    130                 }
    131             }
    132         }
    133
    134
    135         if (tmp != null) {
    136             tmp.delete();
    137         }
    138
    139         return result;
    140     }
    141
    142     /***
    143      * Verifies that serialization preserves equals and hashCode
    144      *
    145      * @param object
    146      */
    147     public static void checkSerializedEquality(Object object) {
    148         Object object2 = serializeAndRecover(object);
    149         Assert.assertEquals("Equals check", object, object2);
    150         Assert.assertEquals("HashCode check", object.hashCode(), object2.hashCode());
    151     }
    152
    153     public static void assertRelativelyEquals(double expected, double actual, double relativeError) {
    154         assertRelativelyEquals(null, expected, actual, relativeError);
    155     }
    156
    157     public static void assertRelativelyEquals(String msg, double expected, double actual, double relativeError) {
    158         if (Double.isNaN(expected)) {
    159             Assert.assertTrue(msg, Double.isNaN(actual));
    160         } else if (Double.isNaN(actual)) {
    161             Assert.assertTrue(msg, Double.isNaN(expected));
    162         } else if (Double.isInfinite(actual) || Double.isInfinite(expected)) {
    163             Assert.assertEquals(expected, actual, relativeError);
    164         } else if (expected == 0.0) {
    165             Assert.assertEquals(msg, actual, expected, relativeError);
    166         } else {
    167             double x = Math.abs((expected - actual) / expected);
    168             Assert.assertEquals(msg, 0.0, x, relativeError);
    169         }
    170     }
    171 }