Testing and Debugging Functions

  1. Each function should be designed, coded and tested as a separate unit from the rest of the program.

  2. In order to test a function, special temporary main functions called driver functions are used to call the function using values input from the keyboard and testing the function's output parameters. A loop is used in the driven function so that different input values can be tested without having to rerun the program.

  3. Once a function is fully tested, it can be used to test other functions.

  4. It is sometimes difficult to test a function without using some other untested or not yet finished functions. In those cases simplified versions of the untested or not yet finished function called stubs are used to deliver values that suffice for testing.

  5. //Driver program to test the function get_input.
    #include <iostream>
    using namespace std;

    void get_input(double& cost, int& turnover);
    //Precondition: User is ready to enters values correctly.
    //Postconditions: The value of cost has been set to the
    //wholesale cost of one item. The value of turnover has been
    //set to the expected number of days until the item is sold.

    int main ( )
    {
          double wholesale_cost;
          int shelf_time;
          char ans;

          do
          {
                get_input(wholesale_cost, shelf_time);

                cout.setf(ios::fixed|ios::showpoint);
                cout.precision(2);
                cout << "Wholesale cost is now $" << wholesale_cost << endl;
                cout << "Days until sold is now " << shelf_time << endl;

                cout << "Test again?" << " (Type y for yes or n for no): ";
                cin >> ans;
                cout << endl;
          } while (ans == 'y' || ans == 'Y');

          return 0;
    }

    //Uses iostream:
    void get_input(double& cost, int& turnover)
    {
          cout << "Enter the wholesale cost of item $";
          cin >> cost;
          cout << "Enter the expected number of days until sold: ";
          cin >> turnover;
    }

  6. // Illustrate the use of a stub to test another function.
    // The function compute_price has not yet been written.
    // It is only a stub.
    // Want to test the main and all the other functions except compute_price.

    #include <iostream>
    using namespace std;

    void introduction( );
    //Postcondition: Description of program is written on the screen.

    void get_input(double& cost, int& turnover);
    //Precondition: User is ready to enters values correctly.
    //Postconditions: The value of cost has been set to the
    //wholesale cost of one item. The value of turnover has been
    //set to the expected number of days until the item is sold.

    double compute_price(double cost, int turnover);
    //Preconditions: cost is the wholesale cost of one item.
    //turnover is the expect number of days until sale of the item.
    //Postcondition: Returns the retail price of the item.

    void give_output(double cost, int turnover, double price);
    //Preconditions: cost is the wholesale cost of one item; turnover is the
    //expect time until sale of the item; price is the retail price of the item.
    //Postconditions: The values of cost, turnover, and price have been
    //written to the screen.

    int main ( )
    {
          double wholesale_cost, retail_price;
          int shelf_time;

          introduction( );
          get_input(wholesale_cost, shelf_time);
          retail_price = compute_price(wholesale_cost, shelf_time);
          give_output(wholesale_cost, shelf_time, retail_price);
          return 0;
    }

    //Uses iostream:
    void introduction( )
    {
          cout << "This program determines the retail price for\n"
                << "an item at a Quick-Shop supermarket store.\n";
    }

    //Uses iostream:
    void get_input(double& cost, int& turnover)
    {
          cout << "Enter the wholesale cost of item $";
          cin >> cost;
          cout << "Enter the expected number of days until sold: ";
          cin >> turnover;
    }

    //Uses iostream:
    void give_output(double cost, int turnover, double price)
    {
          cout.setf(ios::fixed|ios::showpoint);
          cout.precision(2);
          cout << "Wholesale cost = $" << cost << endl
                << "Expected time until sold = "
                << turnover << " days" << endl
                << "Retail price= $" << price << endl;
    }

    //This is only a stub:
    double compute_price(double cost, int turnover)
    {
          return 9.99;      //Not correct, but good enough for testing.
                //Compilers usually will produce warnings about
                //unused parameters.
    }