Repository navigation
Expand file tree
/
Copy pathErrorStateHintGuards.java
More file actions
244 lines (185 loc) · 7.15 KB
/
Copy pathErrorStateHintGuards.java
File metadata and controls
244 lines (185 loc) · 7.15 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package testSuite;
import liquidjava.specification.StateRefinement;
import liquidjava.specification.StateSet;
// A boolean method whose result decides a state that the failing call needs is suggested as a check, only when the
// refinements prove that checking it allows the call
public class ErrorStateHintGuards {
// false decides the state: the check is negated
static void negated() {
GuardChannel c = GuardChannel.make();
c.send(); // Expect: State Refinement Error
}
// known state with no sequence of calls to the needed state: the check, with its parameters for the caller to fill in
static void withParameter() {
GuardLock l = new GuardLock();
l.unlock(); // Expect: State Refinement Error
}
// a true result does not decide the state
static void undecided() {
GuardPoll p = GuardPoll.make();
p.use(); // Expect: State Refinement Error
}
// the check needs a state that is just as unknown, but is allowed once the state is known
static void checkNeedsAState() {
GuardWriter w = GuardWriter.make();
w.write(); // Expect: State Refinement Error
}
static void checkAllowed() {
GuardWriter w = new GuardWriter();
w.write(); // Expect: State Refinement Error
}
// the check decides the state set it tests but resets another one the call needs
static void resetsAnotherSet() {
GuardBoth b = GuardBoth.make();
b.both(); // Expect: State Refinement Error
}
// the result decides the state only for some arguments
static void dependsOnArguments() {
GuardProbe p = GuardProbe.make();
p.fire(); // Expect: State Refinement Error
}
// in some states the refinement applied is not the one that tests the result
static void otherRefinement() {
GuardSteps m = GuardSteps.make();
m.act(); // Expect: State Refinement Error
}
// the state changes whatever the result
static void notACheck() {
GuardToken t = GuardToken.make();
t.peek(); // Expect: State Refinement Error
}
// a result that is not boolean is not suggested as a check
static void notBoolean() {
GuardReader r = new GuardReader();
r.finish(); // Expect: State Refinement Error
}
// several checks would do, so the refinements do not tell which one the call needs
static void severalChecks() {
GuardCoarse g = GuardCoarse.make();
g.run(); // Expect: State Refinement Error
}
// a call reaches the needed state on every path: the check is not suggested, as it would skip the call where it fails
static void reachableOnEveryPath(boolean shown) {
GuardFrame f = new GuardFrame();
if (shown)
f.show();
f.decorate(); // Expect: State Refinement Error
}
}
@StateSet({"open", "closed"})
class GuardChannel {
@StateRefinement(to = "open(this)")
public GuardChannel() {}
@StateRefinement(to = "return ? closed(this) : open(this)")
public boolean isClosed() { return false; }
@StateRefinement(from = "open(this)", to = "closed(this)")
public void close() {}
@StateRefinement(from = "open(this)")
public void send() {}
static GuardChannel make() { return new GuardChannel(); }
}
@StateSet({"unlocked", "locked"})
class GuardLock {
@StateRefinement(to = "unlocked(this)")
public GuardLock() {}
@StateRefinement(from = "unlocked(this)", to = "return ? locked(this) : unlocked(this)")
public boolean tryLock(long timeout) { return true; }
@StateRefinement(from = "locked(this)", to = "unlocked(this)")
public void unlock() {}
}
@StateSet({"ready", "busy"})
class GuardPoll {
@StateRefinement(to = "busy(this)")
public GuardPoll() {}
@StateRefinement(to = "return ? (ready(this) || busy(this)) : busy(this)")
public boolean poll() { return true; }
@StateRefinement(from = "ready(this)")
public void use() {}
static GuardPoll make() { return new GuardPoll(); }
}
@StateSet({"start", "accept"})
class GuardWriter {
@StateRefinement(to = "start(this)")
public GuardWriter() {}
@StateRefinement(from = "start(this)", to = "return ? accept(this) : start(this)")
public boolean canWrite() { return true; }
@StateRefinement(from = "accept(this)")
public void write() {}
static GuardWriter make() { return new GuardWriter(); }
}
@StateSet({"undoable", "notUndoable"})
@StateSet({"redoable", "notRedoable"})
class GuardBoth {
@StateRefinement(to = "notUndoable(this) && notRedoable(this)")
public GuardBoth() {}
@StateRefinement(to = "return ? redoable(this) && notUndoable(this) : notRedoable(this)")
public boolean canRedo() { return true; }
@StateRefinement(from = "redoable(this) && undoable(this)")
public void both() {}
static GuardBoth make() { return new GuardBoth(); }
}
@StateSet({"idle", "armed"})
class GuardProbe {
@StateRefinement(to = "idle(this)")
public GuardProbe() {}
@StateRefinement(to = "(n > 0 && return) ? armed(this) : idle(this)")
public boolean check(int n) { return true; }
@StateRefinement(from = "armed(this)")
public void fire() {}
static GuardProbe make() { return new GuardProbe(); }
}
@StateSet({"s0", "s1", "s2"})
class GuardSteps {
@StateRefinement(to = "s0(this)")
public GuardSteps() {}
@StateRefinement(from = "s0(this)", to = "return ? s1(this) : s0(this)")
@StateRefinement(from = "s2(this)", to = "s2(this)")
public boolean step() { return true; }
@StateRefinement(from = "s1(this)")
public void act() {}
static GuardSteps make() { return new GuardSteps(); }
}
@StateSet({"fresh", "used"})
class GuardToken {
@StateRefinement(to = "fresh(this)")
public GuardToken() {}
@StateRefinement(from = "fresh(this)", to = "used(this)")
public boolean consume() { return true; }
@StateRefinement(from = "fresh(this)")
public void peek() {}
static GuardToken make() { return new GuardToken(); }
}
@StateSet({"start", "running"})
class GuardCoarse {
@StateRefinement(to = "start(this)")
public GuardCoarse() {}
@StateRefinement(to = "return ? running(this) : true")
public boolean canRun() { return true; }
@StateRefinement(to = "return ? running(this) : true")
public boolean canStop() { return true; }
@StateRefinement(from = "running(this)")
public void run() {}
static GuardCoarse make() { return new GuardCoarse(); }
}
@StateSet({"live", "eof"})
class GuardReader {
@StateRefinement(to = "live(this)")
public GuardReader() {}
@StateRefinement(from = "live(this)", to = "return == -1 ? eof(this) : live(this)")
public int read() { return 0; }
@StateRefinement(from = "eof(this)")
public void finish() {}
}
@StateSet({"hidden", "shown"})
class GuardFrame {
@StateRefinement(to = "hidden(this)")
public GuardFrame() {}
@StateRefinement(to = "shown(this)")
public void show() {}
@StateRefinement(to = "hidden(this)")
public void dispose() {}
@StateRefinement(to = "return ? shown(this) : hidden(this)")
public boolean isShown() { return true; }
@StateRefinement(from = "hidden(this)")
public void decorate() {}
}