How to Test Your Program Using Redirection and the diff Command

For programs that require user responses at a command line prompt it is possible to simulate the user responses by placing the appropriate values in a file and redirecting them to the program when you run it.  It is also possible to redirect the program's output to a file. Additionally, Unix/Linux/Mac environments provide a command line tool, called diff, that allows you to compare two files to see if they are the same. By providing you with one or more file-pairs, one containing a set of inputs and the other containing the expected output of your program for those inputs, it is possible for you to easily test your program to see if it is correct. Likewise, it is easy for me to grade them for correctness. Plus, you get to learn a little more Unix - all good outcomes!

Step 1 - Develop and test on your own until you believe your program is 100% correct.

The first step is for you to develop your program, testing as you go, until you believe it is correct.  I strongly suggest not using the methods described in this document until that time. Otherwise, you are likely be overwhelmed by the output and end up wasting lot of time trying to figure out what the tools are trying to tell you. This method, while useful, is not necessarily user-friendly - especially to people new to the environment.

Step 2 - Run your program with the provided input file(s) to produce an output file(s) using redirection.

When you believe your solution is correct you should then obtain the provided input/expected-output file pair(s) and put them in the same directory as the executable you are testing.  If none are provided, and you want to use this technique, you will have to create them yourself.

To capture your program's output for the provided inputs you can use redirection. The screen shot below shows you the contents of a directory before the redirection, the actual command to do the redirection, and the contents of the directory after the command. Note that you can name the input and output files what ever you wish.  In this example the input file is called testInput and the output file is called testOutput.  Notice the use of the < and > symbols,  < redirects the file to its right to the program as input, > redirects any output from the program to the file specfied to its right. The output file will be created if it does not already exist and it will be overwritten if it does.

If the command does not seem to end you can press <cntl>-c to force it to stop. This usually means your program is waiting for more input (and the input file doesn't have any). There is either a bug in your code OR the test input file is not correct OR both, start debugging!

If the command ran as expected output file, whatever you called it, was created (or overwitten) and contains the actual output of your program for the provided inputs. It is a text file and you can examine it with a text editor but it will look a little strange because it does not contain the user's responses, only the output of your program. This is where the diff-command comes in handy.

Step 3 - Compare your program's actual output with the expected correct output.

The screen shot below show what you should see if you program is 100% as expected:


Notice the message "Files expectedOutput and testOutput are identical" in the screen shot above.  This means your program produced the correct results in the correct format - congratulations! The screen shot below shows what you might see if your program is not correct (details will, obviously, be different for different errors and/or different programs):

Can you see the difference? The density differs by 0.000001. Notice the 7c7, that is diff telling you the line numbers of the lines in the two files.  The number on the left is the line number of the difference in first file listed in the command and the number on the right is the line number of the difference on the second file listed in the command.  Also notice the < and > symbols.  The < indicates the line was from the first file listed in the command.  The > indicates the line was from the second file listed in the command. Also notice the ---, This separates the displayed lines from each of the two files for the specific difference being shown.

Some useful diff options if your program is not correct:

NOTE: For your program to be considered correct it must pass using only the -s option (or no options at all).

diff -sw : Causes diff to ignore whitespace difference (spaces, tabs, et cetera).  If you can't see the difference between your output and the expected correct output try this option - if you then "pass" it means you have to look more closely for differences in whitespace.

diff -sy : Caused diff to display the differences, if any, side-by-side.  This might make it easier to find the differences. You will want to increase the width of your termial window when using this command to make it more useful.

You can combine the options, for example diff -swy would cause diff to ignore whitespace and show any difference side-by-side.

There are other diff options, type "man diff" at your command line prompt to read about them (press <space> to page down, up & down arrows to move around, <q> to quit) or use your favorite search engine to scour the web.