EECS168 09:Lab7
| Navigation | 
|---|
| Home | 
| Information | 
| Classwork | 
Objectives
- Write a driver program.
 - Write a program with stub.
 - Understand the concept of Testing the functions
 
Testing and Debugging Functions
If a program uses multiple functions, each function should be tested as a separate unit from the rest of the program. For this a separate program should be written to do testing, such a program is called as driver program. Once a function is fully tested, it can be used in the driver program to test another functions. Each program should be tested in a driver program in which it is the only untested function.
Sometimes it is impossible to test a function without using an untested function. In such cases, we use simplified, missing version of the untested function. Such a simplified version untested function is called stub. The stub need not necessarily perform exact calculations.
To read more about Testing and Debugging Functions refer to page 282 of your book
Program to calculate Factorial of a number
In the present lab, write a program Factorial.cpp that reads in a number, computes the factorial of that number and outputs the result . The program should contain three functions:
- Function name: get_input
- Parameter: Number
 - Return value: None (void)
 - This function is used to read in the Number value.
 
 - Function name: fact_function
- Parameter: Number, Factorial
 - Return value: None (void)
 - This function should compute the factorial of the read number.
 
 - Function name: give_output
- Parameter: Number, Factorial
 - Return value: None (void)
 - This function should display the computed factorial of the number.
 
 
Factorial of a number "n" is represented as "n!" and the formula is:
n! = n*(n-1)*(n-2)*........3*2*1.
Examples: 
4! = 4*3*2*1 = 24
6! - 6*5*4*3*2*1 = 720
Copy the following code into Factorial.cpp and get started.
#include <iostream>
using namespace std;
void get_input(int& Num);
void fact_function(int& Num, int& Fact);
void give_output(int& N, int& F);
int main()
{
int Number, Factorial;
// FUNCTION CALLS
return 0;
} 
// FUNCTION DEFINITION for get_input
// FUNCTION DEFINITION for fact_function
// FUNCTION DEFINITION for give_output
All the three functions are untested functions. We need to test all the three functions.
Program to test "get_input" function
Write a driver program driver_test_input.cpp to test the function get_input. This program should contain only get_input function and should not use any other untested function. You can test the function by printing out the read input value in the main program. You should include option to test the function again if the user chooses to do so.
Program to test "give_output" function
Write a program stub_test_output.cpp to test the function give_output. This program cannot be tested without using other functions. So, it contains already tested get_input function as it is but the untested fact_function as stub. You should define the simplified version of fact_function as a function that assigns known value to the Factorial say "40" instead of actually performing the factorial calculations. And perform the testing. You should include option to test the function again if the user chooses to do so.
Program to test "fact_function" function
Write a program driver_test_fact.cpp to test the function fact_function. This program contains already tested get_input and give_output and tests the function fact_function. You should include option to test the function again if the user chooses to do so.
Note: The function definitions of ALL the tested functions that are used in the driver programs should be in its original version (as it was used in the actual program Factorial.cpp). The stub functions should be in simplified version.
Lab Submission
- Factorial.cpp
 - driver_test_input.cpp
 - stub_test_output.cpp
 - driver_test_fact.cpp