lundi 4 septembre 2017

Testing for reachablity, infection and propagation in code

I have a piece of code that converts a string to list of words. I have been trying very hard to find a test case such that, the code passes if the fault is reached, infected, but does not propagate, but I am unable to come up with 1.

public static ArrayList<String> convertStringToList(String string){
    ArrayList<String> listOfWords = new ArrayList<>();
    String word = "";
    int count = 0;
    for (int i=0; i<string.length(); i++){
        if(string.charAt(i) == ' '){
            if(count<1 || i!=0){   //bug/fault should be if (count<1 && i!=0)
                listOfWords.add(word);
                word = "";
            }
            count++;
        }
        else{
            count = 0;
            word = word+string.charAt(i);
            if(i == string.length()-1){
                listOfWords.add(word);
            }
        }
    }
    return listOfWords;
}

Definition of infection and propagation

Infection: executing the fault must cause the state of the program to become incorrect.

Propagation: the incorrect state must result in some incorrect and observable behavior.

A possible case of failure that I came up with is shown in the code below. It's a failure because the whitespace is included in the list of words whereas it should not be included:

public void testFault() {
     String sentence = " Found a bug on line 7";
     ArrayList<String> expectedList = new ArrayList<>();
     expectedList.addAll(Arrays.asList("Found", "a", "bug", "on", "line", "7"));
     assertEquals(expectedList, Bugs.convertStringToList(sentence));
 }

Any help or suggestions would be appreciated.

Aucun commentaire:

Enregistrer un commentaire