Skip to main content

CSE 331: Testing practice

Question 1: Test Coverage

Here is a method, with its specification in Javadoc comments. Its code has one mistake. Don't fix it.

/**
 * Finds the largest element of an array.
 *
 * @param a the array to search; must be non-null
 * @requires 0 < a.length
 * @return the largest element of a
 */
static int max(int[] a) {
    int m = 0;
    for (int i = 0; i < a.length; i++) {
        if (m < a[i]) {
            m = a[i];
        }
    }
    return m;
}

Each row lists arrays. For each array a in a row, one test calls max(a) and checks the result against the specification. For each row, answer each question about its tests:

  • Y for yes
  • N for no
Statement coverage? Branch coverage? Loop coverage? Some test fails?
A. {3, 3}
B. {2, 9}
C. {-4}
D. {3}
{5, 2}
E. {-3, 0}

A. {3, 3}

Answer
Statement coverage? Branch coverage? Loop coverage? Some test fails?
Y Y N N

Its two elements are equal, but they take different branches, since m changes between them.


Statement coverage: yes.

  • m starts at 0.
  • On the first 3, 0 < 3, so m = a[i] runs.
  • So every statement in the method runs.

Branch coverage: yes.

  • The loop's condition is a branch too: true before each run of the body, and false at the end.
  • The if's condition is true on the first 3, since 0 < 3.
  • Then m is 3, and 3 < 3 is false, so the second 3 takes the false branch.

Loop coverage: no.

  • The body runs twice.
  • No test runs it once.

Some test fails: no.

  • The largest element of {3, 3} is 3.
  • m becomes 3, and the method returns 3.
  • The mistake is int m = 0, where m should start at a[0].
  • It shows only when every element is negative, and 3 is not.

The definitions: Lecture 5's Statement coverage, Branch coverage, and Loop coverage.

B. {2, 9}

Answer
Statement coverage? Branch coverage? Loop coverage? Some test fails?
Y N N N

Each element is larger than m when the loop reaches it, so the if's condition is never false.


Statement coverage: yes.

  • m starts at 0.
  • On 2, 0 < 2, so m = a[i] runs.
  • So every statement in the method runs.

Branch coverage: no.

  • The if's condition is true on 2, since 0 < 2, and true on 9, since 2 < 9.
  • No test makes it false.
  • A smaller element after a larger one, as in {9, 2}, would take the false branch.

Loop coverage: no.

  • The body runs twice.
  • No test runs it once.

Some test fails: no.

  • The largest element of {2, 9} is 9.
  • m becomes 2, then 9, and the method returns 9.

The definitions: Lecture 5's Statement coverage, Branch coverage, and Loop coverage.

C. {-4}

Answer
Statement coverage? Branch coverage? Loop coverage? Some test fails?
N N N Y

Its one element is negative, so m stays 0, and the test fails.


Statement coverage: no.

  • m starts at 0.
  • On -4, 0 < -4 is false.
  • So m = a[i] never runs.

Branch coverage: no.

  • The if's condition is never true, so no test takes its true branch.

Loop coverage: no.

  • The body runs once.
  • No test runs it more than once.

Some test fails: yes.

  • The largest element of {-4} is -4, so the specification asks for -4.
  • But m stays 0, and the method returns 0.

The definitions: Lecture 5's Statement coverage, Branch coverage, and Loop coverage.

D. {3}
{5, 2}

Answer
Statement coverage? Branch coverage? Loop coverage? Some test fails?
Y Y Y N

It has all three kinds of coverage, though no test runs the loop's body zero times: the precondition rules out an empty array. Every test passes.


Statement coverage: yes.

  • m starts at 0.
  • On {3}, 0 < 3, so m = a[i] runs.
  • That test runs every statement in the method.

Branch coverage: yes.

  • The loop's condition is a branch too: true before each run of the body, and false at the end.
  • The if's condition is true on 3 and on 5, and false on 2, since 5 < 2 is false.

Loop coverage: yes.

  • The body runs once on {3}, and twice on {5, 2}.
  • No test runs it zero times.
  • The body runs once for each element of the array.
  • The precondition 0 < a.length rules out an empty array.
  • So no allowed input runs the body zero times, and loop coverage asks for no such test.

Some test fails: no.

  • The method returns 3 and 5, the largest elements.
  • The mistake shows only when every element is negative, and each array here has an element that is not.

The definitions: Lecture 5's Statement coverage, Branch coverage, and Loop coverage.

E. {-3, 0}

Answer
Statement coverage? Branch coverage? Loop coverage? Some test fails?
N N N N

No element is larger than 0, where m starts, so m = a[i] never runs. But the largest element is 0, so the test passes.


Statement coverage: no.

  • 0 < -3 and 0 < 0 are both false.
  • So m = a[i] never runs.

Branch coverage: no.

  • The if's condition is never true, so no test takes its true branch.

Loop coverage: no.

  • The body runs twice.
  • No test runs it once.

Some test fails: no.

  • The largest element of {-3, 0} is 0.
  • m stays 0, and the method returns 0.
  • The mistake shows only when every element is negative, and 0 is not.

The definitions: Lecture 5's Statement coverage, Branch coverage, and Loop coverage.

Question 2: Test Coverage

Here is a method, with its specification in Javadoc comments. Its code has one mistake. Don't fix it.

/**
 * Counts the elements of an array that are greater than a bound.
 *
 * @param a the array to count in; must be non-null
 * @param x the bound
 * @return the number of elements of a greater than x
 */
static int countAbove(int[] a, int x) {
    int n = 0;
    for (int i = 0; i < a.length; i++) {
        if (x <= a[i]) {
            n = n + 1;
        }
    }
    return n;
}

Each row lists arrays. For each array a in a row, one test calls countAbove(a, 5) and checks the result against the specification. For each row, answer each question about its tests:

  • Y for yes
  • N for no
Statement coverage? Branch coverage? Loop coverage? Some test fails?
A. {7}
B. {7, 3}
C. {}
{3}
{7, 3}
D. {}
{5}
E. {}
{3}
{3, 1}

A. {7}

Answer
Statement coverage? Branch coverage? Loop coverage? Some test fails?
Y N N N

Its one test runs every statement, but never takes the if's false branch, and runs the loop's body only once.


Statement coverage: yes.

  • On {7} the loop's body runs once.
  • 5 <= 7, so n = n + 1 runs too.
  • So every statement in the method runs.

Branch coverage: no.

  • The if's condition is true on 7, and no test makes it false.
  • An element below 5, as in {3}, would take the false branch.

Loop coverage: no.

  • The body runs once.
  • No test runs it zero times, or more than once.

Some test fails: no.

  • One element of {7} is greater than 5, so the specification asks for 1.
  • The method returns 1.
  • The mistake is x <= a[i], where the specification counts elements greater than x.
  • It shows only on an element equal to 5, and {7} has none.

The definitions: Lecture 5's Statement coverage, Branch coverage, and Loop coverage.

B. {7, 3}

Answer
Statement coverage? Branch coverage? Loop coverage? Some test fails?
Y Y N N

Its one test takes every branch, but runs the loop's body twice, never zero times or once.


Statement coverage: yes.

  • On {7, 3} the loop's body runs twice.
  • 5 <= 7, so n = n + 1 runs.
  • So every statement in the method runs.

Branch coverage: yes.

  • The loop's condition is a branch too: true before each run of the body, and false at the end.
  • The if's condition is true on 7, and false on 3.

Loop coverage: no.

  • The body runs twice.
  • No test runs it zero times, or once.

Some test fails: no.

  • One element of {7, 3} is greater than 5, so the specification asks for 1.
  • 5 <= 3 is false, so the method counts only 7, and returns 1.

The definitions: Lecture 5's Statement coverage, Branch coverage, and Loop coverage.

C. {}
{3}
{7, 3}

Answer
Statement coverage? Branch coverage? Loop coverage? Some test fails?
Y Y Y N

It has all three kinds of coverage, and still every test passes: no element equals 5, where the mistake shows.


Statement coverage: yes.

  • On {7, 3}, 5 <= 7, so n = n + 1 runs.
  • That test runs every statement in the method.

Branch coverage: yes.

  • The loop's condition is a branch too: true in the tests on {3} and {7, 3}, and false at the end of every test.
  • The if's condition is true on 7, and false on 3.

Loop coverage: yes.

  • The body runs zero times on {}, once on {3}, and twice on {7, 3}.

Some test fails: no.

  • The mistake is x <= a[i], where the specification counts elements greater than x.
  • It shows only on an element equal to 5, and no array here has one.
  • So each test gets the count the specification asks for: 0, 0, and 1.

The definitions: Lecture 5's Statement coverage, Branch coverage, and Loop coverage.

D. {}
{5}

Answer
Statement coverage? Branch coverage? Loop coverage? Some test fails?
Y N N Y

Its tests find the mistake, though they have neither branch coverage nor loop coverage.


Statement coverage: yes.

  • On {5}, 5 <= 5, so n = n + 1 runs.
  • That test runs every statement in the method.

Branch coverage: no.

  • The if's condition is true on 5, and no test makes it false.

Loop coverage: no.

  • The body runs zero times on {}, and once on {5}.
  • No test runs it more than once.

Some test fails: yes.

  • No element of {5} is greater than 5, so the specification asks for 0.
  • But 5 <= 5, so the method counts it, and returns 1.

The definitions: Lecture 5's Statement coverage, Branch coverage, and Loop coverage.

E. {}
{3}
{3, 1}

Answer
Statement coverage? Branch coverage? Loop coverage? Some test fails?
N N Y N

It has loop coverage, but no element is 5 or more, so n = n + 1 never runs.


Statement coverage: no.

  • No element is 5 or more, so the if's condition is never true.
  • So n = n + 1 never runs.

Branch coverage: no.

  • The if's condition is never true, so no test takes its true branch.

Loop coverage: yes.

  • The body runs zero times on {}, once on {3}, and twice on {3, 1}.

Some test fails: no.

  • No element is greater than 5, so the specification asks for 0 each time.
  • No element is 5 or more either, so the method returns 0 each time.

The definitions: Lecture 5's Statement coverage, Branch coverage, and Loop coverage.