JMU
Testing and Debugging Conditionals


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
Desk Checking Conditionals
Testing Conditionals
Testing Conditionals Involving Discrete Sets
Testing Conditionals Involving Discrete Sets (cont.)

An Example:

  if (seatLocationCode == 100) 
  {
    // ...
  } 
  else if (seatLocationCode == 200) 
  {
    // ...
  } 
  else if (seatLocationCode == 300) 
  {
    // ...
  } 
  else 
  {
    // ...
  }
  

Test cases: 50 100 150 200 300 1000

Testing Conditionals Involving Intervals/Ranges
Testing Conditionals Involving Intervals/Ranges (cont.)

An Example:

  if ((income > 0.0) && (income < 20000.0))
  {
     // ...
  }
  else 
  {
     // ...
  }
  

Test cases: -1.0 0.0 10000.0 20000.0 20001.0

Testing Compound and Nested Conditions
Compound/Nested Conditions (cont.)

An Example of a Compound Condition:

  if ((height >= 62) && (sex == 'M'))
  {
    // ...
  }
  

Test cases:
70, 'M'
70, 'F'
62, 'M'
62, 'F'
50, 'M'
50, 'F'

Testing Compound/Nested Conditions (cont.)

An Example of Nested Conditions:

if (height >= 62) 
{
  if (sex == 'M') 
  {
    // ...
  } 
  else
  {
    // ...
  }
} 
else 
{
  if (sex == 'M') 
  {
    // ...
  }
  else 
  {
    // ...
  }
}
  

Test cases:
70, 'M'
70, 'F'
62, 'M'
62, 'F'
50, 'M'
50, 'F'

Instrumenting Conditionals During Debugging
An Example of Instrumenting Conditionals
javaexamples/basics/DebuggingConditionals.java (Fragment: 0)
                // For debugging
        JMUConsole.printf("Before: height(%d) sex(%c)", height, sex);
        JMUConsole.printf("        (height >= 62)  %s)",(height>=62));
        JMUConsole.printf("        (sex == 'M')    %s)",(sex=='M'));

        if ((height >= 62) && (sex == 'M')) 
        {
            // For debugging
            JMUConsole.printf("In if-block: height(%d) sex(%c)", height, sex);

            // ...
        }
        else 
        {
            // For debugging
            JMUConsole.printf("In else-block: height(%d) sex(%c)", height, sex);

            // ...
        }
        // For debugging
        JMUConsole.printf("After: height(%d) sex(%c)", height, sex);