CSC 161 Grinnell College Spring, 2010
Imperative Problem Solving and Data Structures

Program Correctness and Program Testing


This laboratory exercise provides practice with several elements of program correctness and design:


Several elements of this lab are based on lab materials from Marge Coahran. Throughout, text and approaches from Ms. Coahran are integrated with assignments and labs by Henry M. Walker.


Getting Started

Recall that a prime number is a positive integer with exactly two factors: itself and 1. Thus, the number 1 itself is not prime.

  1. Write a C program prime that reads a positive integer n, and prints whether n is a unit (i.e., 1), a prime, or a composite (i.e., a non-prime greater than 1).

  2. Modify program prime so that it reads successive integers until encountering the number zero. For each non-zero number read, the program should print whether n is a unit (i.e., 1), a prime, or a composite (i.e., a non-prime greater than 1).

Use your expanded code from step 2 as the basis for the rest of this laboratory exercise.

Pre- and Post-Conditions

  1. State pre-conditions and post-conditions for program prime, and include these assertions near the header of your main function.

The assert function in C

  1. Add one or more assert statements (as needed) to check the pre-conditions for program prime.

Choosing Testing Cases

  1. Develop a test plan for program prime. That is, identify tests cases that will cover a full range of situations that might be encountered in executing program prime.

  2. Run prime with all cases from your test plan to be sure it works correctly.

Testing with I/O Redirection

  1. Place the test cases from your test plan in a separate file prime-test, with each separate test case starting on a new line. Be sure that the last test case is 0 — the exit condition for the program.

  2. Run your program prime with input from the prime-test file:

       ./prime < prime-test

    Describe the output obtained. For example, what information is printed? How do you know which numbers were read?

  3. Modify the data in prime-test to determine what happens when the data are placed in a different order:

    1. What happens if 0 is not the last test case?
    2. What happens if several test cases are placed on the same line?
    3. What happens if you skip lines in the prime-test file?

The value of testing C programs in this way is that it allows you to use the same test cases multiple times without retyping them. Why is this useful? Consider the possibility that the first time you test a given case, your program gives an incorrect response. Once you fix the problem, you will want to test it again, and you will want to be sure that you have tested it on the same data. Further, you will want to re-test all of your previously working cases to make sure that your most recent change did not cause other cases to fail.

It is good practice (though a somewhat difficult habit to get started) to maintain a set of test cases for each program you write. This makes it easy to re-test your entire program when a new change is made. Re-running all your test cases for each new change is known as system testing.

  1. Experiment with output redirection to a file prime-out. (Be sure you choose a file name you have not used previously! Why?) Use the command:

       ./prime < prime-test > prime-out
    1. What appears in the terminal window?
    2. Look at the file prime-out. Describe what you see.

Testing with a Shell Script

In this section, we will automate the testing of program prime using a shell script that runs the program repeatedly.

  1. Create a new file prime-test-script:

       gcc -Wall -o prime prime.c
       ./prime < prime-test
       ./prime << !

    That is, this script first contains the command to compile program prime and then contains the command to run prime with the test cases you wrote from steps 7-10. Finally, the script adds a few new test cases.

  2. After saving prime-test-script, set its permissions so that it may be run as a GNU/Linux script:

       chmod 700 prime-test-script
  3. Run the script in a terminal window by typing


    Describe what happens.

This document is available on the World Wide Web as

parts of this lab developed February 2007 - February 2008 by Marge Coahran
this version created 18 May 2008 by Henry M. Walker
last revised 25 January 2009
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at