Skip to content

Commit f5599ce

Browse files
committed
add tests for ZdImpl
1 parent 69c438b commit f5599ce

File tree

3 files changed

+1524
-0
lines changed

3 files changed

+1524
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
/*
15+
* Any changes, bugfixes or additions made by the maintainers
16+
* of the https://github.com/stefan-zobel/FFT library are
17+
* licensed under the Apache License, Version 2.0, as explained
18+
* at http://www.apache.org/licenses/LICENSE-2.0
19+
*/
20+
package net.jamu.complex;
21+
22+
import java.util.Random;
23+
24+
import org.junit.Assert;
25+
26+
public final class TestUtils {
27+
28+
private static final Random rng = new Random();
29+
30+
/**
31+
* Verifies that expected and actual are within delta, or are both NaN or
32+
* infinities of the same sign.
33+
*/
34+
public static void assertEquals(double expected, double actual, double delta) {
35+
Assert.assertEquals(null, expected, actual, delta);
36+
}
37+
38+
/**
39+
* Verifies that expected and actual are within delta, or are both NaN or
40+
* infinities of the same sign.
41+
*/
42+
public static void assertEquals(String msg, double expected, double actual, double delta) {
43+
// check for NaN
44+
if (Double.isNaN(expected)) {
45+
Assert.assertTrue("" + actual + " is not NaN.", Double.isNaN(actual));
46+
} else {
47+
Assert.assertEquals(msg, expected, actual, delta);
48+
}
49+
}
50+
51+
/**
52+
* Verifies that the two arguments are exactly the same, either both NaN or
53+
* infinities of same sign, or identical floating point values.
54+
*/
55+
public static void assertSame(double expected, double actual) {
56+
Assert.assertEquals(expected, actual, 0);
57+
}
58+
59+
/**
60+
* Verifies that real and imaginary parts of the two complex arguments are
61+
* exactly the same. Also ensures that NaN / infinite components match.
62+
*/
63+
public static void assertSame(Zd expected, Zd actual) {
64+
assertSame(expected.re(), actual.re());
65+
assertSame(expected.im(), actual.im());
66+
}
67+
68+
/**
69+
* Verifies that real and imaginary parts of the two complex arguments
70+
* differ by at most delta. Also ensures that NaN / infinite components
71+
* match.
72+
*/
73+
public static void assertEquals(Zd expected, Zd actual, double delta) {
74+
Assert.assertEquals(expected.re(), actual.re(), delta);
75+
Assert.assertEquals(expected.im(), actual.im(), delta);
76+
}
77+
78+
/**
79+
* Verifies that the relative error in actual versus expected is less than
80+
* or equal to relativeError. If expected is infinite or NaN, actual must be
81+
* the same (NaN or infinity of the same sign).
82+
*
83+
* @param expected
84+
* expected value
85+
* @param actual
86+
* observed value
87+
* @param relativeError
88+
* maximum allowable relative error
89+
*/
90+
public static void assertRelativelyEquals(double expected, double actual, double relativeError) {
91+
assertRelativelyEquals(null, expected, actual, relativeError);
92+
}
93+
94+
/**
95+
* Verifies that the relative error in actual versus expected is less than
96+
* or equal to relativeError. If expected is infinite or NaN, actual must be
97+
* the same (NaN or infinity of the same sign).
98+
*
99+
* @param msg
100+
* message to return with failure
101+
* @param expected
102+
* expected value
103+
* @param actual
104+
* observed value
105+
* @param relativeError
106+
* maximum allowable relative error
107+
*/
108+
public static void assertRelativelyEquals(String msg, double expected, double actual, double relativeError) {
109+
if (Double.isNaN(expected)) {
110+
Assert.assertTrue(msg, Double.isNaN(actual));
111+
} else if (Double.isNaN(actual)) {
112+
Assert.assertTrue(msg, Double.isNaN(expected));
113+
} else if (Double.isInfinite(actual) || Double.isInfinite(expected)) {
114+
Assert.assertEquals(expected, actual, relativeError);
115+
} else if (expected == 0.0) {
116+
Assert.assertEquals(msg, actual, expected, relativeError);
117+
} else {
118+
double absError = Math.abs(expected) * relativeError;
119+
Assert.assertEquals(msg, expected, actual, absError);
120+
}
121+
}
122+
123+
// each individual double lies in [-1, 1)
124+
public static double[] randomData(int length) {
125+
double[] rand = new double[length];
126+
for (int i = 0; i < rand.length; ++i) {
127+
rand[i] = (rng.nextDouble() * 2.0) - 1.0;
128+
}
129+
return rand;
130+
}
131+
132+
// range is [3, 8193]
133+
public static int randLengthOdd() {
134+
int len = -1;
135+
do {
136+
len = rng.nextInt(8194);
137+
} while (len <= 2 || (len % 2 == 0));
138+
return len;
139+
}
140+
141+
// range is [6, 8194]
142+
public static int randLengthEvenNotPowerOf2() {
143+
int len = -1;
144+
do {
145+
len = rng.nextInt(8195);
146+
} while (len <= 2 || (len % 2 != 0) || (isPowerOfTwo(len)));
147+
return len;
148+
}
149+
150+
public static boolean isPowerOfTwo(int n) {
151+
return (n > 0) && ((n & (n - 1)) == 0);
152+
}
153+
154+
private TestUtils() {
155+
throw new AssertionError();
156+
}
157+
}

0 commit comments

Comments
 (0)