-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathVCFoldingTest.java
More file actions
185 lines (148 loc) · 8.36 KB
/
Copy pathVCFoldingTest.java
File metadata and controls
185 lines (148 loc) · 8.36 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package liquidjava.rj_language.opt;
import static liquidjava.utils.VCTestUtils.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import liquidjava.processor.SimplifiedVCImplication;
import liquidjava.processor.VCImplication;
import liquidjava.rj_language.Predicate;
import liquidjava.rj_language.ast.BinaryExpression;
import liquidjava.rj_language.ast.Enum;
import liquidjava.rj_language.ast.GroupExpression;
import liquidjava.rj_language.ast.LiteralInt;
import org.junit.jupiter.api.Test;
class VCFoldingTest {
private final VCFolding folding = new VCFolding();
@Test
void foldsIntegerArithmeticAndComparisons() {
VCImplication implication = vc("1 + 2 == 3");
assertSimplificationSteps(folding::apply, implication, chain(expect("3 == 3", "1 + 2 == 3")),
chain(expect("true", "3 == 3")));
assertSimplificationSteps(folding::apply, vc("4 > 7"), chain(expect("false", "4 > 7")));
}
@Test
void foldsRealAndMixedNumericExpressions() {
VCImplication realArithmetic = vc("1.5 + 2.0 == 3.5");
VCImplication mixedArithmetic = vc("2 + 0.5 > 2");
assertSimplificationSteps(folding::apply, realArithmetic, chain(expect("3.5 == 3.5", "1.5 + 2.0 == 3.5")),
chain(expect("true", "3.5 == 3.5")));
assertSimplificationSteps(folding::apply, mixedArithmetic, chain(expect("2.5 > 2", "2 + 0.5 > 2")),
chain(expect("true", "2.5 > 2")));
}
@Test
void leavesDivisionAndModuloByZeroUnchanged() {
assertSimplificationSteps(folding::apply, vc("4 / 0 == 0"), chain(expect("4 / 0 == 0")));
assertSimplificationSteps(folding::apply, vc("4 % 0 == 0"), chain(expect("4 % 0 == 0")));
}
@Test
void leavesRealDivisionAndModuloByZeroUnchanged() {
assertSimplificationSteps(folding::apply, vc("4.0 / 0.0 == 0.0"), chain(expect("4.0 / 0.0 == 0.0")));
assertSimplificationSteps(folding::apply, vc("4.0 % 0.0 == 0.0"), chain(expect("4.0 % 0.0 == 0.0")));
}
@Test
void foldsIntegerDivisionTowardZeroForNegativeResults() {
VCImplication implication = vc("(2 - 7) / 2 == -2");
assertSimplificationSteps(folding::apply, implication, chain(expect("(2 - 7) / 2 == -2", "(2 - 7) / 2 == -2")),
chain(expect("-5 / 2 == -2", "(2 - 7) / 2 == -2")), chain(expect("-2 == -2", "-5 / 2 == -2")),
chain(expect("-2 == -2", "-2 == -2")), chain(expect("true", "-2 == -2")));
}
@Test
void foldsIntegerModuloWithJavaSignedRemainder() {
VCImplication negativeDividend = vc("-5 % 2 < 0");
VCImplication negativeDivisor = vc("5 % -2 > 0");
assertSimplificationSteps(folding::apply, negativeDividend, chain(expect("-5 % 2 < 0", "-5 % 2 < 0")),
chain(expect("-1 < 0", "-5 % 2 < 0")), chain(expect("true", "-1 < 0")));
assertSimplificationSteps(folding::apply, negativeDivisor, chain(expect("5 % -2 > 0", "5 % -2 > 0")),
chain(expect("1 > 0", "5 % -2 > 0")), chain(expect("true", "1 > 0")));
}
@Test
void foldsBooleanBinaryExpressions() {
assertSimplificationSteps(folding::apply, vc("true && false"), chain(expect("false", "true && false")));
assertSimplificationSteps(folding::apply, vc("false --> true"), chain(expect("true", "false --> true")));
assertSimplificationSteps(folding::apply, vc("true != false"), chain(expect("true", "true != false")));
}
@Test
void foldsBooleanSubexpressionsInsideLargerExpression() {
assertSimplificationSteps(folding::apply, vc("true && false || ok"),
chain(expect("false || ok", "true && false || ok")));
}
@Test
void foldsNestedConstantsInsideLargerExpression() {
assertSimplificationSteps(folding::apply, vc("x > 1 + 2"), chain(expect("x > 3", "x > 1 + 2")));
assertSimplificationSteps(folding::apply, vc("x + 1 + 2 > 4"), chain(expect("x + 3 > 4", "x + 1 + 2 > 4")));
}
@Test
void foldsPartialComparisonsWithoutDroppingSymbolicTerms() {
assertSimplificationSteps(folding::apply, vc("1 + 2 < x + 4"), chain(expect("3 < x + 4", "1 + 2 < x + 4")));
}
@Test
void foldsUnaryExpressions() {
assertSimplificationSteps(folding::apply, vc("!true"), chain(expect("false", "!true")));
VCImplication implication = vc("-3 < 0");
assertSimplificationSteps(folding::apply, implication, chain(expect("-3 < 0", "-3 < 0")),
chain(expect("true", "-3 < 0")));
}
@Test
void foldsIteExpressions() {
assertSimplificationSteps(folding::apply, vc("true ? a : b"), chain(expect("a", "true ? a : b")));
assertSimplificationSteps(folding::apply, vc("false ? a : b"), chain(expect("b", "false ? a : b")));
assertSimplificationSteps(folding::apply, vc("cond ? b : b"), chain(expect("b", "cond ? b : b")));
}
@Test
void foldsIteBranchesBeforeComparingThem() {
VCImplication implication = vc("cond ? 1 + 2 : 3");
assertSimplificationSteps(folding::apply, implication, chain(expect("cond ? 3 : 3", "cond ? 1 + 2 : 3")),
chain(expect("3", "cond ? 3 : 3")));
}
@Test
void foldsAdjacentIntegerConstants() {
assertSimplificationSteps(folding::apply, vc("x + 1 - 2"), chain(expect("x - 1", "x + 1 - 2")));
assertSimplificationSteps(folding::apply, vc("x - 1 + 2"), chain(expect("x + 1", "x - 1 + 2")));
assertSimplificationSteps(folding::apply, vc("x + 1 + 2"), chain(expect("x + 3", "x + 1 + 2")));
assertSimplificationSteps(folding::apply, vc("x + 1 - 1"), chain(expect("x", "x + 1 - 1")));
}
@Test
void foldsEnumEqualityAndInequality() {
assertSimplificationSteps(folding::apply, vc("Mode.Photo == Mode.Photo"),
chain(expect("true", "Mode.Photo == Mode.Photo")));
assertSimplificationSteps(folding::apply, vc("Mode.Photo != Mode.Video"),
chain(expect("true", "Mode.Photo != Mode.Video")));
}
@Test
void foldsResolvedEnumLiterals() {
Enum limit = new Enum("Config", "LIMIT");
limit.setResolvedLiteral(new LiteralInt(3));
VCImplication implication = new VCImplication(
new Predicate(new BinaryExpression(limit, "==", new LiteralInt(3))));
assertSimplificationSteps(folding::apply, implication, chain(expect("3 == 3", "Config.LIMIT == 3")),
chain(expect("true", "3 == 3")));
}
@Test
void foldsResolvedEnumLiteralsInsideLargerExpression() {
Enum limit = new Enum("Config", "LIMIT");
limit.setResolvedLiteral(new LiteralInt(3));
BinaryExpression arithmetic = new BinaryExpression(limit, "+", new LiteralInt(2));
VCImplication implication = new VCImplication(
new Predicate(new BinaryExpression(arithmetic, "==", new LiteralInt(5))));
assertSimplificationSteps(folding::apply, implication, chain(expect("3 + 2 == 5", "Config.LIMIT + 2 == 5")),
chain(expect("5 == 5", "3 + 2 == 5")), chain(expect("true", "5 == 5")));
}
@Test
void recordsOriginWhenOnlyGroupIsUnwrapped() {
VCImplication implication = vc("(x > 0)");
VCImplication result = assertSimplificationSteps(folding::apply, implication, chain(expect("x > 0", "x > 0")));
SimplifiedVCImplication simplified = assertInstanceOf(SimplifiedVCImplication.class, result);
assertEquals("x > 0", simplified.getRefinement().toString());
assertInstanceOf(GroupExpression.class, simplified.getOrigin().getRefinement().getExpression());
}
@Test
void recordsOriginWhenFoldingLaterImplication() {
VCImplication implication = vc("x > 0", "1 + 2 > 0");
VCImplication result = assertSimplificationSteps(folding::apply, implication,
chain(expect("x > 0"), expect("3 > 0", "1 + 2 > 0")));
SimplifiedVCImplication simplifiedNext = assertInstanceOf(SimplifiedVCImplication.class, result.getNext());
assertEquals("1 + 2 > 0", simplifiedNext.getOrigin().getRefinement().getExpression().toDisplayString());
result = assertSimplificationSteps(folding::apply, result, chain(expect("x > 0"), expect("true", "3 > 0")));
simplifiedNext = assertInstanceOf(SimplifiedVCImplication.class, result.getNext());
assertEquals("3 > 0", simplifiedNext.getOrigin().getRefinement().getExpression().toDisplayString());
}
}