CSC 161 | Grinnell College | Spring, 2009 |
Imperative Problem Solving and Data Structures | ||
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.
Recall that a prime number is a positive integer with exactly two factors: itself and 1. Thus, the number 1 itself is not prime.
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).
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.
State pre-conditions and post-conditions for program prime, and include these assertions near the header of your main function.
Add one or more assert statements (as needed) to check the pre-conditions for program prime.
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.
Run prime with all cases from your test plan to be sure it works correctly.
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.
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?
Modify the data in prime-test to determine what happens when the data are placed in a different order:
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.
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
In this section, we will automate the testing of program prime using a shell script that runs the program repeatedly.
Create a new file prime-test-script with the following contents:
gcc -Wall -o prime prime.c ./prime < prime-test ./prime << ! 5 6 -5 0 !
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.
After saving prime-test-script, set its permissions so that it may be run as a GNU/Linux script:
chmod 700 prime-test-script
Run the script in a terminal window by typing
./prime-test-script
Describe what happens.
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/161.sp09/labs/lab-testing.shtml
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 |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |