File: Switch.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 (193 lines) | stat: -rw-r--r-- 5,530 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
import org.checkerframework.common.value.qual.*;

// Test case for switch statements. Not really about the value checker (more about
// whether the semantics of switch are correct in general), but I needed some
// checker to try it out on.
class Switch {
    void test1(@IntVal({1, 2, 3, 4, 5}) int x) {

        // easy version, no fall through
        switch (x) {
            case 1:
                @IntVal({1}) int y = x;
                break;
            case 2:
                @IntVal({2}) int w = x;
                // :: error: (assignment.type.incompatible)
                @IntVal({1}) int z = x;
                break;
            default:
                @IntVal({3, 4, 5}) int q = x;
                break;
        }
    }

    void test2(@IntVal({1, 2, 3, 4, 5}) int x) {

        // harder version, fall through
        switch (x) {
            case 1:
                @IntVal({1}) int y = x;
            case 2:
            case 3:
                @IntVal({1, 2, 3}) int w = x;
                // :: error: (assignment.type.incompatible)
                @IntVal({2, 3}) int z = x;
                // :: error: (assignment.type.incompatible)
                @IntVal({3}) int z1 = x;
                break;
            default:
                @IntVal({4, 5}) int q = x;

                // :: error: (assignment.type.incompatible)
                @IntVal(5) int q2 = x;
                break;
        }
    }

    void test3(@IntVal({1, 2, 3, 4, 5}) int x) {

        // harder version, fall through
        switch (x) {
            case 1:
                @IntVal({1}) int y = x;
            case 2:
            case 3:
                @IntVal({1, 2, 3}) int w = x;
                // :: error: (assignment.type.incompatible)
                @IntVal({2, 3}) int z = x;
                // :: error: (assignment.type.incompatible)
                @IntVal({3}) int z1 = x;
                break;
            case 4:
            default:
                @IntVal({4, 5}) int q = x;

                // :: error: (assignment.type.incompatible)
                @IntVal(5) int q2 = x;
                break;
        }
    }

    void test4(int x) {
        switch (x) {
            case 1:
                @IntVal({1}) int y = x;
                break;
            case 2:
            case 3:
                @IntVal({2, 3}) int z = x;
                break;
            case 4:
            default:
                return;
        }
        @IntVal({1, 2, 3}) int y = x;
        // :: error: (assignment.type.incompatible)
        @IntVal(4) int y2 = x;
    }

    void test5(@IntVal({0, 1, 2, 3, 4}) int x) {
        @IntVal({0, 1, 2, 3, 4, 5}) int y = x;
        switch (y = y + 1) {
            case 1:
                @IntVal({1}) int a = y;
                // :: error: (assignment.type.incompatible)
                @IntVal({2}) int b = y;
            case 2:
            case 3:
                @IntVal({1, 2, 3}) int c = y;
                break;
            default:
                // :: error: (assignment.type.incompatible)
                @IntVal({4}) int d = y;
                // :: error: (assignment.type.incompatible)
                @IntVal({5}) int e = y;
                @IntVal({4, 5}) int f = y;
                break;
        }
    }

    void testInts1(@IntRange(from = 0, to = 100) int x) {
        switch (x) {
            case 0:
            case 1:
            case 2:
                @IntVal({0, 1, 2}) int z = x;
                return;
            default:
        }

        @IntRange(from = 3, to = 100) int z = x;
    }

    void testInts2(@IntRange(from = 0, to = 100) int x) {

        // harder version, fall through
        switch (x) {
            case 0:
                @IntVal(0) int a = x;
                break;
            case 1:
                @IntVal(1) int b = x;
                break;
            case 2:
                @IntVal(2) int c = x;
            default:
                @IntRange(from = 2, to = 100) int d = x;
                break;
        }
    }

    void testChars(char x) {
        switch (x) {
            case 'a':
            case 2:
                @IntVal({'a', 2}) int z = x;
                break;
            case 'b':
                @IntVal('b') int v = x;
                break;
            default:
                return;
        }
        @IntVal({'a', 2, 'b'}) int y = x;
    }

    void testStrings1(String s) {
        switch (s) {
            case "Good":
                @StringVal("Good") String x = s;
            case "Bye":
                @StringVal({"Good", "Bye"}) String y = s;
                break;
            case "Hello":
                @StringVal("Hello") String z = s;
                break;
            default:
                return;
        }
        @StringVal({"Good", "Bye", "Hello"}) String q = s;
    }

    void testStrings2(String s) {
        String a;
        switch (a = s) {
            case "Good":
                @StringVal("Good") String x1 = a;
                @StringVal("Good") String x2 = s;
            case "Bye":
                @StringVal({"Good", "Bye"}) String y1 = a;
                @StringVal({"Good", "Bye"}) String y2 = s;
                break;
            case "Hello":
                @StringVal("Hello") String z1 = a;
                @StringVal("Hello") String z2 = s;
                break;
            default:
                return;
        }
        @StringVal({"Good", "Bye", "Hello"}) String q1 = a;
        @StringVal({"Good", "Bye", "Hello"}) String q2 = s;
    }
}