forked from jacoco/jacoco
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExhaustiveSwitchFilterTest.java
314 lines (270 loc) · 8.73 KB
/
ExhaustiveSwitchFilterTest.java
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
/*******************************************************************************
* Copyright (c) 2009, 2023 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Evgeny Mandrikov - initial API and implementation
*
*******************************************************************************/
package org.jacoco.core.internal.analysis.filter;
import org.jacoco.core.internal.instr.InstrSupport;
import org.junit.Test;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodNode;
import java.util.HashSet;
import java.util.Set;
/**
* Unit tests for {@link ExhaustiveSwitchFilter}.
*/
public class ExhaustiveSwitchFilterTest extends FilterTestBase {
private final IFilter filter = new ExhaustiveSwitchFilter();
/**
* <pre>
* enum E {
* A, B, C
* }
*
* int example(E e) {
* return switch (e) {
* case A -> 1;
* case B -> 2;
* case C -> 3;
* };
* }
* </pre>
*/
@Test
public void should_filter_when_default_branch_has_LineNumber_of_switch() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"Example", "()I", null, null);
final Label start = new Label();
final Label end = new Label();
m.visitLabel(start);
m.visitLineNumber(0, start);
m.visitFieldInsn(Opcodes.GETSTATIC, "Example$1", "$SwitchMap$Example$E",
"[I");
m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Example$E", "ordinal", "()I",
false);
m.visitInsn(Opcodes.IALOAD);
final Label dflt = new Label();
final Label case1 = new Label();
final Label case2 = new Label();
final Label case3 = new Label();
m.visitLookupSwitchInsn(dflt, new int[] { 1, 2, 3 },
new Label[] { case1, case2, case3 });
final AbstractInsnNode switchNode = m.instructions.getLast();
final Set<AbstractInsnNode> newTargets = new HashSet<AbstractInsnNode>();
m.visitLabel(dflt);
final Range range = new Range();
range.fromInclusive = m.instructions.getLast();
m.visitLineNumber(0, dflt);
m.visitTypeInsn(Opcodes.NEW, "java/lang/IncompatibleClassChangeError");
m.visitInsn(Opcodes.DUP);
m.visitMethodInsn(Opcodes.INVOKESPECIAL,
"java/lang/IncompatibleClassChangeError", "<init>", "()V",
false);
m.visitInsn(Opcodes.ATHROW);
range.toInclusive = m.instructions.getLast();
m.visitLabel(case1);
m.visitInsn(Opcodes.ICONST_1);
newTargets.add(m.instructions.getLast());
m.visitJumpInsn(Opcodes.GOTO, end);
m.visitLabel(case2);
m.visitInsn(Opcodes.ICONST_2);
newTargets.add(m.instructions.getLast());
m.visitLabel(case3);
m.visitInsn(Opcodes.ICONST_3);
newTargets.add(m.instructions.getLast());
m.visitLabel(end);
m.visitInsn(Opcodes.IRETURN);
filter.filter(m, context, output);
assertIgnored(range);
assertReplacedBranches(switchNode, newTargets);
}
/**
* <pre>
* enum E {
* A, B, C
* }
*
* int example(E e) {
* return switch (e) {
* case A -> 1;
* case B -> 2;
* case C -> 3;
* };
* }
* </pre>
*/
@Test
public void should_filter_when_default_branch_has_no_LineNumber() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"Example", "()I", null, null);
final Label start = new Label();
final Label end = new Label();
m.visitLabel(start);
m.visitLineNumber(0, start);
m.visitFieldInsn(Opcodes.GETSTATIC, "Example$1", "$SwitchMap$Example$E",
"[I");
m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Example$E", "ordinal", "()I",
false);
m.visitInsn(Opcodes.IALOAD);
final Label dflt = new Label();
final Label case1 = new Label();
final Label case2 = new Label();
final Label case3 = new Label();
m.visitLookupSwitchInsn(dflt, new int[] { 1, 2, 3 },
new Label[] { case1, case2, case3 });
final AbstractInsnNode switchNode = m.instructions.getLast();
final Set<AbstractInsnNode> newTargets = new HashSet<AbstractInsnNode>();
m.visitLabel(dflt);
final Range range = new Range();
range.fromInclusive = m.instructions.getLast();
m.visitTypeInsn(Opcodes.NEW, "java/lang/IncompatibleClassChangeError");
m.visitInsn(Opcodes.DUP);
m.visitMethodInsn(Opcodes.INVOKESPECIAL,
"java/lang/IncompatibleClassChangeError", "<init>", "()V",
false);
m.visitInsn(Opcodes.ATHROW);
range.toInclusive = m.instructions.getLast();
m.visitLabel(case1);
m.visitInsn(Opcodes.ICONST_1);
newTargets.add(m.instructions.getLast());
m.visitJumpInsn(Opcodes.GOTO, end);
m.visitLabel(case2);
m.visitInsn(Opcodes.ICONST_2);
newTargets.add(m.instructions.getLast());
m.visitLabel(case3);
m.visitInsn(Opcodes.ICONST_3);
newTargets.add(m.instructions.getLast());
m.visitLabel(end);
m.visitInsn(Opcodes.IRETURN);
filter.filter(m, context, output);
assertIgnored(range);
assertReplacedBranches(switchNode, newTargets);
}
/**
* <pre>
* enum E {
* A, B, C
* }
*
* int example(E e) {
* return switch (e) {
* case A -> 1;
* case B -> 2;
* case C -> 3;
* };
* }
* </pre>
*/
@Test
public void should_filter_when_default_branch_throws_Java_21_MatchException() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"Example", "()I", null, null);
final Label start = new Label();
final Label end = new Label();
m.visitLabel(start);
m.visitLineNumber(0, start);
m.visitFieldInsn(Opcodes.GETSTATIC, "Example$1", "$SwitchMap$Example$E",
"[I");
m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Example$E", "ordinal", "()I",
false);
m.visitInsn(Opcodes.IALOAD);
final Label dflt = new Label();
final Label case1 = new Label();
final Label case2 = new Label();
final Label case3 = new Label();
m.visitLookupSwitchInsn(dflt, new int[] { 1, 2, 3 },
new Label[] { case1, case2, case3 });
final AbstractInsnNode switchNode = m.instructions.getLast();
final Set<AbstractInsnNode> newTargets = new HashSet<AbstractInsnNode>();
m.visitLabel(dflt);
final Range range = new Range();
range.fromInclusive = m.instructions.getLast();
m.visitTypeInsn(Opcodes.NEW, "java/lang/MatchException");
m.visitInsn(Opcodes.DUP);
m.visitInsn(Opcodes.ACONST_NULL);
m.visitInsn(Opcodes.ACONST_NULL);
m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/MatchException",
"<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V", false);
m.visitInsn(Opcodes.ATHROW);
range.toInclusive = m.instructions.getLast();
m.visitLabel(case1);
m.visitInsn(Opcodes.ICONST_1);
newTargets.add(m.instructions.getLast());
m.visitJumpInsn(Opcodes.GOTO, end);
m.visitLabel(case2);
m.visitInsn(Opcodes.ICONST_2);
newTargets.add(m.instructions.getLast());
m.visitLabel(case3);
m.visitInsn(Opcodes.ICONST_3);
newTargets.add(m.instructions.getLast());
m.visitLabel(end);
m.visitInsn(Opcodes.IRETURN);
filter.filter(m, context, output);
assertIgnored(range);
assertReplacedBranches(switchNode, newTargets);
}
/**
* <pre>
* enum E {
* A, B, C
* }
*
* int example(E e) {
* return switch (e) {
* case A -> 1;
* case B -> 2;
* default -> throw new IncompatibleClassChangeError();
* };
* }
* </pre>
*/
@Test
public void should_not_filter_when_default_branch_has_LineNumber_different_from_switch() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"Example", "()I", null, null);
final Label start = new Label();
final Label end = new Label();
m.visitLabel(start);
m.visitLineNumber(0, start);
m.visitFieldInsn(Opcodes.GETSTATIC, "Example$1", "$SwitchMap$Example$E",
"[I");
m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Example$E", "ordinal", "()I",
false);
m.visitInsn(Opcodes.IALOAD);
final Label dflt = new Label();
final Label case1 = new Label();
final Label case2 = new Label();
m.visitLookupSwitchInsn(dflt, new int[] { 1, 2 },
new Label[] { case1, case2 });
m.visitLabel(dflt);
m.visitLineNumber(1, dflt);
m.visitTypeInsn(Opcodes.NEW, "java/lang/IncompatibleClassChangeError");
m.visitInsn(Opcodes.DUP);
m.visitMethodInsn(Opcodes.INVOKESPECIAL,
"java/lang/IncompatibleClassChangeError", "<init>", "()V",
false);
m.visitInsn(Opcodes.ATHROW);
m.visitLabel(case1);
m.visitInsn(Opcodes.ICONST_1);
m.visitJumpInsn(Opcodes.GOTO, end);
m.visitLabel(case2);
m.visitInsn(Opcodes.ICONST_2);
m.visitLabel(end);
m.visitInsn(Opcodes.IRETURN);
filter.filter(m, context, output);
assertIgnored();
}
}