    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.complex;
    18
    19  import junit.framework.TestCase;
    20
    21  /***
    22   * @version $Revision: 1.6 $ $Date: 2004/02/21 21:35:16 $
    23   */
    24  public class ComplexTest extends TestCase {
    25
    26      public void testConstructor() {
    27          Complex z = new Complex(3.0, 4.0);
    28          assertEquals(3.0, z.getReal(), 1.0e-5);
    29          assertEquals(4.0, z.getImaginary(), 1.0e-5);
    30      }
    31
    32      public void testConstructorNaN() {
    33          Complex z = new Complex(3.0, Double.NaN);
    34          assertTrue(z.isNaN());
    35
    36          z = new Complex(Double.NaN, 4.0);
    37          assertTrue(z.isNaN());
    38
    39          z = new Complex(3.0, 4.0);
    40          assertFalse(z.isNaN());
    41      }
    42
    43      public void testAbs() {
    44          Complex z = new Complex(3.0, 4.0);
    45          assertEquals(5.0, z.abs(), 1.0e-5);
    46      }
    47
    48      public void testAbsNaN() {
    49          assertTrue(Double.isNaN(Complex.NaN.abs()));
    50      }
    51
    52      public void testAdd() {
    53          Complex x = new Complex(3.0, 4.0);
    54          Complex y = new Complex(5.0, 6.0);
    55          Complex z = x.add(y);
    56          assertEquals(8.0, z.getReal(), 1.0e-5);
    57          assertEquals(10.0, z.getImaginary(), 1.0e-5);
    58      }
    59
    60      public void testAddNaN() {
    61          Complex x = new Complex(3.0, 4.0);
    62          Complex z = x.add(Complex.NaN);
    63          assertTrue(z.isNaN());
    64      }
    65
    66      public void testConjugate() {
    67          Complex x = new Complex(3.0, 4.0);
    68          Complex z = x.conjugate();
    69          assertEquals(3.0, z.getReal(), 1.0e-5);
    70          assertEquals(-4.0, z.getImaginary(), 1.0e-5);
    71      }
    72
    73      public void testConjugateNaN() {
    74          Complex z = Complex.NaN.conjugate();
    75          assertTrue(z.isNaN());
    76      }
    77
    78      public void testDivide() {
    79          Complex x = new Complex(3.0, 4.0);
    80          Complex y = new Complex(5.0, 6.0);
    81          Complex z = x.divide(y);
    82          assertEquals(39.0 / 61.0, z.getReal(), 1.0e-5);
    83          assertEquals(2.0 / 61.0, z.getImaginary(), 1.0e-5);
    84      }
    85
    86      public void testDivideNaN() {
    87          Complex x = new Complex(3.0, 4.0);
    88          Complex z = x.divide(Complex.NaN);
    89          assertTrue(z.isNaN());
    90      }
    91
    92      public void testMultiply() {
    93          Complex x = new Complex(3.0, 4.0);
    94          Complex y = new Complex(5.0, 6.0);
    95          Complex z = x.multiply(y);
    96          assertEquals(-9.0, z.getReal(), 1.0e-5);
    97          assertEquals(38.0, z.getImaginary(), 1.0e-5);
    98      }
    99
    100     public void testMultiplyNaN() {
    101         Complex x = new Complex(3.0, 4.0);
    102         Complex z = x.multiply(Complex.NaN);
    103         assertTrue(z.isNaN());
    104     }
    105
    106     public void testNegate() {
    107         Complex x = new Complex(3.0, 4.0);
    108         Complex z = x.negate();
    109         assertEquals(-3.0, z.getReal(), 1.0e-5);
    110         assertEquals(-4.0, z.getImaginary(), 1.0e-5);
    111     }
    112
    113     public void testNegateNaN() {
    114         Complex z = Complex.NaN.negate();
    115         assertTrue(z.isNaN());
    116     }
    117
    118     public void testSubtract() {
    119         Complex x = new Complex(3.0, 4.0);
    120         Complex y = new Complex(5.0, 6.0);
    121         Complex z = x.subtract(y);
    122         assertEquals(-2.0, z.getReal(), 1.0e-5);
    123         assertEquals(-2.0, z.getImaginary(), 1.0e-5);
    124     }
    125
    126     public void testSubtractNaN() {
    127         Complex x = new Complex(3.0, 4.0);
    128         Complex z = x.subtract(Complex.NaN);
    129         assertTrue(z.isNaN());
    130     }
    131
    132     public void testEqualsNull() {
    133         Complex x = new Complex(3.0, 4.0);
    134         assertFalse(x.equals(null));
    135     }
    136
    137     public void testEqualsClass() {
    138         Complex x = new Complex(3.0, 4.0);
    139         assertFalse(x.equals(this));
    140     }
    141
    142     public void testEqualsSame() {
    143         Complex x = new Complex(3.0, 4.0);
    144         assertTrue(x.equals(x));
    145     }
    146
    147     public void testEqualsTrue() {
    148         Complex x = new Complex(3.0, 4.0);
    149         Complex y = new Complex(3.0, 4.0);
    150         assertTrue(x.equals(y));
    151     }
    152
    153     public void testEqualsRealDifference() {
    154         Complex x = new Complex(0.0, 0.0);
    155         Complex y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
    156         assertFalse(x.equals(y));
    157     }
    158
    159     public void testEqualsImaginaryDifference() {
    160         Complex x = new Complex(0.0, 0.0);
    161         Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
    162         assertFalse(x.equals(y));
    163     }
    164 }