Defect taxonomy exercises

De Software testing
Ir para: navegação, pesquisa

Exercise 1

Consider the following program:

public class firsthwk
{
     /**
      * Checks a list of names and counts how many blanks.
      */
     public static int nameCheck(char[] names)
     {
        int blankCount = 0;
        for (int i = names.length; i > 1; i--) {
           if (names[i-1] == ' ') {
              blankCount++;
           }
        }
        return blankCount;
     }

     public static void main (String[] args)
     {
        // Test driver -- method calls to nameCheck()
     }
}
  1. Compile this program using a Java compiler. You will need to modify the main() method to contain calls to the nameCheck() method and print the result (each call to the method represents one test case).
  2. This program contains a fault. What is it? Does executing the program necessarily result in either incorrect output or in failure?
  3. Find a test case that results in failure. Verify by executing this test case.
  4. Identify another test case that does not result in failure. Verify by executing this test case.
  5. Find tests to execute every statement in the method nameCheck(). Find the minimal set, that is, turn in a set of tests such that if any one test was removed, the remaining tests would no longer execute every statement.

Solution

  1. Compile this program using a Java compiler. You will need to modify the main() method to contain calls to the nameCheck() method and print the result (each call to the method represents one test case).
public class firsthwk
{
	/**
	 * Samples a list of names and counts how many blanks.
	 */
	public static int nameCheck(char[] names)
	{
		int blankCount = 0;
		for (int i = names.length; i > 1; i--) {
			if (names[i-1] == ' ') {
				blankCount++;
			}
		}
		return blankCount;
	}

	/**
	 * Run one test, print and check results.
	 */
	private static void runTest (int testNum, char[] testIn, int expectedOut)
	{
		int actualOut;
		actualOut = nameCheck (testIn);
		System.out.print("Test #" + testNum + ", num blanks: " + actualOut + " ... expected output: " + expectedOut);
		if (actualOut != expectedOut) {
			System.out.print ("   *** FAILURE ***");
		}
		System.out.println();
	}

	/**
	 * Test driver -- method calls to nameCheck().
	 */
	public static void main (String args[])
	{
		int expectedOut;

		// three blanks at the beginning
		char[] arr1 = {' ', ' ', ' ', 'c', 'd'};
		expectedOut = 3;
		runTest(1, arr1, expectedOut);

		// A blank in the middle and at the end.
		char[] arr2 = {'a', 'b', ' ', 'c', ' '};
		expectedOut = 2;
		runTest(2, arr2, expectedOut);

		// No elements
		char[] arr3 = {};
		expectedOut = 0;
		runTest(3, arr3, expectedOut);

		// Null array
		char[] arr4 = null;
		expectedOut = 0;
		runTest(4, arr4, expectedOut);
	}
}
  1. This program contains a fault. What is it? Does executing the program necessarily result in either incorrect output or in failure?

The loop is controlled by the length of the array names. The first element names[0] is not checked, so the method only fails if element 0 is a blank.

  1. Find a test case that results in failure. Verify by executing this test case.
     char arr1 [] = {' ', ' ', ' ', 'c', 'd'};
     System.out.println ("num blanks: " + nameCheck (arr1) + " ... expected output: 3");
  1. Identify another test case that does not result in failure. Verify by executing this test case.
     char arr2 [] = {'a', 'b', ' ', 'c', ' '};
     System.out.println ("num blanks: " + nameCheck (arr2) + " ... expected output: 2");
  1. Find tests to execute every statement in the method nameCheck(). Find the minimal set, that is, turn in a set of tests such that if any one test was removed, the remaining tests would no longer execute every statement.
    Both tests above execute every statement, so either one without the other answers the question.

References


Exercise 2