Goals

To experience use of mygcc to compile C programs, to use your favorite editor to edit a C program, and to write a shell script to test the C program.

Preparation

Copy the guessprime program into your own directory on plank, and compile it:

cp /thayerfs/courses/22fall/cosc050/workspace/activities/day5/guessprime4.c guessprime.c
mygcc -o guessprime guessprime.c

Try running the program a few times.

With a shell script, you can send input to a program as you typed it from stdin as in this example:

echo 2 3 31 | ./guessprime

This code essentially connects the output (stdout) of echo to the stdin of guessprime. In this case guessprime will first read the 2 as if you typed it from the keyboard, then it will read a 3, finally it will read the 31.

Task

Write a shell script to test guessprime with various inputs. Think about defensive programming and what user input you’d like to test (for now you can assume the input is always an integer, we address non-numeric input soon). Ensure your script tests all the cases you’ve written in the C code. Test to see if the program behaves as expected when the following are entered:

  • Correct answer (e.g., 31)
  • Numbers too small or too large
  • Non-prime numbers
  • Incorrect, but prime numbers
  • Edge cases numbers.

Your script should echo the condition it is testing, and the expected output, then should run the script. You can then compare the expected output with the actual output. For example:

answer=31  # make sure no white spaces before and after =
echo "TEST 1: test with correct answer"
echo "should see no output after prompt to enter number"
echo $answer | ./guessprime 
echo 
echo

Make sure you give 31 as the last input so the script stops!

Candidate Solution