1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math.transform;
18  
19  import junit.framework.TestCase;
20  
21  /**
22   * JUnit Test for HadamardTransformerTest
23   * @see org.apache.commons.math.transform.FastHadamardTransformer
24   */
25  public final class FastHadamardTransformerTest extends TestCase {
26  
27      /**
28       * Test of transformer for the a 8-point FHT (means n=8)
29       */
30      public void test8Points() {
31          checkAllTransforms(new int[] { 1, 4, -2, 3, 0, 1, 4, -1 },
32                         new int[] { 10, -4, 2, -4, 2, -12, 6, 8 });
33      }
34  
35      /**
36       * Test of transformer for the a 4-points FHT (means n=4)
37       */
38      public void test4Points() {
39          checkAllTransforms(new int[] { 1, 2, 3, 4 },
40                             new int[] { 10, -2, -4, 0 });
41      }
42  
43      /**
44       * Test the inverse transform of an integer vector is not always an integer vector
45       */
46      public void testNoIntInverse() {
47          FastHadamardTransformer transformer = new FastHadamardTransformer();
48          double[] x = transformer.inversetransform(new double[] { 0, 1, 0, 1});
49          assertEquals( 0.5, x[0], 0);
50          assertEquals(-0.5, x[1], 0);
51          assertEquals( 0.0, x[2], 0);
52          assertEquals( 0.0, x[3], 0);
53      }
54  
55      /**
56       * Test of transformer for wrong number of points
57       */
58      public void test3Points() {
59          try {
60              new FastHadamardTransformer().transform(new double[3]);
61              fail("an exception should have been thrown");
62          } catch (IllegalArgumentException iae) {
63              // expected
64          }
65      }
66  
67      private void checkAllTransforms(int[]x, int[] y) {
68          checkDoubleTransform(x, y);
69          checkInverseDoubleTransform(x, y);
70          checkIntTransform(x, y);
71      }
72  
73      private void checkDoubleTransform(int[]x, int[] y) {
74          // Initiate the transformer
75          FastHadamardTransformer transformer = new FastHadamardTransformer();
76  
77          // check double transform
78          double[] dX = new double[x.length];
79          for (int i = 0; i < dX.length; ++i) {
80              dX[i] = x[i];
81          }
82          double dResult[] = transformer.transform(dX);
83          for (int i = 0; i < dResult.length; i++) {
84              // compare computed results to precomputed results
85              assertEquals((double) y[i], dResult[i]);
86          }
87      }
88  
89      private void checkIntTransform(int[]x, int[] y) {
90          // Initiate the transformer
91          FastHadamardTransformer transformer = new FastHadamardTransformer();
92  
93          // check integer transform
94          int iResult[] = transformer.transform(x);
95          for (int i = 0; i < iResult.length; i++) {
96              // compare computed results to precomputed results
97              assertEquals(y[i], iResult[i]);
98          }
99  
100     }
101     
102     private void checkInverseDoubleTransform(int[]x, int[] y) {
103         // Initiate the transformer
104         FastHadamardTransformer transformer = new FastHadamardTransformer();
105 
106         // check double transform
107         double[] dY = new double[y.length];
108         for (int i = 0; i < dY.length; ++i) {
109             dY[i] = y[i];
110         }
111         double dResult[] = transformer.inversetransform(dY);
112         for (int i = 0; i < dResult.length; i++) {
113             // compare computed results to precomputed results
114             assertEquals((double) x[i], dResult[i]);
115         }
116 
117     }
118     
119 }