Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not mutate fall through switch cases #1138

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Read all about it at http://pitest.org

## Releases

## 1.10.5 (unreleased)

* #1138 Do not mutate redundant fall through to default switch cases

## 1.10.4

* #1134 Add `excludedRunners` parameter to command line interface and Ant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* default label. We change each label to the default label, thus removing it
*/
public class RemoveSwitchMutator implements MethodMutatorFactory {
// EXPERIMENTAL_REMOVE_SWITCH_MUTATOR;

private static final String REMOVE_SWITCH_MUTATOR_NAME = "EXPERIMENTAL_REMOVE_SWITCH_MUTATOR_";
private static final int GENERATE_FROM_INCLUDING = 0;
private static final int GENERATE_UPTO_EXCLUDING = 100;
Expand Down Expand Up @@ -69,7 +69,7 @@ private final class RemoveSwitchMethodVisitor extends MethodVisitor {
@Override
public void visitTableSwitchInsn(final int min, final int max,
final Label defaultLabel, final Label... labels) {
if ((labels.length > RemoveSwitchMutator.this.key) && shouldMutate(value(min,max))) {
if ((labels.length > RemoveSwitchMutator.this.key) && labels[key] != defaultLabel && shouldMutate(value(min,max))) {
final Label[] newLabels = labels.clone();
newLabels[RemoveSwitchMutator.this.key] = defaultLabel;
super.visitTableSwitchInsn(min, max, defaultLabel, newLabels);
Expand All @@ -81,7 +81,7 @@ public void visitTableSwitchInsn(final int min, final int max,
@Override
public void visitLookupSwitchInsn(final Label defaultLabel,
final int[] ints, final Label[] labels) {
if ((labels.length > RemoveSwitchMutator.this.key) && shouldMutate(ints[key])) {
if ((labels.length > RemoveSwitchMutator.this.key) && labels[key] != defaultLabel && shouldMutate(ints[key])) {
final Label[] newLabels = labels.clone();
newLabels[RemoveSwitchMutator.this.key] = defaultLabel;
super.visitLookupSwitchInsn(defaultLabel, ints, newLabels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,59 @@ public void shouldNotTouchIt() {
v.forClass(HasFewerLabelsWithDefault.class)
.noMutantsCreated();
}


private static class FallThroughLookupSwitch implements IntFunction<Integer> {

@Override
public Integer apply(int value) {
int i = 0;
switch (value) {
case 1:
i = 1;
break;
case 200:
i = 4;
break;
case 300:
case 400:
default:
i = 0;
}
return i;
}
}

@Test
public void doesNotReplaceLookupSwitchCasesThatFallThroughToDefault() {
v.forClass(FallThroughLookupSwitch.class)
.noMutantsCreated();
}

private static class FallThroughTableSwitch implements Function<TimeUnit,Integer> {

@Override
public Integer apply(TimeUnit value) {
switch (value) {
case NANOSECONDS:
return 1;
case MICROSECONDS:
return 2;
case MILLISECONDS:
case SECONDS:
default:
return -1;
}
}
}

@Test
public void doesNotReplaceTableSwitchCasesThatFallThroughToDefault() {
v.forClass(FallThroughTableSwitch.class)
.noMutantsCreated();
}


}


Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public void createsNMutants(int n) {
}

public void noMutantsCreated() {
assertThat(findMutations()).isEmpty();
assertThat(findMutations())
.as(() -> "Expecting no mutants to be generated for " + printClass(clazz))
.isEmpty();
}

public final StringAssert firstMutantDescription() {
Expand Down