001    package org.hackystat.projectbrowser.page.trajectory.dtw;
002    
003    import static org.junit.Assert.assertEquals;
004    
005    import org.junit.Test;
006    
007    /**
008     * Tests the Euclidean distance.
009     *
010     * @author Pavel Senin.
011     *
012     */
013    public class TestEuclideanDistance {
014    
015      // 1D points for the test
016      private static final double[] testPoint1D1 = { 0.545 };
017      private static final double[] testPoint1D2 = { 0.845 };
018    
019      // 3D points for the test
020      private static final double[] testPoint3D1 = { 0.545, 0.856, 0.856 };
021      private static final double[] testPoint3D2 = { 0.845, 0.654, 0.986 };
022    
023      // 2d series for the test
024      private static final double[][] testSeries1 = { { 0.2, 0.6 }, { 0.3, 0.5 }, { 0.4, 0.4 },
025          { 0.5, 0.3 }, { 0.6, 0.2 } };
026      private static final double[][] testSeries2 = { { 1.0, 1.8 }, { 1.2, 1.6 }, { 1.4, 1.4 },
027          { 1.6, 1.2 }, { 1.8, 1.0 } };
028    
029      /**
030       * Test the distance between single points.
031       *
032       * @throws DTWException If error occures.
033       */
034      @Test
035      public void testPointDistance() throws DTWException {
036        assertEquals("test 1D distance", EuclideanDistance.pointDistance(testPoint1D1, testPoint1D2),
037            Math.abs(testPoint1D2[0] - testPoint1D1[0]), 0.01D);
038    
039        double dist = 0D;
040        for (int i = 0; i < 3; i++) {
041          dist += (testPoint3D1[i] - testPoint3D2[i]) * (testPoint3D1[i] - testPoint3D2[i]);
042        }
043        dist = Math.sqrt(dist);
044    
045        assertEquals("test 1D distance", EuclideanDistance.pointDistance(testPoint3D1, testPoint3D2),
046            dist,0.01D);
047      }
048    
049      /**
050       * Test the distance between two series.
051       *
052       * @throws DTWException If error occures.
053       */
054      @Test
055      public void testSeriesDistance() throws DTWException {
056    
057        Double dist = EuclideanDistance.getSeriesDistnace(testSeries1, testSeries2);
058         assertEquals("testing distance, ", 3.193743885, dist, 0.01D);
059      }
060    
061    }