File: Postcondition.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (317 lines) | stat: -rw-r--r-- 9,555 bytes parent folder | download | duplicates (3)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import org.checkerframework.dataflow.qual.Pure;
import org.checkerframework.framework.qual.EnsuresQualifier;
import org.checkerframework.framework.qual.EnsuresQualifierIf;
import org.checkerframework.framework.qual.EnsuresQualifiers;
import org.checkerframework.framework.qual.EnsuresQualifiersIf;
import org.checkerframework.framework.test.*;
import testlib.util.*;

class Postcondition {

    String f1, f2, f3;
    Postcondition p;

    @Pure
    String p1() {
        return null;
    }

    /** *** normal postcondition ***** */
    @EnsuresQualifier(expression = "f1", qualifier = Odd.class)
    void oddF1() {
        f1 = null;
    }

    @EnsuresQualifier(expression = "p.f1", qualifier = Odd.class)
    void oddF1_1() {
        p.f1 = null;
    }

    @EnsuresQualifier(expression = "#1.f1", qualifier = Odd.class)
    void oddF1_2(final Postcondition param) {
        param.f1 = null;
    }

    @EnsuresQualifier(expression = "p.p1()", qualifier = Odd.class)
    void oddF1_3() {
        if (p.p1() == null) {
            return;
        }
        throw new RuntimeException();
    }

    @EnsuresQualifier(expression = "f1", qualifier = Value.class)
    // :: error: (contracts.postcondition.not.satisfied)
    void valueF1() {}

    @EnsuresQualifier(expression = "---", qualifier = Value.class)
    // :: error: (flowexpr.parse.error)
    void error() {}

    @EnsuresQualifier(expression = "#1.#2", qualifier = Value.class)
    // :: error: (flowexpr.parse.error)
    void error2(final String p1, final String p2) {}

    @EnsuresQualifier(expression = "f1", qualifier = Value.class)
    void exception() {
        throw new RuntimeException();
    }

    @EnsuresQualifier(expression = "#1", qualifier = Value.class)
    void param1(final @Value String f) {}

    @EnsuresQualifier(
            expression = {"#1", "#2"},
            qualifier = Value.class)
    // :: error: (flowexpr.parameter.not.final)
    void param2(@Value String f, @Value String g) {
        f = g;
    }

    @EnsuresQualifier(expression = "#1", qualifier = Value.class)
    // :: error: (flowexpr.parse.index.too.big)
    void param3() {}

    // basic postcondition test
    void t1(@Odd String p1, String p2) {
        valueF1();
        // :: error: (assignment.type.incompatible)
        @Odd String l1 = f1;
        oddF1();
        @Odd String l2 = f1;

        // :: error: (flowexpr.parse.error.postcondition)
        error();
    }

    // test parameter syntax
    void t2(@Odd String p1, String p2) {
        // :: error: (flowexpr.parse.index.too.big)
        param3();
    }

    // postcondition with more complex flow expression
    void tn1(boolean b) {
        // :: error: (assignment.type.incompatible)
        @Odd String l1 = p.f1;
        oddF1_1();
        @Odd String l2 = p.f1;
    }

    // postcondition with more complex flow expression
    void tn2(boolean b) {
        Postcondition param = null;
        // :: error: (assignment.type.incompatible)
        @Odd String l1 = param.f1;
        oddF1_2(param);
        @Odd String l2 = param.f1;
    }

    // postcondition with more complex flow expression
    void tn3(boolean b) {
        // :: error: (assignment.type.incompatible)
        @Odd String l1 = p.p1();
        oddF1_3();
        @Odd String l2 = p.p1();
    }

    /** *** many postcondition ***** */
    @EnsuresQualifiers({
        @EnsuresQualifier(expression = "f1", qualifier = Odd.class),
        @EnsuresQualifier(expression = "f2", qualifier = Value.class)
    })
    void oddValueF1(@Value String p1) {
        f1 = null;
        f2 = p1;
    }

    @EnsuresQualifier(expression = "f1", qualifier = Odd.class)
    @EnsuresQualifier(expression = "f2", qualifier = Value.class)
    void oddValueF1_repeated1(@Value String p1) {
        f1 = null;
        f2 = p1;
    }

    @EnsuresQualifiers({
        @EnsuresQualifier(expression = "f1", qualifier = Odd.class),
    })
    @EnsuresQualifier(expression = "f2", qualifier = Value.class)
    void oddValueF1_repeated2(@Value String p1) {
        f1 = null;
        f2 = p1;
    }

    @EnsuresQualifier(expression = "f1", qualifier = Odd.class)
    @EnsuresQualifiers({@EnsuresQualifier(expression = "f2", qualifier = Value.class)})
    void oddValueF1_repeated3(@Value String p1) {
        f1 = null;
        f2 = p1;
    }

    @EnsuresQualifiers({
        @EnsuresQualifier(expression = "f1", qualifier = Odd.class),
        @EnsuresQualifier(expression = "f2", qualifier = Value.class)
    })
    // :: error: (contracts.postcondition.not.satisfied)
    void oddValueF1_invalid(@Value String p1) {}

    @EnsuresQualifiers({
        @EnsuresQualifier(expression = "--", qualifier = Odd.class),
    })
    // :: error: (flowexpr.parse.error)
    void error2() {}

    // basic postcondition test
    void tnm1(@Odd String p1, @Value String p2) {
        // :: error: (assignment.type.incompatible)
        @Odd String l1 = f1;
        // :: error: (assignment.type.incompatible)
        @Value String l2 = f2;
        oddValueF1(p2);
        @Odd String l3 = f1;
        @Value String l4 = f2;

        // :: error: (flowexpr.parse.error.postcondition)
        error2();
    }

    /** *** conditional postcondition ***** */
    @EnsuresQualifierIf(result = true, expression = "f1", qualifier = Odd.class)
    boolean condOddF1(boolean b) {
        if (b) {
            f1 = null;
            return true;
        }
        return false;
    }

    @EnsuresQualifierIf(result = false, expression = "f1", qualifier = Odd.class)
    boolean condOddF1False(boolean b) {
        if (b) {
            return true;
        }
        f1 = null;
        return false;
    }

    @EnsuresQualifierIf(result = false, expression = "f1", qualifier = Odd.class)
    boolean condOddF1Invalid(boolean b) {
        if (b) {
            f1 = null;
            return true;
        }
        // :: error: (contracts.conditional.postcondition.not.satisfied)
        return false;
    }

    @EnsuresQualifierIf(result = false, expression = "f1", qualifier = Odd.class)
    // :: error: (contracts.conditional.postcondition.invalid.returntype)
    void wrongReturnType() {}

    @EnsuresQualifierIf(result = false, expression = "f1", qualifier = Odd.class)
    // :: error: (contracts.conditional.postcondition.invalid.returntype)
    String wrongReturnType2() {
        f1 = null;
        return "";
    }

    @EnsuresQualifierIf(result = true, expression = "#1", qualifier = Odd.class)
    boolean isOdd(final String p1) {
        return isOdd(p1, 0);
    }

    @EnsuresQualifierIf(result = true, expression = "#1", qualifier = Odd.class)
    boolean isOdd(final String p1, int p2) {
        return p1 == null;
    }

    @EnsuresQualifierIf(result = false, expression = "#1", qualifier = Odd.class)
    boolean isNotOdd(final String p1) {
        return !isOdd(p1);
    }

    // basic conditional postcondition test
    void t3(@Odd String p1, String p2) {
        condOddF1(true);
        // :: error: (assignment.type.incompatible)
        @Odd String l1 = f1;
        if (condOddF1(false)) {
            @Odd String l2 = f1;
        }
        // :: error: (assignment.type.incompatible)
        @Odd String l3 = f1;
    }

    // basic conditional postcondition test (inverted)
    void t4(@Odd String p1, String p2) {
        condOddF1False(true);
        // :: error: (assignment.type.incompatible)
        @Odd String l1 = f1;
        if (!condOddF1False(false)) {
            @Odd String l2 = f1;
        }
        // :: error: (assignment.type.incompatible)
        @Odd String l3 = f1;
    }

    // basic conditional postcondition test 2
    void t5(boolean b) {
        condOddF1(true);
        if (b) {
            // :: error: (assignment.type.incompatible)
            @Odd String l2 = f1;
        }
    }

    /** *** many conditional postcondition ***** */
    @EnsuresQualifiersIf({
        @EnsuresQualifierIf(result = true, expression = "f1", qualifier = Odd.class),
        @EnsuresQualifierIf(result = false, expression = "f1", qualifier = Value.class)
    })
    boolean condsOddF1(boolean b, @Value String p1) {
        if (b) {
            f1 = null;
            return true;
        }
        f1 = p1;
        return false;
    }

    @EnsuresQualifiersIf({
        @EnsuresQualifierIf(result = true, expression = "f1", qualifier = Odd.class),
        @EnsuresQualifierIf(result = false, expression = "f1", qualifier = Value.class)
    })
    boolean condsOddF1_invalid(boolean b, @Value String p1) {
        if (b) {
            // :: error: (contracts.conditional.postcondition.not.satisfied)
            return true;
        }
        // :: error: (contracts.conditional.postcondition.not.satisfied)
        return false;
    }

    @EnsuresQualifiersIf({
        @EnsuresQualifierIf(result = false, expression = "f1", qualifier = Odd.class)
    })
    // :: error: (contracts.conditional.postcondition.invalid.returntype)
    String wrongReturnType3() {
        return "";
    }

    void t6(@Odd String p1, @Value String p2) {
        condsOddF1(true, p2);
        // :: error: (assignment.type.incompatible)
        @Odd String l1 = f1;
        // :: error: (assignment.type.incompatible)
        @Value String l2 = f1;
        if (condsOddF1(false, p2)) {
            @Odd String l3 = f1;
            // :: error: (assignment.type.incompatible)
            @Value String l4 = f1;
        } else {
            @Value String l5 = f1;
            // :: error: (assignment.type.incompatible)
            @Odd String l6 = f1;
        }
    }
}