-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericTest.java
159 lines (143 loc) · 5.37 KB
/
GenericTest.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package expression.generic;
import expression.BaseTest;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* @author Georgiy Korneev ([email protected])
*/
public class GenericTest extends BaseTest {
private static final int SIZE = 20;
public static final int MAX = Integer.MAX_VALUE - 1;
public static final int MIN = Integer.MIN_VALUE;
protected final List<Op<F<?>>> tests = new ArrayList<>();
private final Tabulator tabulator = new GenericTabulator();
public GenericTest() {
allConst("10", 10);
allConst("10 + 4 / 2 - 7", 5);
all(
"x",
(x, y, z) -> (long) x,
(x, y, z) -> (double) x,
(x, y, z) -> bi(x)
);
all(
"y + 2",
(x, y, z) -> y + 2L,
(x, y, z) -> y + 2.0,
(x, y, z) -> bi(y).add(bi(2))
);
all(
"z / 2",
(x, y, z) -> z / 2L,
(x, y, z) -> z / 2.0,
(x, y, z) -> bi(z / 2)
);
all(
"y / z",
(x, y, z) -> y / (long) z,
(x, y, z) -> y / (double) z,
(x, y, z) -> bi(y / z)
);
all(
"10000000 * x * y * 10000000 + z",
(x, y, z) -> mul(mul(mul(10000000, x), y), 10000000) + z,
(x, y, z) -> 10000000.0 * x * y * 10000000.0 + z,
(x, y, z) -> bi(10000000).multiply(bi(x)).multiply(bi(y)).multiply(bi(10000000)).add(bi(z))
);
all(
"x * y + (z - 1) / 10",
(x, y, z) -> mul(x, y) + check(z - 1) / 10L,
(x, y, z) -> x * (double) y + (z - 1.0) / 10,
(x, y, z) -> bi(x).multiply(bi(y)).add(bi(z).subtract(BigInteger.ONE).divide(BigInteger.TEN))
);
}
protected static long mul(final long a, final long b) {
return check(a * b);
}
protected static long check(final long v) {
if (v != (int) v) {
throw new RuntimeException("Overflow");
}
return v;
}
protected static BigInteger bi(final int v) {
return BigInteger.valueOf(v);
}
protected Integer i(final long x) {
return Integer.MIN_VALUE <= x && x <= Integer.MAX_VALUE ? (int) x : null;
}
protected void all(final String expression, final F<Long> fi, final F<Double> fd, final F<BigInteger> fbi) {
test(expression, "i", (x, y, z) -> (int) check(fi.apply(x, y, z)));
test(expression, "d", fd);
test(expression, "bi", fbi);
}
protected void allConst(final String expression, final Integer v) {
final BigInteger bi = bi(v);
all(expression, (x, y, z) -> (long) v, (x, y, z) -> (double) v, (x, y, z) -> bi);
}
protected void test(final String expression, final String name, final F<?> f) {
tests.add(op(name + ": " + expression, f));
}
@Override
public void test() {
for (final Op<F<?>> test : tests) {
final String[] parts = test.name.split(": ");
test(
parts[0], parts[1], test.f,
-randomInt(SIZE), randomInt(SIZE),
-randomInt(SIZE), randomInt(SIZE),
-randomInt(SIZE), randomInt(SIZE)
);
test(
parts[0], parts[1], test.f,
MAX - randomInt(SIZE), MAX,
MAX - randomInt(SIZE), MAX,
MAX - randomInt(SIZE), MAX
);
test(
parts[0], parts[1], test.f,
MIN, MIN + randomInt(SIZE),
MIN, MIN + randomInt(SIZE),
MIN, MIN + randomInt(SIZE)
);
}
}
private void test(final String mode, final String expression, final F<?> f, final int x1, final int x2, final int y1, final int y2, final int z1, final int z2) {
System.out.format("mode=%s, x=[%d, %d] y=[%d, %d] z=[%d, %d], expression=%s%n", mode, x1, x2, y1, y2, z1, z2, expression);
final Object[][][] result;
try {
result = tabulator.tabulate(mode, expression, x1, x2, y1, y2, z1, z2);
} catch (final Exception e) {
throw new AssertionError(e);
}
for (int x = x1; x <= x2; x++) {
for (int y = y1; y <= y2; y++) {
for (int z = z1; z <= z2; z++) {
counter.nextTest();
Object expected;
try {
expected = f.apply(x, y, z);
} catch (final RuntimeException e) {
expected = null;
}
final Object actual = result[x - x1][y - y1][z - z1];
assert Objects.equals(expected, actual) :
String.format("table[%d][%d][%d](x=%d, y=%d, z=%d]) = %s (expected %s)",
x - x1, y - y1, z - z1,
x, y, z,
actual, expected
);
counter.passed();
}
}
}
}
public static void main(final String[] args) {
new GenericTest().run();
}
protected interface F<T> {
T apply(int x, int y, int z);
}
}