    1   package baseCode.dataStructure.matrix;
    2
    3   import junit.framework.TestCase;
    4   import baseCode.dataStructure.matrix.RCDoubleMatrix1D;
    5   import cern.colt.list.DoubleArrayList;
    6   import cern.colt.list.IntArrayList;
    7   import cern.colt.matrix.DoubleMatrix1D;
    8   import cern.colt.matrix.impl.DenseDoubleMatrix1D;
    9
    10  /***
    11   * <hr>
    12   * <p>
    13   * Copyright (c) 2004 Columbia University
    14   *
    15   * @author pavlidis
    16   * @version $Id: TestRCDoubleMatrix1D.java,v 1.1 2005/03/17 13:58:42 pavlidis Exp $
    17   */
    18  public class TestRCDoubleMatrix1D extends TestCase {
    19
    20     RCDoubleMatrix1D a;
    21     RCDoubleMatrix1D b;
    22     DoubleMatrix1D c;
    23
    24
    25     /*
    26      * @see TestCase#setUp()
    27      */
    28     protected void setUp() throws Exception {
    29
    30        /*
    31         * a: 0 1 2 0 5
    32         * b: 5 3 2 1 (0)
    33         *
    34         */
    35
    36        DoubleArrayList va = new DoubleArrayList( new double[] {
    37              1, 2, 5
    38        } );
    39         IntArrayList ina = new IntArrayList( new int[] {
    40               1, 2, 4
    41        } );
    42
    43        DoubleArrayList vb = new DoubleArrayList( new double[] {
    44              5, 3, 2, 1
    45        } );
    46        IntArrayList inb = new IntArrayList( new int[] {
    47              0, 1, 2, 3
    48        } );
    49
    50        DoubleArrayList vc = new DoubleArrayList( new double[] {
    51              5, 3, 2, 1
    52        } );
    53        IntArrayList inc = new IntArrayList( new int[] {
    54              0, 1, 2, 3
    55        } );
    56
    57
    58        a = new RCDoubleMatrix1D(ina, va );
    59        b = new RCDoubleMatrix1D(inb, vb );
    60
    61        c = new DenseDoubleMatrix1D( new double[] {5,3,2,1});
    62        super.setUp();
    63     }
    64
    65     public void testForEachNonZero() {
    66        DoubleMatrix1D actualReturn = a
    67              .forEachNonZero( new cern.colt.function.DoubleFunction() {
    68                 public double apply(  double value ) {
    69                    return value / 2.0;
    70                 }
    71              } );
    72        DoubleMatrix1D expectedReturn = new RCDoubleMatrix1D( new double[] {
    73              0, 0.5, 1, 0, 2.5
    74        } );
    75        assertEquals( "return value", new DoubleArrayList( expectedReturn
    76              .toArray() ), new DoubleArrayList( actualReturn.toArray() ) );
    77     }
    78
    79     /*
    80      * Class under test for double zDotProduct(DoubleMatrix1D)
    81      */
    82     public void testZDotProductDoubleMatrix1D() {
    83        double actualReturn = a.zDotProduct( b );
    84        double expectedReturn = 7;
    85        assertEquals( "return value", expectedReturn, actualReturn, 0.0001 );
    86     }
    87
    88
    89     /*
    90      * Class under test for double zDotProduct(DoubleMatrix1D)
    91      */
    92     public void testZDotProductDoubleMatrix1DReverse() {
    93        double actualReturn = b.zDotProduct( a );
    94        double expectedReturn = 7;
    95        assertEquals( "return value", expectedReturn, actualReturn, 0.0001 );
    96     }
    97
    98     /*
    99      * Class under test for double zDotProduct(DoubleMatrix1D)
    100     */
    101    public void testZDotProductDoubleMatrix1DHarder() {
    102       double actualReturn = a.zDotProduct( c  );
    103       double expectedReturn =  7;
    104       assertEquals( "return value", expectedReturn, actualReturn, 0.0001 );
    105    }
    106
    107    /*
    108     * Class under test for double zDotProduct(DoubleMatrix1D)
    109     */
    110    public void testZDotProductDoubleMatrix1DHarderReverse() {
    111       double actualReturn = c.zDotProduct( a  );
    112       double expectedReturn =  7;
    113       assertEquals( "return value", expectedReturn, actualReturn, 0.0001 );
    114    }
    115
    116
    117
    118    /*
    119     * Class under test for DoubleMatrix1D assign(DoubleFunction)
    120     */
    121    public void testAssignDoubleFunction() {
    122       DoubleMatrix1D actualReturn = a
    123             .assign( new cern.colt.function.DoubleFunction() {
    124                public double apply( double value ) {
    125                   return 2;
    126                }
    127             } );
    128       DoubleMatrix1D expectedReturn = new RCDoubleMatrix1D( new double[] {
    129             0, 2, 2, 0, 2
    130       } );
    131       assertEquals( "return value", new DoubleArrayList( expectedReturn
    132             .toArray() ), new DoubleArrayList( actualReturn.toArray() ) );
    133    }
    134
    135 }