- Forward


Testing and Debugging Conditionals


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Desk Checking:
    • Verifying that a program satisfies specifications by carefully reading the code
  • Testing:
    • Verifying that a program satisfies specifications by executing the code
  • Debugging:
    • Locating and repairing logic errors
Desk Checking Conditionals
Back SMYC Forward
  • Conditions Involving Equality:
    • Check for the use of = instead of ==
    • Check for the use of == instead of .equals() with reference types
    • Check for the use of == with floating point numbers
  • Conditions Involving Inequality:
    • Check for directionality mistakes
    • Check for weak inequalities in the place of strict inequalities
Testing Conditionals
Back SMYC Forward
  • The Sets Involved:
    • A discrete set of elements
    • A continuous range/interval of elements
  • The Condition(s):
    • Single
    • Compound
    • Nested
Testing Conditionals Involving Discrete Sets
Back SMYC Forward
  • Possible Errors:
    • An error involving one or more elements of the set
    • An error that involves non-elements
  • Test Cases to Include:
    • Each member of the set
    • A value smaller than the smallest member
    • A value larger than the largest member
    • A value between two members
Testing Conditionals Involving Discrete Sets (cont.)
Back SMYC Forward

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
Back SMYC Forward
  • Possible Errors:
    • An error involving one or more elements of the set
    • An error that involves a boundary of the set (whether the interval is open or closed)
    • An error that involves non-elements
  • Test Cases to Include:
    • Several elements in the interval
    • A value smaller than the lower bound
    • A value larger than the upper bound
    • The lower bound
    • The upper bound
Testing Conditionals Involving Intervals/Ranges (cont.)
Back SMYC Forward

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
Back SMYC Forward
  • Possible Errors:
    • All of the errors above, but now in various combinations
  • Test Cases to Include:
    • All combinations (if possible)
Compound/Nested Conditions (cont.)
Back SMYC Forward

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.)
Back SMYC Forward

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
Back SMYC Forward
  • The Basics:
    • Include print() calls before, inside, and after the conditional statements
    • Include print() calls that check for "accidental assignments" (i.e., use of the = operator instead of the == operator)
  • A Warning:
    • You may need to reorganize your code (e.g., use intermediate variables) in order to print some values
An Example of Instrumenting Conditionals
Back SMYC Forward
javaexamples/basics/DebuggingConditionals.java (Fragment: 0)
 
There's Always More to Learn
Back -