Repository navigation
Expand file tree
/
Copy pathCorrectLoopStateFixpoint.java
More file actions
113 lines (99 loc) · 2.97 KB
/
Copy pathCorrectLoopStateFixpoint.java
File metadata and controls
113 lines (99 loc) · 2.97 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
package testSuite;
import liquidjava.specification.StateRefinement;
import liquidjava.specification.StateSet;
// The states of an object at the start of every iteration of a loop are those before the loop and those at the end of
// any iteration, computed until they stop changing
@StateSet({"open", "marked", "closed"})
public class CorrectLoopStateFixpoint {
@StateRefinement(to = "open(this)")
CorrectLoopStateFixpoint() {}
@StateRefinement(from = "open(this)")
@StateRefinement(from = "marked(this)")
void read() {}
@StateRefinement(from = "open(this)", to = "marked(this)")
@StateRefinement(from = "marked(this)")
void mark() {}
@StateRefinement(from = "marked(this)", to = "open(this)")
void reset() {}
@StateRefinement(from = "!closed(this)", to = "closed(this)")
void close() {}
// b is open or marked in every iteration: both allow read and mark
static void openOrMarked(int n) {
CorrectLoopStateFixpoint b = new CorrectLoopStateFixpoint();
for (int i = 0; i < n; i++) {
b.read();
b.mark();
}
b.read();
b.close();
}
// the body restores the state it starts from
static void restoredInBody(int n) {
CorrectLoopStateFixpoint b = new CorrectLoopStateFixpoint();
while (n > 0) {
b.mark();
b.reset();
n--;
}
b.mark();
b.reset();
}
static void nestedLoops(int n) {
CorrectLoopStateFixpoint b = new CorrectLoopStateFixpoint();
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++)
b.mark();
b.read();
}
b.close();
}
static void doWhile(int n) {
CorrectLoopStateFixpoint b = new CorrectLoopStateFixpoint();
do {
b.mark();
n--;
} while (n > 0);
b.reset();
}
static void forEach(int[] values) {
CorrectLoopStateFixpoint b = new CorrectLoopStateFixpoint();
for (int value : values) {
b.mark();
b.read();
}
b.read();
}
// the only exit is the break, with b marked
static void exitOnlyThroughBreak(int n) {
CorrectLoopStateFixpoint b = new CorrectLoopStateFixpoint();
for (;;) {
b.mark();
if (n > 3)
break;
b.reset();
n++;
}
b.reset();
}
// a continue skips the close, which ends the loop with a return
static void continueOrClose(int n) {
CorrectLoopStateFixpoint b = new CorrectLoopStateFixpoint();
for (int i = 0; i < n; i++) {
b.read();
if (i < 5)
continue;
b.close();
return;
}
b.close();
}
@StateRefinement(from = "open(this)")
void thisOpenOrMarked(int n) {
while (n > 0) {
read();
this.mark();
n--;
}
read();
}
}