-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathCorrectSimpleIfElse.java
More file actions
49 lines (42 loc) · 1.44 KB
/
Copy pathCorrectSimpleIfElse.java
File metadata and controls
49 lines (42 loc) · 1.44 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
package testSuite;
import liquidjava.specification.Refinement;
@SuppressWarnings("unused")
public class CorrectSimpleIfElse {
// -Integer.MIN_VALUE overflows back to Integer.MIN_VALUE (still negative), so the negation is only
// provably positive away from that boundary. We bound the input to a small negative window so the
// result is a small positive value (also keeping the caller's `toPositive(ex_a) * 10` overflow-free).
@Refinement("_ > 0 && _ < 1000")
public static int toPositive(@Refinement("a < 0 && a > -1000") int a) {
return -a;
}
@Refinement("_ < 0")
public static int toNegative(@Refinement("a > 0") int a) {
return -a;
}
public static void main(String[] args) {
@Refinement("_ < 10")
int a = 5;
if (a < 0) {
@Refinement("b < 0")
int b = a;
} else {
@Refinement("b >= 0")
int b = a;
}
// EXAMPLE 2
// Bound ex_a to the small negative window toPositive accepts (and keep the *10 below in range).
@Refinement("_ < 10 && _ > -1000")
int ex_a = 5;
if (ex_a < 0) {
@Refinement("_ >= 10")
int ex_b = toPositive(ex_a) * 10;
} else {
if (ex_a != 0) {
@Refinement("_ < 0")
int ex_d = toNegative(ex_a);
}
@Refinement("_ < ex_a")
int ex_c = -10;
}
}
}