Skip to content

Commit

Permalink
include case value in switch descriptions
Browse files Browse the repository at this point in the history
Fixes #1098
  • Loading branch information
Henry Coles committed Oct 24, 2022
1 parent cef0e18 commit b93565e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ private final class RemoveSwitchMethodVisitor extends MethodVisitor {
}

@Override
public void visitTableSwitchInsn(final int i, final int i1,
public void visitTableSwitchInsn(final int min, final int max,
final Label defaultLabel, final Label... labels) {
if ((labels.length > RemoveSwitchMutator.this.key) && shouldMutate()) {
if ((labels.length > RemoveSwitchMutator.this.key) && shouldMutate(value(min,max))) {
final Label[] newLabels = labels.clone();
newLabels[RemoveSwitchMutator.this.key] = defaultLabel;
super.visitTableSwitchInsn(i, i1, defaultLabel, newLabels);
super.visitTableSwitchInsn(min, max, defaultLabel, newLabels);
} else {
super.visitTableSwitchInsn(i, i1, defaultLabel, labels);
super.visitTableSwitchInsn(min, max, defaultLabel, labels);
}
}

@Override
public void visitLookupSwitchInsn(final Label defaultLabel,
final int[] ints, final Label[] labels) {
if ((labels.length > RemoveSwitchMutator.this.key) && shouldMutate()) {
if ((labels.length > RemoveSwitchMutator.this.key) && shouldMutate(ints[key])) {
final Label[] newLabels = labels.clone();
newLabels[RemoveSwitchMutator.this.key] = defaultLabel;
super.visitLookupSwitchInsn(defaultLabel, ints, newLabels);
Expand All @@ -90,13 +90,17 @@ public void visitLookupSwitchInsn(final Label defaultLabel,
}
}

private boolean shouldMutate() {
private boolean shouldMutate(int value) {
final MutationIdentifier mutationId = this.context.registerMutation(
RemoveSwitchMutator.this, "RemoveSwitch "
+ RemoveSwitchMutator.this.key + " mutation");
+ RemoveSwitchMutator.this.key + " (case value " + value + ")");
return this.context.shouldMutate(mutationId);
}

private int value(int min, int max) {
return min + key;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ public Integer apply(int value) {
}
}

private static class HasContinuousIntSwitchWithDefault implements IntFunction<Integer> {
@Override
public Integer apply(int value) {
switch (value) {
case 10:
return 0;
case 11:
return 1;
case 12:
return 2;
default:
return -1;
}
}
}

@Test
public void shouldChangeLabelInt() {
IntMutantVerifier<Integer> v2 = v.forIntFunctionClass(HasIntSwitchWithDefault.class);
Expand All @@ -78,6 +94,20 @@ public void shouldChangeLabelInt() {
v2.firstMutantShouldReturn(3, -1);
}

@Test
public void includesValuesInDescriptionForTableSwitchMutations() {
MutatorVerifierStart.forMutator(new RemoveSwitchMutator(0))
.forIntFunctionClass(HasContinuousIntSwitchWithDefault.class)
.firstMutantDescription()
.isEqualTo("RemoveSwitch 0 (case value 10)");

MutatorVerifierStart.forMutator(new RemoveSwitchMutator(2))
.forIntFunctionClass(HasContinuousIntSwitchWithDefault.class)
.firstMutantDescription()
.isEqualTo("RemoveSwitch 2 (case value 12)");

}

private static class HasCharSwitchWithDefault implements Function<Character,Character> {

@Override
Expand Down Expand Up @@ -166,6 +196,12 @@ public void shouldReplaceTableLabelsInt() {
8);
}

@Test
public void includesValueInLookupSwitchDescription() {
IntMutantVerifier<Integer> v2 = v.forIntFunctionClass(HasMultipleArmIntSwitchWithDefault.class);
v2.firstMutantDescription().isEqualTo("RemoveSwitch 2 (case value 4000)");
}

private static class HasMultipleArmIntSwitchWithoutDefault implements IntFunction<Integer> {

@Override
Expand Down

0 comments on commit b93565e

Please sign in to comment.