CS 239 Advanced Programming
Lab239-04: Testing and Debugging

Objectives

The students will be able to:

Purpose     

Many programmers are required to correct code that has been created by someone else.  Often that requires a simple change; other times you need to find a pesky bug.  Before you can correct the program, you must understand what it is doing, then attempt to determine what is not working correctly.

You will build a test suite for this program.  You will be testing this program to see if it works, then you will fix the problem(s) encountered.  Finally, you will retest the program using the original test suite.

Background

This program will read in an expression in postfix notation (the operator follows the operands) and displays it in infix notation (operator in between the two operands).

            Example of infix:            1+a
            Example of postfix:            1a+
            Example of prefix:            +1a   (not used in this program)

In addition, this program reads in the operators as hexidecimal (base 16) digits.  The hexidecimal digits are:
0            1            2            3            4            5            6            7            8            9            a            b            c            d            e            f

The output of the program is the original expression in infix notation and the result converted to base 10.

For example, 3f+ should result in the line “3 + f = 18”

But, there is a problem.  The program is wrong.  There are compiler errors as well as logic errors in carrying out this work.  Your job today is to correct those errors.

New Terms

test case
A single test of a software product
test suite
A set of test cases for a sofware product
infix notation
arithmetic operation notation in which binary operators sit between the two operands (a + b)
prefix notation
arithmetic operation notation in which the binary operator comes before the two operands (+ a b)
postfix notation
arithmetic operation notation in which the binary operator comes after the two operands (a b +)

Material

Testing worksheet: This worksheet will be provided in hard copy form in the lab.

 


Part 1 - Designing your test cases- Black box test

Your tasks-you will have one hour to complete this lab, allowing a brief follow-up at the end:

  1. Understand the problem. You can't build test cases until you are thoroughly aware of the problem at hand.
  2. Build a test suite of potential values and the expected result.  Use the worksheet provided.  (Put both partner’s names on the paper).

 

Designing tests: You should consider "normal cases", extremes, and errors.

A normal case would be anything expected such as ab+ or a6-.

An extreme would use values on the extremes of the acceptable range like 0 and f.

Errors would be Strings that had incorrect values such as ab or xyz0. The empty and null Strings should be considered.

How many test cases will you need to "black box test" your code?

 

  1. Compile the program.  Take care of any compile problems if there are any.
  2. Run your test cases against the program.  Record your results.  Do they match your prediction?  

Part 2 - Debug the code and white box test

  1. Read the code carefully.  Try to figure out what it is doing and how it is doing it.
  2. Note the code that says: 
                            if (operand1 >= 'a' || operand1 <= 'f')
                               operand1Value = 10 + operand1 - 'a'; 

    This is a technique for changing characters into their equivalent integers.  See ASCII code table.  A char is a representation and an int is an actual number as stored in the corresponding byte.  In this example, if ‘f’ were the character its value will be the value of ‘f’ – ‘a’.  That result would be 5 (since ‘f’ is 5 higher than ‘a’).

  3. Add in any test cases that you need to test branches in the code that are not covered by your original test suite.  (White box test)
  4. Begin to work through the errors. 
  5. Continue to test using your test cases.  When you have gotten a successful run with all of your test cases raise your hand.
  6. To “submit” this lab work, put both partner’s names on the top of the test worksheet. 

NOTE:  Notice the DEBUG constant at the beginning of the program.  This constant is used to control whether we are debugging the program or not.  By building the debug lines into the code and conditioning those lines by the constant’s value, you can successively test with or without the extra lines.  Notice that each debug line gives you a context for where that line is.  This is especially important if you are debugging several methods at once or alternate choices in a select statement. 


created by Arch Harris
last updated 01/12/13 - nlh