Have you ever wondered what you need to mutate a Java code excerpt? All you need is the help of LittleDarwin! In this notebook, we are going to see how this can be done, and what you can do with the machinery LittleDarwin provides for you.
Consider this very simple piece of code:
public class MyCode {
void bubbleSort(int array[]) {
int len = array.length;
for (int i = 0; i < len - 1; i++)
for (int j = 0; j < len - i - 1; j++)
if (array[j] > array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
arr[j+1] = temp;
}
}
}
First, we need to write some Python code, because LittleDarwin is written in Python for some reason.
We wrap the code in a string:
In [1]:
myJavaCode = """
public class MyCode {
void bubbleSort(int array[])
{
int len = array.length;
for (int i = 0; i < len - 1; i++)
for (int j = 0; j < len - i - 1; j++)
if (array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
arr[j+1] = temp;
}
}
}
"""
Now we need to summon the knights of LittleDarwin: JavaParse and JavaMutate:
In [2]:
from littledarwin import JavaParse, JavaMutate
Then, we initiate the parser, and retrieve the parse tree:
In [3]:
myParser = JavaParse.JavaParse()
myParseTree = myParser.parse(myJavaCode)
Now we can initiate the mutator by giving it all it wants: the parse tree, source code, and the JavaParse instance. We can then execute gatherMutants method to retrieve all the mutants the knights of LittleDarwin could create.
In [4]:
myMutator = JavaMutate.JavaMutate(myParseTree, myJavaCode, myParser)
mutants, mutantTypes = myMutator.gatherMutants("All")
At this point, the job is done. You have two things back from this quest: a bunch of mutants in a list, and a dictionary that tells you how many of each mutant type there is.
In [5]:
for mt in mutants:
print("------------------------------------------------------------------\n")
print(mt)
print("------------------------------------------------------------------\n")
In [6]:
for mtType in mutantTypes.keys():
if mutantTypes[mtType] > 0:
print(mtType, ":", mutantTypes[mtType])
That's all folks! More complicated stuff coming soon.