Skip to content

Commit 15953bf

Browse files
committed
java: inline range test
1 parent c8c15a0 commit 15953bf

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
public class B {
2+
public int forloop() {
3+
int result = 0;
4+
for (int i = 0; i < 10; i++) {// $ bound="i in [0..10]" bound="i in [0..9]"
5+
result = i; // $ bound="i in [0..9]"
6+
}
7+
return result; // $ bound="result in [0..9]"
8+
}
9+
10+
public int forloopexit() {
11+
int result = 0;
12+
for (; result < 10;) { // $ bound="result in [0..10]"
13+
result += 1; // $ bound="result in [0..9]"
14+
}
15+
return result; // $ bound="result = 10"
16+
}
17+
18+
public int forloopexitstep() {
19+
int result = 0;
20+
for (; result < 10;) { // $ bound="result in [0..12]"
21+
result += 3; // $ bound="result in [0..9]"
22+
}
23+
return result; // $ bound="result = 12"
24+
}
25+
26+
public int forloopexitupd() {
27+
int result = 0;
28+
for (; result < 10; result++) { // $ bound="result in [0..9]" bound="result in [0..10]"
29+
}
30+
return result; // $ bound="result = 10"
31+
}
32+
33+
public int forloopexitnested() {
34+
int result = 0;
35+
for (; result < 10;) {
36+
int i = 0;
37+
for (; i < 3;) { // $ bound="i in [0..3]"
38+
i += 1; // $ bound="i in [0..2]"
39+
}
40+
result += i; // $ bound="result in [0..9]" bound="i = 3"
41+
}
42+
return result; // $ MISSING:bound="result = 12"
43+
}
44+
45+
public int emptyforloop() {
46+
int result = 0;
47+
for (int i = 0; i < 0; i++) { // $ bound="i = 0" bound="i in [0..-1]"
48+
result = i; // $ bound="i in [0..-1]"
49+
}
50+
return result; // $ bound="result = 0"
51+
}
52+
53+
public int noloop() {
54+
int result = 0;
55+
result += 1; // $ bound="result = 0"
56+
return result; // $ bound="result = 1"
57+
}
58+
59+
public int foreachloop() {
60+
int result = 0;
61+
for (int i : new int[] {1, 2, 3, 4, 5}) {
62+
result = i;
63+
}
64+
return result;
65+
}
66+
67+
public int emptyforeachloop() {
68+
int result = 0;
69+
for (int i : new int[] {}) {
70+
result = i;
71+
}
72+
return result;
73+
}
74+
75+
public int whileloop() {
76+
int result = 100;
77+
while (result > 5) { // $ bound="result in [4..100]"
78+
result = result - 2; // $ bound="result in [6..100]"
79+
}
80+
return result; // $ bound="result = 4"
81+
}
82+
83+
public int oddwhileloop() {
84+
int result = 101;
85+
while (result > 5) { // $ bound="result in [5..101]"
86+
result = result - 2; // $ bound="result in [7..101]"
87+
}
88+
return result; // $ bound="result = 5"
89+
}
90+
}

java/ql/test/library-tests/dataflow/range-analysis-inline/range.expected

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Inline range analysis tests for Java.
3+
* See `shared/util/codeql/dataflow/test/InlineFlowTest.qll`
4+
*/
5+
6+
import java
7+
import semmle.code.java.dataflow.RangeAnalysis
8+
private import codeql.util.test.InlineExpectationsTest
9+
private import TestUtilities.internal.InlineExpectationsTestImpl
10+
private import Make<Impl> as IET
11+
12+
module RangeTest implements IET::TestSig {
13+
string getARelevantTag() { result = "bound" }
14+
15+
predicate hasActualResult(Location location, string element, string tag, string value) {
16+
tag = "bound" and
17+
exists(Expr e, int lower, int upper |
18+
constrained(e, lower, upper) and
19+
e instanceof VarRead and
20+
e.getCompilationUnit().fromSource()
21+
|
22+
location = e.getLocation() and
23+
element = e.toString() and
24+
if lower = upper
25+
then value = "\"" + e.toString() + " = " + lower.toString() + "\""
26+
else
27+
value = "\"" + e.toString() + " in [" + lower.toString() + ".." + upper.toString() + "]\""
28+
)
29+
}
30+
31+
private predicate constrained(Expr e, int lower, int upper) {
32+
lower = min(int delta | bounded(e, any(ZeroBound z), delta, false, _)) and
33+
upper = min(int delta | bounded(e, any(ZeroBound z), delta, true, _))
34+
}
35+
}
36+
37+
import IET::MakeTest<RangeTest>

0 commit comments

Comments
 (0)