001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.math.transform; 018 019 import junit.framework.TestCase; 020 021 /** 022 * JUnit Test for HadamardTransformerTest 023 * @see org.apache.commons.math.transform.FastHadamardTransformer 024 */ 025 public final class FastHadamardTransformerTest extends TestCase { 026 027 /** 028 * Test of transformer for the a 8-point FHT (means n=8) 029 */ 030 public void test8Points() { 031 checkAllTransforms(new int[] { 1, 4, -2, 3, 0, 1, 4, -1 }, 032 new int[] { 10, -4, 2, -4, 2, -12, 6, 8 }); 033 } 034 035 /** 036 * Test of transformer for the a 4-points FHT (means n=4) 037 */ 038 public void test4Points() { 039 checkAllTransforms(new int[] { 1, 2, 3, 4 }, 040 new int[] { 10, -2, -4, 0 }); 041 } 042 043 /** 044 * Test the inverse transform of an integer vector is not always an integer vector 045 */ 046 public void testNoIntInverse() { 047 FastHadamardTransformer transformer = new FastHadamardTransformer(); 048 double[] x = transformer.inversetransform(new double[] { 0, 1, 0, 1}); 049 assertEquals( 0.5, x[0], 0); 050 assertEquals(-0.5, x[1], 0); 051 assertEquals( 0.0, x[2], 0); 052 assertEquals( 0.0, x[3], 0); 053 } 054 055 /** 056 * Test of transformer for wrong number of points 057 */ 058 public void test3Points() { 059 try { 060 new FastHadamardTransformer().transform(new double[3]); 061 fail("an exception should have been thrown"); 062 } catch (IllegalArgumentException iae) { 063 // expected 064 } 065 } 066 067 private void checkAllTransforms(int[]x, int[] y) { 068 checkDoubleTransform(x, y); 069 checkInverseDoubleTransform(x, y); 070 checkIntTransform(x, y); 071 } 072 073 private void checkDoubleTransform(int[]x, int[] y) { 074 // Initiate the transformer 075 FastHadamardTransformer transformer = new FastHadamardTransformer(); 076 077 // check double transform 078 double[] dX = new double[x.length]; 079 for (int i = 0; i < dX.length; ++i) { 080 dX[i] = x[i]; 081 } 082 double dResult[] = transformer.transform(dX); 083 for (int i = 0; i < dResult.length; i++) { 084 // compare computed results to precomputed results 085 assertEquals((double) y[i], dResult[i]); 086 } 087 } 088 089 private void checkIntTransform(int[]x, int[] y) { 090 // Initiate the transformer 091 FastHadamardTransformer transformer = new FastHadamardTransformer(); 092 093 // check integer transform 094 int iResult[] = transformer.transform(x); 095 for (int i = 0; i < iResult.length; i++) { 096 // compare computed results to precomputed results 097 assertEquals(y[i], iResult[i]); 098 } 099 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 }