-
Notifications
You must be signed in to change notification settings - Fork 73
/
ExpressionTest.java
88 lines (77 loc) · 2.62 KB
/
ExpressionTest.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
package net.sourceforge.kolmafia;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import net.sourceforge.kolmafia.preferences.Preferences;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
public class ExpressionTest {
@BeforeEach
public void beforeEach() {
KoLCharacter.reset("Expression");
Preferences.reset("Expression");
}
@ParameterizedTest
@CsvSource({"1+1, 2", "70-1, 69", "4*2, 8", "30/5, 6", "41%10, 1", "3^3, 27"})
public void canDoBasicArithmetic(String input, String expected) {
var exp = new Expression(input, input);
assertEquals(Double.parseDouble(expected), exp.eval());
}
@ParameterizedTest
@ValueSource(strings = {"1/0", "-4^0.5", "999^999", "sqrt(-1)"})
public void invalidArithmeticReturnsZero(String invalidExpr) {
var exp = new Expression(invalidExpr, invalidExpr);
assertEquals(0.0, exp.eval());
}
@Test
public void invalidExpressionReturnsZero() {
var exp = new Expression("@", "Invalid bytecode");
assertEquals(0.0, exp.eval());
}
@ParameterizedTest
@CsvSource({
"abs(-11), 11",
"ceil(9.1), 10",
"floor(5.6), 5",
"'min(1,2)', 1",
"'max(1,2)', 2",
"sqrt(25), 5",
})
public void canDoSupportedMathFunctions(String input, String expected) {
var exp = new Expression(input, input);
assertEquals(Double.parseDouble(expected), exp.eval());
}
@ParameterizedTest
@CsvSource({
"true, 1",
"false, 0",
"45, 45",
})
public void canReadPrefs(String prefValue, String expected) {
Preferences.setString("test", prefValue);
var exp = new Expression("pref(test)", "pref(test) where pref = " + prefValue);
assertEquals(Double.parseDouble(expected), exp.eval());
}
@ParameterizedTest
@CsvSource({
"abc, 1", "xyz, 0",
})
public void canComparePrefs(String prefValue, String expected) {
Preferences.setString("test", prefValue);
var exp = new Expression("pref(test,abc)", "pref(test,abc) where pref = " + prefValue);
assertEquals(Double.parseDouble(expected), exp.eval());
}
@Test
public void canTrackBiggerInts() {
var exp = new Expression("32769", "integer bigger than 32768");
assertEquals(32769.0, exp.eval());
}
@Test
void canReportMultipleErrors() {
var exp = new Expression("1+(4*path(The Source))", "nonexistent function");
assertThat(exp.hasErrors(), equalTo(true));
}
}