blob: 2a7995a075300c407b6b4041cf582123df0b38ae (
plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Test switch() blocks
*/
public class Main {
// TODO: This should be translated to smali tests, so it is guaranteed we have the right kind
// of switch.
// Simple packed-switch.
public static void packedSwitch(int value) {
switch (value) {
case 0:
System.out.println("0"); break;
case 1:
System.out.println("1"); break;
case 2:
System.out.println("2"); break;
case 3:
System.out.println("3"); break;
case 4:
System.out.println("4"); break;
default:
System.out.println("default"); break;
}
}
// Simple packed-switch starting at a negative index.
public static void packedSwitch2(int value) {
switch (value) {
case -3:
System.out.println("-3"); break;
case -2:
System.out.println("-2"); break;
case -1:
System.out.println("-1"); break;
case 0:
System.out.println("0"); break;
case 1:
System.out.println("1"); break;
case 2:
System.out.println("2"); break;
default:
System.out.println("default"); break;
}
}
// Simple packed-switch starting above 0.
public static void packedSwitch3(int value) {
switch (value) {
case 2:
System.out.println("2"); break;
case 3:
System.out.println("3"); break;
case 4:
System.out.println("4"); break;
case 5:
System.out.println("5"); break;
case 6:
System.out.println("6"); break;
default:
System.out.println("default"); break;
}
}
// Simple packed-switch going up to max_int.
public static void packedSwitch4(int value) {
switch (value) {
case Integer.MAX_VALUE - 1:
System.out.println(Integer.MAX_VALUE - 1); break;
case Integer.MAX_VALUE:
System.out.println(Integer.MAX_VALUE); break;
default:
System.out.println("default"); break;
}
}
// Simple packed-switch starting at min_int.
public static void packedSwitch5(int value) {
switch (value) {
case Integer.MIN_VALUE:
System.out.println(Integer.MIN_VALUE); break;
case Integer.MIN_VALUE + 1:
System.out.println(Integer.MIN_VALUE + 1); break;
default:
System.out.println("default"); break;
}
}
// Simple (packed-)switch with only min_int.
public static void packedSwitch6(int value) {
switch (value) {
case Integer.MIN_VALUE:
System.out.println(Integer.MIN_VALUE); break;
default:
System.out.println("default"); break;
}
}
// Long packed-switch that might lead to not creating chained-ifs.
public static void packedSwitch7(int value) {
switch (value) {
case 1:
System.out.println(1); break;
case 2:
System.out.println(2); break;
case 3:
System.out.println(3); break;
case 4:
System.out.println(4); break;
case 5:
System.out.println(5); break;
case 6:
System.out.println(6); break;
case 7:
System.out.println(7); break;
case 8:
System.out.println(8); break;
case 9:
System.out.println(9); break;
case 10:
System.out.println(10); break;
case 11:
System.out.println(11); break;
case 12:
System.out.println(12); break;
case 13:
System.out.println(13); break;
case 14:
System.out.println(14); break;
case 15:
System.out.println(15); break;
default:
System.out.println("default"); break;
}
}
// Sparse switch, just leave a gap.
public static void sparseSwitch(int value) {
switch (value) {
case 0:
System.out.println("0"); break;
case 1:
System.out.println("1"); break;
case 3:
System.out.println("3"); break;
case 4:
System.out.println("4"); break;
default:
System.out.println("default"); break;
}
}
// Simple sparse-switch starting at a negative index.
public static void sparseSwitch2(int value) {
switch (value) {
case -3:
System.out.println("-3"); break;
case -2:
System.out.println("-2"); break;
case -1:
System.out.println("-1"); break;
case 0:
System.out.println("0"); break;
case 2:
System.out.println("2"); break;
default:
System.out.println("default"); break;
}
}
// Simple sparse-switch starting above 0.
public static void sparseSwitch3(int value) {
switch (value) {
case 2:
System.out.println("2"); break;
case 4:
System.out.println("4"); break;
case 5:
System.out.println("5"); break;
case 6:
System.out.println("6"); break;
default:
System.out.println("default"); break;
}
}
// Simple sparse-switch going up to max_int.
public static void sparseSwitch4(int value) {
switch (value) {
case Integer.MAX_VALUE - 2:
System.out.println(Integer.MAX_VALUE - 2); break;
case Integer.MAX_VALUE:
System.out.println(Integer.MAX_VALUE); break;
default:
System.out.println("default"); break;
}
}
// Simple sparse-switch starting at min_int.
public static void sparseSwitch5(int value) {
switch (value) {
case Integer.MIN_VALUE:
System.out.println(Integer.MIN_VALUE); break;
case Integer.MIN_VALUE + 2:
System.out.println(Integer.MIN_VALUE + 2); break;
default:
System.out.println("default"); break;
}
}
// Long sparse-switch that might lead to not creating chained-ifs.
public static void sparseSwitch7(int value) {
switch (value) {
case 1:
System.out.println(1); break;
case 2:
System.out.println(2); break;
case 4:
System.out.println(4); break;
case 5:
System.out.println(5); break;
case 6:
System.out.println(6); break;
case 7:
System.out.println(7); break;
case 8:
System.out.println(8); break;
case 9:
System.out.println(9); break;
case 10:
System.out.println(10); break;
case 11:
System.out.println(11); break;
case 12:
System.out.println(12); break;
case 13:
System.out.println(13); break;
case 14:
System.out.println(14); break;
case 15:
System.out.println(15); break;
default:
System.out.println("default"); break;
}
}
public static void main(String args[]) {
/*
* Note: We are using for loops and calls to hopefully avoid simplifying the switch
* structure from constant propagation. When inlining is supported, this needs to
* be revisited.
*/
System.out.println("packed");
for (int i = -2; i < 3; i++) {
packedSwitch(i);
}
packedSwitch(Integer.MIN_VALUE);
packedSwitch(Integer.MAX_VALUE);
System.out.println("packed2");
for (int i = -2; i < 3; i++) {
packedSwitch2(i);
}
packedSwitch2(Integer.MIN_VALUE);
packedSwitch2(Integer.MAX_VALUE);
System.out.println("packed3");
for (int i = -2; i < 7; i++) {
packedSwitch3(i);
}
packedSwitch3(Integer.MIN_VALUE);
packedSwitch3(Integer.MAX_VALUE);
System.out.println("packed4");
for (int i = Integer.MAX_VALUE - 2; i > 0; i++) {
packedSwitch4(i);
}
packedSwitch4(Integer.MIN_VALUE);
System.out.println("packed5");
for (int i = Integer.MIN_VALUE; i < Integer.MIN_VALUE + 2; i++) {
packedSwitch5(i);
}
packedSwitch5(Integer.MAX_VALUE);
System.out.println("packed6");
packedSwitch6(Integer.MIN_VALUE);
packedSwitch6(Integer.MAX_VALUE);
System.out.println("packed7");
for (int i = -1; i < 17; i++) {
packedSwitch7(i);
}
System.out.println("sparse");
for (int i = -2; i < 4; i++) {
sparseSwitch(i);
}
sparseSwitch(Integer.MIN_VALUE);
sparseSwitch(Integer.MAX_VALUE);
System.out.println("sparse2");
for (int i = -2; i < 3; i++) {
sparseSwitch2(i);
}
sparseSwitch2(Integer.MIN_VALUE);
sparseSwitch2(Integer.MAX_VALUE);
System.out.println("sparse3");
for (int i = -2; i < 7; i++) {
sparseSwitch3(i);
}
sparseSwitch3(Integer.MIN_VALUE);
sparseSwitch3(Integer.MAX_VALUE);
System.out.println("sparse4");
for (int i = Integer.MAX_VALUE - 2; i > 0; i++) {
sparseSwitch4(i);
}
sparseSwitch4(Integer.MIN_VALUE);
System.out.println("sparse5");
for (int i = Integer.MIN_VALUE; i < Integer.MIN_VALUE + 2; i++) {
sparseSwitch5(i);
}
sparseSwitch5(Integer.MAX_VALUE);
System.out.println("sparse7");
for (int i = -1; i < 17; i++) {
sparseSwitch7(i);
}
// Older tests.
int a = 1;
switch (a) {
case -1: System.out.print("neg one\n"); break;
case 0: System.out.print("zero\n"); break;
case 1: System.out.print("CORRECT (one)\n"); break;
case 2: System.out.print("two\n"); break;
case 3: System.out.print("three\n"); break;
case 4: System.out.print("four\n"); break;
default: System.out.print("???\n"); break;
}
switch (a) {
case 3: System.out.print("three\n"); break;
case 4: System.out.print("four\n"); break;
default: System.out.print("CORRECT (not found)\n"); break;
}
a = 0x12345678;
switch (a) {
case 0x12345678: System.out.print("CORRECT (large)\n"); break;
case 0x12345679: System.out.print("large+1\n"); break;
default: System.out.print("nuts\n"); break;
}
switch (a) {
case 0x12345678: System.out.print("CORRECT (large2)\n"); break;
case 0x12345700: System.out.print("large+many\n"); break;
default: System.out.print("nuts\n"); break;
}
switch (a) {
case 57: System.out.print("fifty-seven!\n"); break;
case -6: System.out.print("neg six!\n"); break;
case 0x12345678: System.out.print("CORRECT (large3)\n"); break;
case 22: System.out.print("twenty-two!\n"); break;
case 3: System.out.print("three!\n"); break;
default: System.out.print("huh?\n"); break;
}
switch (a) {
case -6: System.out.print("neg six!\n"); break;
case 3: System.out.print("three!\n"); break;
default: System.out.print("CORRECT (not found)\n"); break;
}
a = -5;
switch (a) {
case 12: System.out.print("twelve\n"); break;
case -5: System.out.print("CORRECT (not found)\n"); break;
case 0: System.out.print("zero\n"); break;
default: System.out.print("wah?\n"); break;
}
switch (a) {
default: System.out.print("CORRECT (default only)\n"); break;
}
a = -10;
switch (a) {
case -10: System.out.print("CORRECT big sparse / first\n"); break;
case -5: System.out.print("neg five\n"); break;
case 0: System.out.print("zero\n"); break;
case 5: System.out.print("five\n"); break;
case 10: System.out.print("ten\n"); break;
case 15: System.out.print("fifteen\n"); break;
case 20: System.out.print("twenty\n"); break;
case 50: System.out.print("fifty\n"); break;
case 100: System.out.print("hundred\n"); break;
default: System.out.print("blah!\n"); break;
}
a = 100;
switch (a) {
case -10: System.out.print("neg ten\n"); break;
case -5: System.out.print("neg five\n"); break;
case 0: System.out.print("zero\n"); break;
case 5: System.out.print("five\n"); break;
case 10: System.out.print("ten\n"); break;
case 15: System.out.print("fifteen\n"); break;
case 20: System.out.print("twenty\n"); break;
case 50: System.out.print("fifty\n"); break;
case 100: System.out.print("CORRECT big sparse / last\n"); break;
default: System.out.print("blah!\n"); break;
}
for (a = 253; a <= 258; a++) {
switch (a) {
case 254: System.out.println("254"); break;
case 255: System.out.println("255"); break;
case 256: System.out.println("256"); break;
case 257: System.out.println("257"); break;
default: System.out.println("default"); break;
}
}
}
}
|