Laboratory Exercise on Program Correctness and Testing
Work Started in Class
Testing
Pre- and Post-Conditions
-
Review the MyroC.h header file.
-
In the documentation, find at least two functions that have stated a "pre-condition" and at least two functions that have stated a "post-condition".
-
Using these functions as examples, write a careful definition of what is meant by the terms ,"pre-condition" and "post-condition".
-
In anticipation of later work in this lab, review the documentation for the function rMotors. The documentation does not explicitly state pre-conditions for this function, but one might infer such conditions. Write a careful statement of the implied pre-condition(s) for rMotors.
-
-
The program motors-test.c sets the motor speeds of the Scribbler to the given leftspeed and rightspeed.
-
Initialize the variables leftspeed and rightspeed to 1.
-
Initialize the variables leftspeed and rightspeed to 1 and -1 respectively.
-
Initialize the variables leftspeed and rightspeed to 2 and -1. respectively.
-
Now try 6 and 5 respectively.
-
Try other numbers that you might need to figure out what works and what doesn't.
-
How do these experimental results compare with the pre-conditions that you inferred in Step 5c?
-
The assert Function in C
-
Modify the same code, motors-test.c, to use
assert
so that it will test the precondition(s) you wrote forrMotors
inmotors-test.c
.Note: You can read about C's assert function in the reading on program management and assertions and/or using the command man assert in a terminal window. Better yet, read about assert in both places!
Choosing Test Cases
-
Copy the program object-avoid.c to your working directory. This program attempts to control the robot in avoiding obstacles. Run the program a few times and observe what it does.
-
Develop two test plans for object-avoid.c to figure out if the program works correctly. That is, apply both black-box and white box testing by identifying test cases that will cover a full range of situations that might be encountered in executing the program
object-avoid.c
. Remember that:-
Black-box testing is when the problem is examined to determine the logical cases that might arise. Test cases are developed without reference to details of code.
-
White-box testing is when the code is examined to determine each of the possible conditions that may arise, and tests are developed to exercise each part of the code.
-
-
You should have found that the program is not working properly. Now fix the program so that it performs in the intended way and the robot avoids obstacles.
-
Homework
Debugging
-
Program follower-test.c is supposed to control the robot in following a sheet of paper which is held in front of the robot.
-
Develop two test plans for this program, using both black-box and white-box testing strategies. Apply both black-box and white box testing to identify tests cases that will cover a full range of situations that might be encountered.
-
Run
follower-test.c
with all the cases from your test plan to determine if the program works correctly. -
Fix the errors you found in the program.
-
Run the program again with all the cases from your test plan to be sure that it now works correctly.
-
More Values and Addresses as Parameters
ints by Reference
-
Consider the program rand-beep.c.
-
Copy this program to your account, review the code, and run it a few times to see what it does.
-
Write a few sentences explaining what the program is doing, making sure include the following points:
-
Identify every time the address of an int is passed into a function.
-
When addresses are passing into a function, explain how the values at these addresses are referenced within the function.
-
What durations are possible for the robot to beep for?
-
What frequencies are possible for the robot to beep for?
-
When will the loop end?
-
-
Indicating Function Success
-
Write a simple function which finds the square root of a number. It will have the following signature: int safe_sqrt (double * num).
-
It will use the double sqrt(double num) function from the math.h library to find the square root of num. Make sure you compile with the -lm flag when using sqrt().
-
If num is zero or positive, your function will modify num to be its square root and return 1 to indicate success.
-
If num is negative, your function will not modify num and return 0 to indicate failure.
-
You will have to use the & operator when calling your function so that num is passed in by reference.
-
Using C's math library math.h
The header file math.h for C's math library can be used in the same way as other C libraries, and compiling a program that includes math.h can proceeds without trouble. That is, the translation of a C program using the math library follows the same steps as programs using other libraries.
However, by default, the gcc compiler does not link compiled programs with the math library. (The math library is large and thus is not added to an executable program, unless needed.) To instruct the computer to link your program to the math library, you need to use the -lm option when compiling:
gcc -lm ...
Addresses
-
Copy get-ir.c into your directory for this lab and write a few sentences explaining what the program is doing.
Complete the parts of the program which say "code here!". It should only require one line of code at each spot, and there are three locations where you must add code.
Hint: If you are getting the warning "assignment makes pointer from integer without a cast", try putting * (an asterisk) in front of the integer you are assigning to.
created 20 July 2011 by Erik Opavsky and Dilan Ustek revised 29 July 2011 by Erik Opavsky and Dilan Ustek revised 12 October 2011 by Erik Opavsky revised 31 October 2011 by Erik Opavsky revised (shortening lab (removal of old steps 1, 6, 7)) 21 July 2012 by Henry M. Walker reorganized within module 27-28 January 2014 readings added 19 September 2014 by Henry M. Walker wording refined for passing values and addresses as parameters 21 September 2014 by Henry M. Walker reformatted 12 August 2016 by Henry M. Walker split testing from function pointers 29 March 2022 by Henry M. Walker |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |