use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.
the class StringLiteralAmplifier method amplify.
@Override
protected Set<CtLiteral<String>> amplify(CtLiteral<String> existingLiteral) {
final Factory factory = existingLiteral.getFactory();
Set<CtLiteral<String>> values = new HashSet<>();
String value = (String) existingLiteral.getValue();
// TODO idk if we should add all values around
values.addAll(this.testClassToBeAmplified.getElements(new TypeFilter<CtLiteral<String>>(CtLiteral.class)).stream().filter(element -> element.getValue() instanceof String && element.getValue() != null && !element.equals(existingLiteral)).map(CtLiteral::clone).collect(Collectors.toList()));
if (value != null) {
if (value.length() > 2) {
int length = value.length();
// add one random char
int index = AmplificationHelper.getRandom().nextInt(length - 2) + 1;
values.add(factory.createLiteral(value.substring(0, index - 1) + AmplificationHelper.getRandomChar() + value.substring(index, length)));
// replace one random char
index = AmplificationHelper.getRandom().nextInt(length - 2) + 1;
values.add(factory.createLiteral(value.substring(0, index) + AmplificationHelper.getRandomChar() + value.substring(index, length)));
// remove one random char
index = AmplificationHelper.getRandom().nextInt(length - 2) + 1;
values.add(factory.createLiteral(value.substring(0, index) + value.substring(index + 1, length)));
} else {
values.add(factory.createLiteral("" + AmplificationHelper.getRandomChar()));
}
}
// add one random string
values.add(factory.createLiteral(AmplificationHelper.getRandomString(10)));
// add empty string
values.add(factory.createLiteral(""));
return values;
}
use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.
the class NumberLiteralAmplifierTest method testShortMutation.
@Test
public void testShortMutation() throws Exception {
final String nameMethod = "methodShort";
final short originalValue = 23;
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
AmplificationHelper.setSeedRandom(42L);
NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<Short> expectedValues = Arrays.asList((short) 22, (short) 24, Short.MIN_VALUE, Short.MAX_VALUE, (short) 0);
List<CtMethod> mutantMethods = amplificator.apply(method);
assertEquals(5, mutantMethods.size());
for (int i = 0; i < mutantMethods.size(); i++) {
CtMethod mutantMethod = mutantMethods.get(i);
assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName());
CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
assertNotEquals(originalValue, mutantLiteral.getValue());
assertTrue(mutantLiteral.getValue() + " not in expected values", expectedValues.contains(mutantLiteral.getValue()));
}
}
use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.
the class NumberLiteralAmplifierTest method testIntMutation.
@Test
public void testIntMutation() throws Exception {
final String nameMethod = "methodInteger";
final int originalValue = 23;
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
AmplificationHelper.setSeedRandom(42L);
NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<Integer> expectedValues = Arrays.asList(22, 24, 2147483647, -2147483648, 0);
List<CtMethod> mutantMethods = amplificator.apply(method);
assertEquals(5, mutantMethods.size());
for (int i = 0; i < mutantMethods.size(); i++) {
CtMethod mutantMethod = mutantMethods.get(i);
assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName());
CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
assertNotEquals(originalValue, mutantLiteral.getValue());
assertTrue(mutantLiteral.getValue() + " not in expected values", expectedValues.contains(mutantLiteral.getValue()));
}
}
use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.
the class NumberLiteralAmplifierTest method testLongMutation.
@Test
public void testLongMutation() throws Exception {
final String nameMethod = "methodLong";
final long originalValue = 23L;
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
AmplificationHelper.setSeedRandom(42L);
NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<Long> expectedValues = Arrays.asList(22L, 24L, Long.MIN_VALUE, Long.MAX_VALUE, 0L);
List<CtMethod> mutantMethods = amplificator.apply(method);
assertEquals(5, mutantMethods.size());
for (int i = 0; i < mutantMethods.size(); i++) {
CtMethod mutantMethod = mutantMethods.get(i);
assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName());
CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
assertNotEquals(originalValue, mutantLiteral.getValue());
assertTrue(mutantLiteral.getValue() + " not in expected values", expectedValues.contains(mutantLiteral.getValue()));
}
}
use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.
the class NumberLiteralAmplifierTest method testDoubleMutation.
@Test
public void testDoubleMutation() throws Exception {
final String nameMethod = "methodDouble";
final double originalValue = 23.0D;
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
AmplificationHelper.setSeedRandom(42L);
NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<Double> expectedValues = Arrays.asList(22.0D, 24.0D, Double.MAX_VALUE, Double.MIN_VALUE, Double.MIN_NORMAL, Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0D);
List<CtMethod> mutantMethods = amplificator.apply(method);
assertEquals(9, mutantMethods.size());
for (int i = 0; i < mutantMethods.size(); i++) {
CtMethod mutantMethod = mutantMethods.get(i);
assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName());
CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
assertNotEquals(originalValue, mutantLiteral.getValue());
assertTrue(mutantLiteral.getValue() + " not in expected values", expectedValues.contains(mutantLiteral.getValue()));
}
}
Aggregations