Function Pointers and Testing
This lab covers two main topics:
Function Parameters
As in Scheme, a function can be a parameter for another function. In Scheme, of course, all parameters are un-typed — the parameter may have one type (e.g., a number) at one time, then another type (e.g, a string) for another call, and yet another type (e.g., a function) for a third call. In C, each variable and parameter has a designated type. However, as we shall see, the designated type can indicate a function. For example, a function might have the following signature:
void printTable (double func (double, double))
-
Program
func-param.cuses a functionprintTablewith the signature above to print a table of metric equivalents.-
Save, compile, and run the program, and describe what
is printed. Also, explain the roles of the
functions
toLitersandtoCentimeters, and how these functions are utilized to obtain the output. -
Function
printTableprints a table, with rows always going from 0.0 to 10.0 and the columns going from 0.0 to 10.0, based on local variablesrowStart,rowEnd,colStart, andcolEnd. In this example, however, it would make more sense for the number of pints to go from 0.0 to 7.0 (number of pints less than a gallon), and the number of inches to go from 0.0 to 11.0 (number of inches less than a foot).Replace these local variables with parameters, so that function
printTablehas the new signature:
Update the documentation to match this new signature. Be sure to think carefully about the preconditions!void printTable (double rowStart, double rowEnd, double colStart, double colEnd, double func (double, double)) -
Change the calls to
printTablein the main program to more appropriate limits for each type of table.
-
Save, compile, and run the program, and describe what
is printed. Also, explain the roles of the
functions
-
Within a program to control a Scribbler 2 robot, write a function with the
following signature:
The function should move the Scribbler 2 robot 5 times using the following sequence:void boxMove (void movement (double, double))- move forward for 1 second
- call the
movementfunction with values 0.6, 1.0.
The main part of the program should call
boxMoveusing the MyroCrTurnLeftfunction as parameter and then callboxMoveagain usingrTurnRight.Why do you think the name
boxMovewas chosen? Is there a better name?
Arrays of Functions
As noted earlier in this lab, functions as parameters provide one mechanism to take advantage of common elements within an algorithm. A second approach involves utilizing an array of functions.
Program
func-param-arrays.c
produces exactly the same output as
program func-param.c
from Step 1.
-
Copy this program to your account, compile it, and run it to check that the
output matches the output from Step 1.
-
Check that the functions
toLiters,toCentimeters, andprintTableare unchanged from Step 1. -
In the main program, note how variable
titlesis declared and initialized as an array of two strings. (In this context,char *indicatestitleswill refer to the start of strings, and[2]indicates the array will refer to the start of 2 strings.) -
In the main program, note how the
variable
conversionsrefers to an array in which each item contains the address of a function taking twodoubleparameters.
-
Check that the functions
-
Write 3 functions that cause the Scribbler 2 robot to react in different
ways (e.g., move/turn, beep, spin). Then, write a program that uses these
three functions as follows.
-
Within
main, make an array that references each of these functions. - Use a loop to execute these functions in order, and then use a second loop to execute the functions in reverse order.
-
Within
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". (Note that every function should have postconditions, otherwise there would be not point in calling it.)
-
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) forrMotors. -
The program
motors-test.csets the motor speeds of the Scribbler to the givenleftspeedandrightspeed.-
Initialize the variables
leftspeedandrightspeedto 1. -
Initialize the variables
leftspeedandrightspeedto 1 and -1 respectively. -
Initialize the variables
leftspeedandrightspeedto 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?
-
Initialize the variables
The assert Function in C
-
Modify the same code,
motors-test.c, to useassertso that it will test the precondition(s) you wrote forrMotorsinmotors-test.c.
Note: You can read about C's assert function in
the reading for this lab 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.cto 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.cto 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 programobject-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.
-
Develop two test plans
for
Debugging
-
Program
follower-test.cis 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.cwith 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.
