Demo entry 3548568

EngineeringDesign

   

Submitted by anonymous on Jan 13, 2016 at 22:32
Language: Java. Code size: 3.8 kB.

public class JavaExample {
	/*
	 * Engineering Design A3 2.3
	 * Java example of difference between code coverage analysis and mutation analysis of tests.
	 */

	public static int[] insertionSort(int[] input){
		int[] output = new int[input.length];
		for(int i = 0; i < input.length; i++){
			output[i] = input[i];
		}
		
		int temp;
        for (int i = 1; i < output.length; i++) {
            for(int j = i ; j > 0 ; j--){
                if(output[j] < output[j-1]){
                    temp = output[j];
                    output[j] = output[j-1];
                    output[j-1] = temp;
                }
            }
        }
        return output;
	}
	
	public static String toString(int[] input){
		String res = "[";
		for(int i = 0; i < input.length; i++){
			res = res + input[i] + " ";
		}
		res = res.substring(0, res.length()-1) + "]";
		return res;
	}
	
	public static void main(String args[]){
		int[] arrayOriginal = {11,44,21,546,71,63,22,10};
		int[] arraySorted = insertionSort(arrayOriginal);
		int[] arrayMutated = mutatedSort(arrayOriginal);
		
		testSuite(arraySorted);
		mutatedTestSuite(arrayMutated);
		codeCoverageTestSuite(arrayOriginal);
    }
	
	/*
	 * Correct test.
	 */
	public static void testSuite(int[] arraySorted){
		System.out.println("test suite result:");
		System.out.println(toString(arraySorted).equals("[10 11 21 22 44 63 71 546]"));
	}
	
	public static void mutatedTestSuite(int[] arrayMutated){
		System.out.println("mutation analysis test result (false):");
		System.out.println(toString(arrayMutated).equals("[10 11 21 22 44 63 71 546]"));
	}
	
	/*
	 * Mutated version of insertionSort. Note the omission of the temp variable.
	 */
	public static int[] mutatedSort(int[] input){		
		int[] output = new int[input.length];
		for(int i = 0; i < input.length; i++){
			output[i] = input[i];
		}
		
        for (int i = 1; i < output.length; i++) {
            for(int j = i ; j > 0 ; j--){
                if(output[j] < output[j-1]){
                    output[j] = output[j-1];
                    output[j-1] = output[j];
                }
            }
        }
        return output;
	}
	
	public static void codeCoverageTestSuite(int [] arrayCoverage){
		System.out.println("code coverage test suite results (true, false):");
		System.out.println(completeCoverage(arrayCoverage));
		int[] incompleteCoverageInput = {10,11,21,22,44,63,71,546};
		System.out.println(completeCoverage(incompleteCoverageInput));

	}
	
	/*
	 * This function is a copy of the original insertion sort with added checks to see whether each section is covered with the input of the test.
	 * In the case of an already sorted array, the function will return false.
	 */
	public static boolean completeCoverage(int[] input){
		boolean res = false;
		boolean check1 = false; boolean check2 = false; boolean check3 = false; boolean check4 = false; boolean check5 = false; boolean check6 = false; boolean check7 = false;
		
		check1 = true;
		int[] output = new int[input.length];
		for(int i = 0; i < input.length; i++){
			output[i] = input[i];
			check2 = true;
		}
		
    	check3 = true;
		int temp;
        for (int i = 1; i < output.length; i++) {
        	check4 = true;
            for(int j = i ; j > 0 ; j--){
            	check5 = true;
                if(output[j] < output[j-1]){
                	check6 = true;
                    temp = output[j];
                    output[j] = output[j-1];
                    output[j-1] = temp;
                }
            }
        }
    	check7 = true;
        if(check1 && check2 && check3 && check4 && check5 && check6 && check7){
        	res = true;
        }
		return res;
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).