Lab: Head to Head Testing

Steven J. Zeil

Last modified: Dec 02, 2014

Contents:
1. Lab Instructions
2. Problem Description: Triangle Diagnosis

There are many circumstances where you are working on a program but have access to a working (or nearly working) executable.

Whenever you find yourself working on a system for which you already have a (nearly) working executable, you can take advantage of this by doing head to head testing, in which you

Whatever the reason, it’s the differences in outputs that are particularly interesting. Spotting those differences can be quite tricky, however, if they are buried in a sea of nearly identical outputs or if the differences are “invisible” changes like adding blanks at the ends of lines or improperly indenting output.

Luckily, there are simple tools that you can use to find and highlight those differences.

1. Lab Instructions

  1. Read the problem description below.

  2. You will find files for this lab here.

    Unpack the archive (unzip head2headFiles.zip).

    Compile the program on Linux (use the command “make”). The executable should be named triangle. Rename it to triangleA.

  3. Now introduce a simple bug of some kind into triangle.cpp. For example, swap the “else if” and output statements for the isosceles and equilateral cases in report().

    Recompile the program and rename the executable to triangleB.

  4. Run the input files provided through each version of the program. To start with, try running

    ```sh ./triangleA < test0.in

    ```

    just to be sure things are working.

    We saw the use of < to redirect input so we would not have to type everything manually in a much earlier lab.

    We can also use > to redirect output into a file that otherwise would have gone to our screen. So run

    ./triangleA < test0.in > test0A.out
    ./triangleA < test1.in > test1A.out
    ./triangleB < test0.in > test0B.out
    ./triangleB < test1.in > test1B.out
    
    

    You now have four output files. Inspect these with more or an editor.

  5. Next we are going to explore some of the common tools for quickly and automatically comparing output files head-to-head to see if any differences exist and, if so, what those differences are.

    Unfortunately, although “diff” tools like these are widely available, they are not a standard part of Windows distributions. They are, however, available in just about any Unix or Linux system.

  6. diff is pretty straightforward, but the output isn’t particularly pretty. Try this next:

    sdiff test0A.out test0B.out | more
    sdiff test1A.out test1B.out | more
    

    (Hit ‘q’ to exit the “more” program.)

    sdiff shows the two files “side by side”. You may find it useful to make the window for your ssh session a bit wider when using this program. Lines that match exactly are shown side by side with no marker in between them. Lines that appear to have been changed have a ‘|’. Lines that appear to have been added or removed from one file but not the other are marked with a ‘<’ or ‘>’ pointing at the “extra” line.

  7. If you have progressed in CS252 as far as the exercises on X, you might want to try the emacs in X assignment next. As part of that, you will be using a more interactive difference viewer in emacs that steps from one change to another, highlighting the differences in color.

  8. Although this lab has focused on programs that you would use in a Unix or Linux environment, you can perform head-to-head testing in Windows environments as well. Even if your program writes to standard output, you can use output redirection in a Windows cmd window to capture the output into a file, just as you uses input redirection to a program in the earlier “Supplying Inputs to Programs” lab.

    If you are seated as a CS Dept lab Windows PC, run the program winmerge. (If you are not in a CS lab, use remote desktop to visit the CS Virtual PC lab and run winmerge there.

    From the File menu, select “Open.”. Use the “Left” and “Right” controls to select the two files that you want to compare, the test0A.out and test0B.out files that you created in your Linux ssh session. ( These should be available in your Z: drive.)

    If all you get is a simple report saying that the files are “different”, try selecting “Expand” from the View menu.

  9. Repeat the winmerge process with the test1A.out and test1B.out files

Winmerge is quite a useful program. You can get your own copy at WinMerge. It is also available in a portable version that can be installed on a USB flashdrive and executed on almost any Windows machine).]

2. Problem Description: Triangle Diagnosis

This is a simple program that reads in three floating-point numbers at a time. These numbers represent the lengths of the three sides of a triangle. The program interprets these to classify the triangle and prints the name of the kind of triangle that it is. You may remember these terms from a geometry class long, long ago:

In addition, there are some less obvious cases:

The program reads from the standard input, three numbers at a time, until end of input is reached. For each trio of numbers, it prints a triangle classification to the standard output.