1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import static org.junit.Assert.assertEquals;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.Test;
public class TestCases {
private static void test(double expected, double[] arrVal, String[] arrUnit) {
assertEquals(expected, Solution.solution(arrVal, arrUnit), expected * 1e-5);
}
@Test
public void fixedTests() {
test(6.67e-12, new double[] {1000, 1000, 100}, new String[] {"g", "kg", "m"});
test(6.67e-9, new double[] {1000, 1000, 100}, new String[] {"kg", "kg", "m"});
test(0.0000667, new double[] {1000, 1000, 100}, new String[] {"kg", "kg", "cm"});
}
private static final String[] WEIGHT_UNITS = {"kg", "g", "mg", "μg", "lb"};
private static final double[] WEIGHT_UNIT_CONVERSION_FACTORS = {1, 1e-3, 1e-6, 1e-9, 0.453592};
private static final String[] DISTANCE_UNITS = {"m", "cm", "mm", "μm", "ft"};
private static final double[] DISTANCE_UNIT_CONVERSION_FACTORS = {1, 1e-2, 1e-3, 1e-6, 0.3048};
@Test
public void randomTests() {
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < 100; i++) {
double m1 = random.nextDouble(1, 1e5);
double m2 = random.nextDouble(1, 1e5);
double r = random.nextDouble(0.3, 1e7);
double[] arrVal = new double[] {m1, m2, r};
double f = 6.67e-11 * m1 * m2 / r / r;
int u1 = random.nextInt(WEIGHT_UNITS.length);
int u2 = random.nextInt(WEIGHT_UNITS.length);
int u3 = random.nextInt(DISTANCE_UNITS.length);
arrVal[0] /= WEIGHT_UNIT_CONVERSION_FACTORS[u1];
arrVal[1] /= WEIGHT_UNIT_CONVERSION_FACTORS[u2];
arrVal[2] /= DISTANCE_UNIT_CONVERSION_FACTORS[u3];
String[] arrUnit = new String[] {WEIGHT_UNITS[u1], WEIGHT_UNITS[u2], DISTANCE_UNITS[u3]};
test(f, arrVal, arrUnit);
}
}
}
|